1118. Birds in Forest (25) 并查集
来源:互联网 发布:淘宝在线客服工作时间 编辑:程序博客网 时间:2024/06/05 07:41
题目地址
和1107,1114题类似。
//// Created by aleafall on 16-9-28.//#include <iostream>#include <set>using namespace std;const int maxn = 10005;int father[maxn];int findFather(int x) { if (father[x] == x) return x; else { int F = findFather(father[x]); father[x] = F; return F; }}void Union(int a, int b) { int faA = findFather(a); int faB = findFather(b); if (faA != faB) father[faA] = faB;}int main() { for (int i = 0; i < maxn; ++i) { father[i] = i; } int n, k, index, b = 0; int q, b1, b2; set<int> st; cin >> n; for (int i = 0; i < n; ++i) { cin >> k; int temp = 0; for (int j = 0; j < k; ++j) { cin >> index; if (index > b) b = index; if (j == 0) temp = index; else Union(temp, index); } } for (int i = 1; i <= b; ++i) { st.insert(findFather(i)); } cout << st.size() << " " << b << endl; cin >> q; for (int i = 0; i < q; ++i) { cin >> b1 >> b2; if (father[b1] == father[b2]) cout << "Yes\n"; else cout << "No\n"; } return 0;}
0 0
- 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) <并查集+set>
- PAT 1118. Birds in Forest (25) 并查集
- PAT--1118. Birds in Forest(并查集)
- pat甲1118. Birds in Forest(并查集)
- PAT A1118. Birds in Forest (25)(并查集)
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- PAT1118. Birds in Forest (并查集)
- 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)
- 栈的应用案例2:中缀表达式转后缀表达式
- 【C++研发面试笔记】18. 常用算法-查找算法
- 51的串口功能函数
- 期望——计算机里的英语,第一语言
- 洛谷10月月赛Round.1 A.丝绸之路
- 1118. Birds in Forest (25) 并查集
- Well Designed Sites
- Android WindowManager
- JavaScript数据的基本类型
- C/C++中的作用域种类
- Linux shell 常用命令
- 1119. Pre- and Post-order Traversals (30) 二叉树
- JDBC--数据库管理系统
- sqoop的简单实用