#include <bits/stdc++.h>
using namespace std;
int n, edge[205][205], path[205][205];
int fa[205];
void init(int n)
{
for(int i = 1; i <= n; ++i)
fa[i] = i;
}
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void floyd()
{
memcpy(path, edge, sizeof(edge));
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
path[i][j] = path[i][j] || path[i][k] && path[k][j];
}
int main()
{
int j, ct = 0;
cin >> n;
init(n);
for(int i = 1; i <= n; ++i)
while(cin >> j && j != 0)
edge[i][j] = 1;
floyd();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(path[i][j])
fa[j] = find(i);
for(int i = 1; i <= n; ++i)
if(find(i) == i)
ct++;
cout << ct;
return 0;
}
11-10
553
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)