这道题坑多,看数据。
0 0
1 1 0 0
1 2 2 1 0 0
1 2 2 3 4 5 5 6 0 0
Yes
Yes
No
No
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define LL long long
#define OJ_DEBUG 0
#define READ_FILE 1
using namespace std;
const int NN_MAX = 100000+10;
const int MM_MAX = 100010;
const int INF = 0x3fffffff;
/**********************************************************/
int p[NN_MAX];
set<int> sets;
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void swap (int& a, int& b){a^=b;b^=a;a^=b;}
int find (int i){return i==p[i]?i:p[i]=find (p[i]);}
bool merge (int i,int j)
{
int x=find (i), y=find (j);
if (x==y&&i!=j)
return false;
p[y]=x;
return true;
}
/**********************************************************/
int main()
{
if (READ_FILE) freopen ("in.txt","r",stdin);
int a,b;
while (1)
{
for (int i=0;i<NN_MAX;i++) p[i]=i;
bool yn=true;
sets.clear ();
while (1)
{
scanf ("%d %d",&a,&b);
if (a==-1&&b==-1) return 0;
if (a==0&&b==0)
break;
sets.insert (a);sets.insert (b);
if(!merge (a,b))
yn=false;
}
if (!yn) {printf ("No\n");continue;}
int x,cnt=0;
for (set<int>::iterator it=sets.begin ();it!=sets.end ();it++){
x=(*it);
if (x==find (x))
cnt++;
}
if (cnt>=2) printf ("No\n");
else printf ("Yes\n");
}
return 0;
}