您的位置:首页 > 其它

HDU 1272 小希的迷宫

2014-08-01 15:11 288 查看
还是并查集判断环的问题。。。这个题输入弄的很恶心。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;

int root[110000];
int vis[110000];
int x,y;
int find (int a){
while(root[a]!=a){
a = root[a];
}
return a;
}
void combine (int a, int b){
int aa = find(a);
int bb = find(b);
if(aa!=bb){
root[bb] = aa;
}
}
int main(int argc, const char * argv[])
{
while(scanf("%d %d", &x, &y)!=EOF){
if(x==-1 && y==-1) break;
if(x==0 && y==0){
cout << "Yes" << endl;
continue;
}
int result= 0;
int maxn = 0;
for(int i = 0; i < 110000; i++) root[i] = i;
memset(vis, 0, sizeof(vis));
while(x||y){
maxn = (maxn < (x < y ? y : x) ? (x < y ? y : x) : maxn);
vis[x] = 1;
vis[y] = 1;
if(find(x)==find(y)) result = 1;
else combine(x, y);
cin >> x >> y;
}
int count = 0;
for(int i = 1; i <= maxn; i++){
if(vis[i] && root[i]==i) count++;
}
if(result==1) cout << "No" << endl;
else {
if(count!=1) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: