Floyd_Warshall POJ 3660 Cow Contest
题意: m组关系,A能打败B,问最后有几头牛的排名能确定
分析:如果排名确定,那么能打败它的到它一定通,它到能打败的一定能通,也就是和为n-1.用Floyd的传递闭包
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N = 1e2 + 5; bool d[N][N]; int in[N]; int n, m; void Floyd_Warshall(void) { for (int k=1; k<=n; ++k) { for (int i=1; i<=n; ++i) { for (int j=1; j<=n; ++j) { d[i][j] = (d[i][j] || (d[i][k] && d[k][j])); } } } } int main(void) { while (scanf ("%d%d", &n, &m) == 2) { memset (d, false, sizeof (d)); memset (in, 0, sizeof (in)); for (int u, v, i=1; i<=m; ++i) { scanf ("%d%d", &u, &v); d[u][v] = true; in[v]++; } Floyd_Warshall (); int ans = 0; for (int i=1; i<=n; ++i) { int c1 = 0, c2 = 0; for (int j=1; j<=n; ++j) { if (i == j) continue; if (d[i][j]) c1++; if (d[j][i]) c2++; } if (c1 + c2 == n - 1) ans++; } printf ("%d\n", ans); } return 0; }
编译人生,运行世界!