Description
Input
整个文件以两个-1结尾。
Output
Sample Input
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Sample Output
Yes Yes No
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int max_num=100005;
typedef struct
{
int num,root,in; //,,入du
}Node;
Node node[max_num];
void Init()
{
for(int i=0;i<max_num;i++)
{
node[i].num=0;
node[i].root=i;
node[i].in=0;
}
}
int Find_root(int x)
{
if(node[x].root!=x)
return node[x].root=Find_root(node[x].root);
return node[x].root;
}
void join(int x,int y)
{
int fx=Find_root(x),fy=Find_root(y);
if(fx!=fy)
node[fy].root=fx;
}
int main()
{
int a,b;
bool flag=true;
Init();
while(scanf("%d%d",&a,&b)!=EOF&&a!=-1&&b!=-1)
{
if(!flag&&a!=0&&b!=0)continue;
if(a==0&&b==0)
{
int root_num=0;
for(int i=1;i<max_num;i++)
{
if(node[i].num&&Find_root(i)==i)
root_num++;
if(node[i].in>1)
{
flag=false;
break;
}
}
if(root_num>1)
flag=false;
if(flag)
printf("Yes\n");
else
printf("No\n");
flag=true;
Init();
continue;
}
if((a!=b&&Find_root(a)==Find_root(b))||a==b)
flag=false;
else
{
node[a].num=1;
node[b].num=1;
node[b].in++;
join(a,b);
}
}
return 0;
}