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;
}
}
}
#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;
}
}
}
相关文章推荐
- hdu 1272 小希的迷宫(并查集 最小生成树)
- hdu 1272 小希的迷宫(并查集)
- 并查集 HDU 1272 小希的迷宫
- HDU-1272 小希的迷宫 (并查集)
- hdu 1272 小希的迷宫 解题报告
- hdu 1272 小希的迷宫(并查集)
- 【HDU】1272 - 小希的迷宫(并查集)
- HDU 1272 小希的迷宫【并查集判断无向图回路】
- hdu 1272 小希的迷宫(并查集/附爆栈的原因)
- Hdu 1272 小希的迷宫
- HDU 1272 小希的迷宫(并查集判断有无成环)
- (step5.1.6)hdu 1272(小希的迷宫——并查集)
- HDU 1272 小希的迷宫
- HDU 1272 小希的迷宫
- hdu 1272 小希的迷宫 【并查集】
- HDU-1272 小希的迷宫 (并查集、判断图是否为树)
- HDU 1272 小希的迷宫 并查集
- HDU 1272 小希的迷宫
- HDU 1272 - 小希的迷宫
- hdu 1272 小希的迷宫