标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38890 Accepted Submission(s): 11933
1 #include <cstdio> 2 #include <algorithm> 3 #include <queue> 4 using namespace std; 5 const int maxn = 100050; 6 7 int f[maxn]; 8 bool vis[maxn]; 9 typedef pair<int, int> P; 10 vector<P> v; 11 12 int sf(int x){ return x == f[x] ? x : f[x] = sf(f[x]); } 13 int main() 14 { 15 int a, b, n = 0; 16 while (scanf("%d%d", &a, &b) == 2 && a != -1 && b != -1) 17 { 18 if (a&&b) 19 { 20 v.push_back(P(a, b)); 21 n = max(a, n); 22 n = max(b, n); 23 } 24 else 25 { 26 bool flag = true; 27 int cnt = 0;//连通 28 if (n == 0) { printf("Yes\n"); continue; } 29 memset(vis, 0, sizeof(vis)); 30 for (int i = 1; i <= n; i++) f[i] = i; 31 for (vector<P>::iterator it = v.begin(); it != v.end(); it++) 32 { 33 if (!vis[it->first]){ cnt++; vis[it->first] = true; } 34 if (!vis[it->second]){ cnt++; vis[it->second] = true; } 35 int q = sf(it->first); int w = sf(it->second); 36 if (q != w){ cnt--; f[q] = w; } 37 else{ flag = false; break; } 38 } 39 if (flag && cnt==1) printf("Yes\n"); 40 else printf("No\n"); 41 42 //清空和重置 43 v.clear(); 44 n = 0; 45 } 46 } 47 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5185064.html