Luo Gu solution to a problem P2721 [camera]

This is the most I've seen blue water problem

Is not that the question you topological sorting board

Topic effect; squirrel smashed the camera does not get caught

A camera can monitor to another may have seen enough to side with the adjacent chain store, I do not know adjacency matrix in the end can not be too safe or adjacency list. It is contemplated that squirrel order not to be caught must not hit the camera surveillance cameras anything else, then that is the point of zero degrees, a very simple topological sorting can think of, because the point is not a topological sorting is from the edge of the beginning go step by step, each step is not to go into the same side of the point (and like dominoes). Topological sorting in the side and not into the camera is not monitored here can be a good link, you can know with a topological sorting.

First find first-degree point 0, and then began to keep looking down from this point, if no other penetration at a certain point to point zero, that is, jj, and squirrels very poor, will certainly the number caught it outputs the remaining cameras. (Each number can be safely destroyed a camera with a counter that is incremented a counter, that time is not on total output minus the camera counter, if the counter is equal to the total number of cameras would output "YES" and then end)

AC Code

#include<cstdio>
#include<stack>

using namespace std;

struct node
{
    int u,v,ne;
}a[100000];
int n,num,num1;
int head[10000];
int into[10000];
int bian[1000];
bool use[100000];

void add(int u,int v)
{
    a[++num].ne = head[u];
    a[num].u = u;
    a[num].v = v;
    head[u] = num;
}

int main()
{
    stack<int>s;
    scanf("%d",&n);
    for(int i = 1;i <= n;++ i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(use[x] == false)
        bian[++ num1] = x;
        while(y --)
        {
            int xx;
            scanf("%d",&xx);
            add(x,xx);
            into[xx] ++;
            if(use[xx] == false)
            bian[++ num1] = xx;
        }
    }
    int qwq = 0;
    for(int i = 1;i <= num1;++ i)
    {
        if(into[bian[i]] == 0)
        {
            s.push(i);qwq ++;
        }
    }
    while(!s.empty())
    {
        int u = s.top();
        s.pop();
        for(int i = head[u];i != 0;i = a[i].ne)
        {
            into[a[i].v] --;
            if(into[a[i].v] == 0)
            {
                s.push(a[i].v);
                qwq ++;
            }
        }
    }
    if(qwq == n)
        printf("YES\n");
    else
        printf("%d\n",n - qwq);
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/acioi/p/11324046.html