基本上按拓扑排序原模版来的
#include <bits/stdc++.h>
using namespace std;
const int N=510;
int e[N],ne[N],h[N],idx;
int d[N];
bool st[N];//记录当前位置有无摄像头
int q[N],hh,tt=-1;//数组模拟队列
int n;
int maxx;//记录下出现过的最大位置
int ans;//看最后有多少个摄像头没删
void add(int a,int b)//链表存储
{
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void topsort()//拓扑排序原模版
{
for (int i=1;i<=maxx;i++)
{
if (d[i]==0)
{
q[++tt]=i;
}
}
while (hh<=tt)
{
int a=q[hh++];
for (int i=h[a];i!=-1;i=ne[i])
{
int j=e[i];
d[j]--;
if (d[j]==0) q[++tt]=j;
}
}
}
int main()
{
scanf("%d",&n);
memset (h,-1,sizeof h);
int x,m;
for (int i=1;i<=n;i++)
{
scanf("%d%d",&x,&m);
st[x]=true;//当前位置有摄像头
maxx=max(maxx,x);//更新出现的最大位置
int y;
while (m--)
{
scanf("%d",&y);
maxx=max(maxx,y);
add(x,y);
d[y]++;
}
}
topsort();
for (int i=1;i<=maxx;i++)
{
if (d[i]!=0&&st[i]) ans++;//如果当前位置有摄像头并且入度还不是0(即没有被删除)答案就加一
}
if (ans==0) puts("YES");//如果全删了就输出YES
else printf("%d",ans);
return 0;
}