题目来源:POJ 3660 Cow Contest
题意:n头牛 下面m行 每行x y 代表牛x打败了牛y 问有几头牛的最终排名是确定的
思路:传递闭包 如果x打败了y 令a[x][y]=1 并且a[y][x]=-1 其他不知道的都为0 然后floyd
最后对于每头牛数一下是否有n-1个1或者-1(就是不为0) 如果有n-1个不为0 说明该牛和其他牛都确定了状态
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
using namespace std;
const int maxn = 110;
int a[maxn][maxn];
int n, m;
void floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(a[i][k] == -1 && a[k][j] == -1)
a[i][j] = -1;
if(a[i][k] == 1 && a[k][j] == 1)
a[i][j] = 1;
}
}
int main()
{
scanf("%d %d", &n, &m);
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
a[u][v] = 1;//赢
a[v][u] = -1;//输
}
floyd();
int ans = 0;
for(int i = 1; i <= n; i++)
{
int cnt = 0;
for(int j = 1; j <= n; j++)
{
if(i == j)
continue;
if(a[i][j])
cnt++;
}
if(cnt == n-1)
{
ans++;
//printf("%d\n", i);
}
}
printf("%d\n", ans);
return 0;
}