USACO08JAN Cow Contest

题目链接

题解

用Floyd算法补全胜负关系,然后如果能够确定一个牛对所有其他牛的胜负关系就认为名次可确认。

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
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, m, d[105][105];
void init(){
memset(d, -1, sizeof(d));
n = read(), m = read();
for(int i = 0; i < m; ++i){
int u = read(), v = read();
d[u][v] = 1, d[v][u] = 0;
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(d[i][k] != -1 && d[k][j] != -1 && d[i][k] == d[k][j])
d[i][j] = d[i][k];
}
void solve(){
int ans = 0;
for(int i = 1; i <= n; ++i){
int w = 0, f = 0;
for(int j = 1; j <= n; ++j)
if(d[i][j] == 1) w++;
else if(d[i][j] == 0) f++;
if(w + f == n - 1)
ans++;
}
printf("%d\n", ans);
}
int main(){
init();
solve();
return 0;
}