1118. Birds in Forest (25)
2017-02-27 19:04
495 查看
并查集
#include<iostream> #include<vector> #include<algorithm> using namespace std; int pre[10005]; vector<bool> visited(10005,false); int findroot(int t) { if (pre[t] != t) pre[t] = findroot(pre[t]); return pre[t]; } void join(int x, int y) { int fx = findroot(x); int fy = findroot(y); if (fx != fy) { pre[max(fx, fy)] = min(fx, fy); } } int main() { int N; cin >> N; for (int t = 0;t < 10005;t++) pre[t] = t; while (N--) { int n; cin >> n; if (n == 0) continue; int x, y; cin >> x; visited[x] = true; while (--n) { cin >> y; join(x, y); visited[y] = true; } } int cnt1 = 0,cnt2=0; for (int t = 0;t < 10005;t++) { if (visited[t]) { cnt2 = max(cnt2, t); if (pre[t] == t) cnt1++; } } cin >> N; cout << cnt1 << " " << cnt2 << endl; while (N--) { int a, b; cin >> a >> b; if (findroot(a) == findroot(b)) cout << "Yes" << endl; else cout << "No" << endl; } }
相关文章推荐
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- 1118. Birds in Forest (25)
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25) 并查集
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- PAT (Advanced Level)1118. Birds in Forest (25)&&1114. Family Property (25)并查集找爸爸
- 1118. Birds in Forest (25)[并查集]
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- 1118. Birds in Forest (25) PAT 甲级
- 1118. Birds in Forest (25)
- 1118. Birds in Forest
- 1118. Birds in Forest
- PAT A1118 birds in forest (25)
- pat甲1118. Birds in Forest(并查集)