链接:https://vjudge.net/problem/POJ-3660
题意:
有N个牛(1-100),两两对决M次(1-2500)。
得到一个结果。求能准确确定名次的牛的个数。
思路:
一头牛可以被a头牛击败,同时击败b头牛时,这头牛的名次确定。
Floyd算法。
代码:
#include <iostream> #include <memory.h> #include <string> #include <istream> #include <sstream> #include <vector> #include <stack> #include <algorithm> using namespace std; const int MAXN = 100+10; int Map[MAXN][MAXN]; int win[MAXN],lose[MAXN]; int n,m; void Floyd() { for (int i = 1;i<=n;i++) for (int j = 1;j<=n;j++) for (int k = 1;k<=n;k++) if (Map[j][i] == 1&&Map[i][k] == 1) Map[j][k] = 1; } int main() { scanf("%d%d",&n,&m); int l,r; memset(Map,0,sizeof(Map)); for (int i = 1;i<=m;i++) { scanf("%d%d",&l,&r); Map[l][r] = 1; } Floyd(); for (int i = 1;i<=n;i++) for (int j = 1;j<=n;j++) { if (Map[i][j] == 1) { win[i]++; lose[j]++; } } int sum = 0; for (int i = 1;i<=n;i++) if (win[i]+lose[i] == n-1) sum++; printf("%d ",sum); return 0; }