POJ3660 Cow Contest —— Floyd 传递闭包
题目链接:http://poj.org/problem?id=3660
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13085 | Accepted: 7289 |
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 has a greater skill level than cow B (1 ≤ A ≤ N; 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 ≤ 4,500) 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
5 5 4 3 4 2 3 2 1 2 2 5
Sample Output
2
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++) 13 #define ms(a,b) memset((a),(b),sizeof((a))) 14 using namespace std; 15 typedef long long LL; 16 const double EPS = 1e-8; 17 const int INF = 2e9; 18 const LL LNF = 9e18; 19 const int MOD = 1e9+7; 20 const int MAXN = 1e2+10; 21 22 int n, m; 23 bool gra[MAXN][MAXN]; 24 25 int main() 26 { 27 while(scanf("%d%d", &n,&m)!=EOF) 28 { 29 memset(gra, false, sizeof(gra)); 30 for(int i = 1; i<=m; i++) 31 { 32 int u, v; 33 scanf("%d%d", &u,&v); 34 gra[u][v] = true; 35 } 36 37 for(int k = 1; k<=n; k++) //求传递闭包 38 for(int i = 1; i<=n; i++) 39 for(int j = 1; j<=n; j++) 40 gra[i][j] = gra[i][j] || (gra[i][k]&&gra[k][j]); 41 42 int ans = 0; 43 for(int i = 1; i<=n; i++) 44 { 45 int cnt = 0; 46 for(int j = 1; j<=n; j++) 47 if( gra[i][j] || gra[j][i] ) 48 cnt++; 49 if(cnt==n-1) //i与剩下的n-1个数都能确定关系,则i的位置确定 50 ans++; 51 } 52 53 printf("%d\n", ans); 54 } 55 }