P2419 [USACO08JAN] Cow Contest S
作者:
闪回
,
2024-03-30 22:18:55
,
所有人可见
,
阅读 3
bool型floyd算法
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
bool g[N][N];
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++)
g[i][j] = g[i][j] || (g[i][k] && g[k][j]);
}
int main()
{
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
g[a][b] = true;
}
for(int i = 1;i<=n;i++)g[i][i] = true;
floyd();
int ans = 0;
for(int i = 1;i<=n;i++)
{
bool flag = true;
for(int j = 1;j<=n;j++)
{
flag = flag && (g[i][j] || g[j][i]);
}
if(flag)ans++;
}
cout<<ans<<endl;
return 0;
}