1118. Birds in Forest (25)
2018-03-04 18:09
471 查看
#include <iostream> #include <vector> #include <algorithm> #include <stack> #include <map> using namespace std; int father[10001]; int to[10001]; int find(int x){ while (x != father[x]) { x = father[x]; } return x; } void Union(int a, int b){ a = find(a); b = find(b); if(a < b) father[b] = a; else father[a] = b; } int main(){ int n; cin >> n; map<int, bool> ma; vector<int> v; for (int i = 0; i < 10001; i++) { father[i] = i; } for (int i = 1; i <= n; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int t; cin >> t; if(!ma[t]){ v.push_back(t); ma[t] = true; to[t] = i;//每一只鸟在那棵树上 } Union(i, find(to[t]));//将两棵树联系在一起 } } int cnt = 0; map<int, bool> mb; for (int i = 0; i < v.size(); i++) { if(find(to[v[i]]) && !mb[find(to[v[i]])]){ mb[find(to[v[i]])] = true; cnt++; } } cout << cnt << ' ' << v.size() << endl; int k; cin >> k; for (int i = 0; i < k; i++) { int a, b; cin >> a >> b; if(find(to[a]) == find(to[b])){ cout << "Yes\n"; }else{ cout << "No\n"; } } return 0; }
相关文章推荐
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25) PAT 甲级
- 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)并查集找爸爸
- 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)
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- pat甲1118. Birds in Forest(并查集)
- 1118. Birds in Forest
- 1118. Birds in Forest
- 1118. Birds in Forest