题目传送门:https://www.luogu.org/problemnew/show/P2712
题意:
有n个摄像头,每一个摄像头可以拍到一定范围内的东西,每一个东西上有可能有摄像头。现在你需要知道最少有多少个摄像头不会被拍到。注意:当一个摄像头不会被派到时,你可以将它砸掉,它所拍到的地方都不会被它拍到了。
思路:
拓扑排序裸题。
代码:
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
queue<int> f;
int n,len=0,ans;
int last[510],du[510];
bool bz[510];
struct node{int x,y,next;} a[10010];
void ins(int x,int y)
{
a[++len].x=x;a[len].y=y;a[len].next=last[x];last[x]=len;
}
int main()
{
int x,y,t,ma=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&x,&t);
bz[x]=true;
ma=max(ma,x);
for(int j=1;j<=t;j++)
{
scanf("%d",&y);
ins(x,y);
du[y]++;
}
}
for(int i=1;i<=ma;i++)
if(!du[i]&&bz[i]) f.push(i);
ans=n;
while(!f.empty())
{
int x=f.front();
f.pop();
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
du[y]--;
if(!du[y]&&bz[y]) f.push(y);
}
ans--;
}
if(!ans) printf("YES"); else printf("%d",ans);
}