CSU1081 directed graph BFS

Team grouping

Description
Central South University ACM summer training camp is about to begin, the training will name all the Players' N (respectively numbered 1, 2, ..., N) trials by training ranking divided into two groups, former players divided into K group A, the remaining group members divided into B.

But now assistant coach CSGrandeur accidentally lost rankings training trials, but not any before it will determine the group A and group staff of B, then CSGrandeur training staff intend to ask them what each rank, in order to a group of players to determine what.

However, as the players are training ranking as dirt, only vaguely remember some people came in their own back, back to a final total of M message CSGrandeur here, each message can use a tuple (x, y) (x! = y) said, meaning the y-ranked players to rely on their own after more than x players remember.

Now CSGrandeur want to know, according to the M pieces of information, you can determine whether A group of players it? (By default, all information is reflected in the players training with the facts.)

Input
input comprising a plurality of sets of test data.

For each test, the first line contains three positive integer N (2 <= N <= 1000), K (1 <= K <= N), M (1 <= M <= 10000), meaning supra. Next M lines each have two positive integers x, y (1 <= x, y <= N, and x! = Y), describe the M pieces of information, for each pair of x, y, x represents each name I remember the first team players in the rankings after y than their own to rely on.

Output
For each test, can be determined if members of the group A, output "YES" (without the quotes), and otherwise outputs "NO" (without the quotes).

Sample Input

3 1 2
1 2
1 3

3 2 2
1 2
1 3

Sample output

YES
NO

Hint

Idea: do a search for all the adjacency list can determine how many people he can forward, if the point of greater than or equal nm, then that first name m.

#include <bits / STDC ++ H.>
 the using  namespace STD;
 const  int MAXN = 1E5 + . 5 ;
 int First [MAXN], Next [MAXN], Edge [MAXN] [ 2 ];
 int VIS [MAXN];
 int the Add ( int A , int B, int c) 
{ 
    Next [c] = first [a]; // Next the information for pointing to the first occurrence of a; 
    first [a] = c; // tag appears in a current message c 
    edge [C] [ 0 ] = A; 
    Edge [C] [ . 1 ] = B; 
} 
Queue < int > Q;
int bfs(int i)
{

    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop();
    q.push(i);
    int cnt=0;
    vis[i]=1;
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        for(int j=first[t];j!=-1;j=next[j])
        {
            if(vis[edge[j][1]]==0)
            {
                vis[edge[j][1]]=1;
                q.push(edge[j][1]);
                cnt++;
            }
        }
    }
    return cnt;
}
int main()
{
    int n,m,k;
    int xx,yy;
    while(scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        memset(first,-1,sizeof(first));
        memset(next,0,sizeof(next));
        int cnt1=0;
        for(int i=0;i<k;i++)
        {
            scanf("%d %d",&xx,&yy);
            add(xx,yy,i);
        }
        for(int i=1;i<=n;i++)
        {
            if(bfs(i)>=(n-m))
            {
                cnt1++;
            }
        }
        if(cnt1>=m)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/wakaka12345/p/11470631.html