1118. Birds in Forest (25)
来源:互联网 发布:python多线程加锁 编辑:程序博客网 时间:2024/04/30 06:12
并查集
#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; }}
0 0
- 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)
- 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
- 1118. Birds in Forest
- 1118. Birds in Forest
- CakePHP命名约定
- Ubuntu16.04配置TensorFlow
- VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事...
- 从NV中读取之前保存的网络信息
- ScrollView中嵌套ViewPager,ViewPager中使用FlowLayout
- 1118. Birds in Forest (25)
- 初来乍到,请多关照
- Poj 3661 Running(DP)
- Linux服务器性能检测常用工具
- 每日170227-29
- 前端之路——张根根篇
- Android下拉/上拉刷新ListView之Android-PullToRefresh
- 数据结构实验之栈六:下一较大值(二)
- c# Blob Mysql