Fork me on GitHub

摄像头

https://www.luogu.org/problem/show?pid=2712
这里写图片描述
拓扑排序嘛。每次找入度为零即没有被观测的摄像头砸掉,再把它所观测的摄像头的入度-1.
不过此题有一个特别坑的地方,就是摄像头的编号并不是1~n。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cmath> 
using namespace std;
int n,maxn;
int ru[1009],to[1009][1009]; 
bool f[1009];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x,m;
        scanf("%d%d",&x,&m);
        for(int i=1;i<=m;i++) 
        {
            int p;scanf("%d",&p);
            to[x][++to[x][0]]=p;
            ru[p]++;
        }
        f[x]=1;
        maxn=max(maxn,x);
    }
    int num=n;
    while(1)
    {
        int p=0;
        for(int i=1;i<=maxn;i++)
          if(f[i]==1&&ru[i]==0) {p=i;f[p]=0;break;}
        if(!p) break;
        for(int i=1;i<=to[p][0];i++)
          ru[to[p][i]]--;
        num--;
    }
    if(num) printf("%d",num);
    else printf("YES");
    return 0;   
}
posted @ 2017-09-24 17:48  primes  阅读(131)  评论(0)    收藏  举报