hdu1272
路径压缩会爆栈吗,我试了下我画斜线的,可以通过。
/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
typedef long long LL;
using namespace std;
int u, v;
int f[100005];
// int Find(int x){
// return x == f[x] ? x : Find(f[x]);
// }
int Find(int x){
while(x != f[x]){
x = f[x];
}
return f[x];
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(scanf("%d %d", &u, &v) == 2){
if(u == -1 && v == -1) break;
for (int i = 1; i <= 100000; i++){
f[i] = i;
}
if(!u && !v){ //坑
printf("Yes\n");
continue;
}
set<int>st;
st.insert(u), st.insert(v);
int x = Find(u), y = Find(v);
f[x] = y;
int flag = 0;
int cnt = 1;
while(scanf("%d %d", &u, &v) == 2){
if(u == 0 && v == 0) break;
x = Find(u), y = Find(v);
st.insert(u), st.insert(v);
cnt++;
if(x != y){
f[x] = y;
}else{
flag = 1;
}
}
(flag || st.size() != cnt + 1) ? printf("No\n") : printf("Yes\n");
}
return 0;
}
/**/