3660 Floyd transit closure

Intention

Find out how many levels are determined

Thought

Using transfer characteristics such as; A>B, B>C, you can launch A>C. Set an array to record whether there is a relationship between the two cows, and change the judgment condition in floyd to (vis[i][j] || ( vis[i][k] && vis[k][j] ) ) , vis= 1 means there is a relationship. If ( vis[i][k] && vis[k][j] )=1 , then vis[i][j] =1 can be derived;

Whether the number of cattle that can be defeated by the i-th cow and the number of cows that can defeat the i-head are equal to n -1, and if equal, the level of the i-th cow can be determined.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
bool vis[110][110];
void floyd()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(vis[i][j] || (vis[i][k]&&vis[k][j]))
                    vis[i][j]=1;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int a,b;
        int s[110]={0};
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            vis[a][b]=1;
        }

        floyd();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(vis[i][j])
                    s[i]++,s[j]++;
            }
        }
        int k=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]==n-1)
                k++;
        }
       printf("%d\n",k);
    }
    return 0;
}