POJ - 3660 Cow Contest(floyed 传递闭包)

Cow Contest
题 意:有N头牛去参加比赛,给你M场两两比赛的结果,问你至少能确定多少头牛的排名。
数据范围:
1<=N<=100
1<=M<=4500

输入样例:

5 5
4 3
4 2
3 2
1 2
2 5

输出样例:

2

思 路:任何一头牛排名的确定由其他n-1头牛确定,所以我们只要知道任何一头牛和其他n-1头牛的关系,就能唯一确定。用floyd传递闭包。确定俩俩是否有关系则可。
收 获:传递闭包(lll¬ω¬) 更多floyd的姿势

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef pair<double,int> P;
const double INF = 0x3f3f3f3f;
const int maxn = 1e2+5;
int dp[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++){
                dp[i][j] = dp[i][j] || (dp[i][k]&&dp[k][j]);
            }
        }
    }
}
int main(){
    while(~scanf("%d %d",&n,&m)){
        memset(dp,0,sizeof(dp));
        for(int i=0;i<m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            dp[b][a] = 1;
        }
        floyd();
        int ans = 0,c = 0 ;
        for(int i=1;i<=n;i++){
            c = 0;
            for(int j=1;j<=n;j++){
                if(j == i) continue;
                if(dp[i][j] || dp[j][i]) c++;
            }
            if(c == n-1)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/81539542