题意:
给出n的牛的m轮比赛结果,计算有几头牛的排名已经可以确定。
思路:
Floyd跑一遍,然后判断每个牛,如果他和别人都有比赛,那么它排名可定。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100+5;
using namespace std;
int n,m;
int G[maxn][maxn];
void Floyd(){
for(int k = 1; k <= n; ++k){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
G[i][j] = G[i][j]||(G[i][k]&&G[k][j]);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) == 2){
memset(G, 0, sizeof(G));
for(int i = 1; i <= n; ++i) G[i][i] = 1;
for(int i = 0; i < m; ++i){
int a,b; scanf("%d%d",&a,&b);
G[a][b] = 1;
}
Floyd();
int ans = 0;
for(int i = 1; i <= n; ++i){
bool flag = true;
for(int j = 1; j <= n; ++j) if(!G[j][i]&&!G[i][j]) {flag = false; break; }
if(flag) ++ans;
}
printf("%d\n",ans);
}
fclose(stdin);
return 0;
}