hdu1272 小希的迷宮

1.輸入u,v 如果 已經存在find(u) == find(v) ,那么就說明u , v 已經存在一條路徑，是No的一種情況

2.對於連通圖的所有頂點，只有一個頂點的par[i] = i,所有當多個頂點的par[i] = i 就說明這個圖存在多個連通圖

3.當第一個數都是0,0時，輸出Yes

```/***********************************************
* Author: fisty
* Created Time: 2015/2/26 22:45:51
* File Name   : M.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 100100
int u, v;
int ok;
int par[MAX_N];
int vis[MAX_N];
void init(){
for(int i = 0;i <= MAX_N; i++){
par[i] = i;
vis[i] = 0;
}
}
int find(int x){
if(x == par[x]) return x;
return par[x] = find(par[x]);
}
bool unio(int x, int y){
x = find(x);
y = find(y);
if(x != y){
par[x] = y;
return true;
}else
return false;
}
int main() {
//freopen("in.cpp", "r", stdin);
cin.tie(0);
ios::sync_with_stdio(false);
while(cin >> u >> v && (u != -1 && v != -1)){
if(!u && !v){
cout << "Yes" << endl;
continue;
}
init();
ok = 1;
int _max = 0, _min = INF;
while(u || v){
_max = max(max(_max,u), v);
_min = min(min(_min,u), v);
vis[u] = 1; vis[v] = 1;
if(unio(u, v) == false){
ok = 0;
}
cin >> u >> v;
}
if(!ok){
cout << "No" << endl;
}else{
int cnt = 0;
for(int i = _min;i <= _max; i++){
if(find(i) == i && vis[i]){
cnt++;
}
}
if(cnt == 1){
cout << "Yes" << endl;
}else{
//Debug(cnt);
cout << "No" << endl;
}
}
}
return 0;
}
```