题意: 给定牛a能打败牛b 求能确定几只牛的排名
思路:
- floyd 传递闭包
- 当一只牛能被打败与能打败牛的总数和为牛的总数减一或没有与其他牛未确定关系时牛的排名可确定
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool martix[101][101];
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++)
martix[i][j] = martix[i][j] || (martix[i][k] && martix[k][j]);
}
}
bool cnt(int cow){
int cnt;
for (int i = 1;i<=N;i++){
if (i != cow){
if (!(martix[i][cow] || martix[cow][i])) return false;
}
}
return true;
}
int main()
{
scanf("%d%d",&N,&M);
memset(martix,0,sizeof martix);
for (int i = 0;i<M;i++){
int a,b;
scanf("%d%d",&a,&b);
martix[a][b] = true;
}
floyd();
int succed = 0;
for (int i = 1;i<=N;i++)
if (cnt(i)) succed++;
printf("%d\n",succed);
return 0;
}