传递闭包
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
已知有n头牛,m次战斗关系,询问最终可以确定排名的牛的数量。
Input
多组测试数据,对于每组测试数据,第1行输入两个整数n(1 <= n <= 100)和m(0 <= m <= 4950),分别表示有n头牛和m次战斗关系,之后m行每行输入两个正整数x和y表示编号为x的牛可以战胜编号为y的牛,数据保证合法,询问可以确定排名的牛的数量。
Output
对于每组测试数据,输出整数ans,表示可以确定排名的牛的数量。
Sample Input
5 5 4 3 4 2 3 2 1 2 2 5
Sample Output
2
Hint
Source
xry-fhf
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn = 110;
int d[maxn];
int mp[maxn][maxn];
void Flord(int n)
{
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
mp[i][j] = mp[i][j] || (mp[i][k] && mp[k][j]);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int a,b;
memset(mp,0,sizeof(mp));
while(m--)
{
scanf("%d%d",&a,&b);
mp[a][b] = 1;
}
Flord(n);
int ret = 0;
for(int i = 1; i <= n;i++)
{
int ans = 0;
for(int j = 1;j <= n;j++)
{
if(i == j)continue;
if(mp[i][j] || mp[j][i])ans++;
}
if(ans == n - 1)ret++;
}
printf("%d\n",ret);
}
return 0;
}

被折叠的 条评论
为什么被折叠?



