poj3660


Time Limit: 1000MS Memory Limit: 65536K

Description

N (1≤N≤100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A (1≤A≤N) has a greater skill level than cow B (1≤B≤N; A≠B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1≤M≤4500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

Line 1: Two space-separated integers: N and M

Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

Line 1: A single integer representing the number of cows whose ranks can be determined

Sample Input

1
2
3
4
5
6
5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

1
2

Source

USACO 2008 January Silver

题解

题意:

n (1≤n≤100)个人参加比赛, 有m (1≤m≤4500)条实力信息, 对于每一条信息A B(1≤A,B≤n, A!=B)表示A的实力比B强.

在保证输入信息不矛盾的情况下, 求最后有多少个人可以确定自己的排名.

思路:

Floyd的模板题.

如果$v_i$与$v_k$有关系且$v_k$和$v_j$有关系并且三者的关系相同那么就进行松弛.

最后再计算有多少个人和其他n-1个人都有关系.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <cstring>
using namespace std;
const int maxn = 1e3 + 5;

int n, m;
int dis[maxn][maxn];

int ()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(dis[i][k] != 0 && dis[i][k] == dis[k][j])
dis[i][j] = dis[i][k];

int ans = 0;
for(int i = 1; i <= n; i++)
{
int tmp = 0;
for(int j = 1; j <= n; j++)
if(i == j)
continue;
else if(dis[i][j] != 0)
tmp++;
else
break;
if(tmp == n - 1)
ans++;
}
return ans;
}

int main()
{
ios_base::sync_with_stdio(0);cin.tie();
cin >> n >> m;
memset(dis, 0, sizeof(dis));
for(int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
dis[a][b] = -1;
dis[b][a] = 1;
}
cout << floyd() << endl;
return 0;
}