代码如下 (本蒟蒻刚学拓扑排序)
#include<bits/stdc++.h>
using namespace std;
int deg[10000],maxx = 0x3f3f3f3f,sorted = 0;
vector<int> v[10000];
bool vis[10000];
void topsort()
{
queue<int> q;
while(1)
{
//cout<<"topsorting....."<<endl;
for(int i = 1; i <= maxx; i++)
{
// cout<<"chosing....."<<endl;
if(deg[i] == 0 && !vis[i])
{
q.push(i);
vis[i] = 1;
sorted++;
}
}
if(q.empty())
{
//cout<<"unchosed....."<<endl;
break;
}
while(!q.empty())
{
//cout<<"deleting....."<<endl;
int st = q.front();
q.pop();
for(int i = 0; i <= v[st].size()-1; i++)
{
int u = v[st][i];
deg[u]--;
}
}
}
return ;
}
int main(){
int n;
cin>>n;
for(int i = 0; i <= n-1; i++)
{
int x,m;
cin>>x>>m;
maxx = min(x,maxx);
for(int i = 0; i <= m-1; i++)
{
int a;
cin>>a;
deg[a]++;
v[x].push_back(a);
}
}
topsort();
if(sorted == n)
{
cout<<"YES"<<endl;
return 0;
}
cout<<n-sorted<<endl;
return 0;
}