Wide off the ice I

Total time limit: 
1000ms
 
Memory Limit: 
65536kB
description

Pharaoh like iced wide fall.

Initial time, the n-cup wide off the desktop, numbered 1 to n. Pharaoh always wanted one cup to another cup wide fall down, so that he could drink a lot of one-time drop a lot of wide, assuming cup capacity is large enough.

There m operations, each operation comprising two integers x and y.

If the original number x the width down to the original number y of wide drop has been in the same cup, please output "Yes"; otherwise, we will be original numbered y where the cup all the broad drop, down to the original number x where cup, and outputs "No".

Finally, Pharaoh wanted to know what a cup of ice fell wide.

 

Entry
Multiple sets of test data, fewer than 5 groups.
Each set of test data, a first line of two integers n, m (n, m < = 50000). Next m lines of two integers x, y (1 <= x , y <= n).
Export
Each test case, the output of the first m rows "Yes" or "No".
M + 1-line output an integer representing the number of cups have wide colonies.
Second row m + 2 has a plurality of integers, from small to large number of outputs cups.
Sample input
3 2
1 2
2 1
4 2
1 2
4 3
Sample Output
No
Yes
2
1 3 
No
No
2
1 4


#include <bits/stdc++.h>
using namespace std;
string str;
int a[50000+5],cnt;
int root(int x)
{
    if(a[x]==x) return x;
    return root(a[x]);
}
void find(int l,int r)
{
    int ll=root(l),rr=root(r);
    if(ll==rr)
    {
        printf("Yes\n");
        //cout<<"Yes"<<endl;
    }
    else
    {
        a[rr] =ll;
        cnt--;
        //cout<<"No"<<endl;
        printf("No\n");
    }
}
int main()
{
    int m,n,l,r;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        cnt=n;
        for(int i=1;i<=n;i++)
        {
            a[i]=i;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&l,&r);
            //cin>>l>>r;
            find(l,r);
        }
        printf("%d\n",cnt);
        //cout<<cnt<<endl;
        for(int i=1;i<=n;i++)
            if(a[i]==i) printf("%d ",i);//cout<<i<<" ";
        //cout<<endl;
        printf("\n");
    }
    return 0;

}

 

Guess you like

Origin www.cnblogs.com/Shallow-dream/p/11415521.html