bzoj 1612: [Usaco2008 Jan] Cow Contest Cow Competition【Floyd】

Floyd transitive relationship, the condition for a cow to determine the ranking is to be able to determine the relationship with all cows

#include<iostream>
#include<cstdio>
using namespace std;
const int N=105;
int n,m,a[N][N],ans;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[x][y]=1;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                a[i][j]=a[i][j]|(a[i][k]&a[k][j]);
    for(int i=1;i<=n;i++)
    {
        int ok=1;
        for(int j=1;j<=n;j++)
            if(i!=j&&!a[i][j]&!a[j][i])
            {
                ok=0;
                break;
            }
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890671&siteId=291194637