1107. Social Clusters (30) 并查集
来源:互联网 发布:java api手机安卓版 编辑:程序博客网 时间:2024/06/07 00:23
题目地址
用vector<int>
数组来记录每次输入有这个爱好的人的ID,
用map<int,int>
来统计有几个集合,注意最后统计时还要进行路径压缩。
//// Created by aleafall on 16-10-3.//#include<iostream>#include<cstdio>#include<algorithm>#include<map>#include<vector>using namespace std;const int maxn = 1001;int father[maxn];vector<int> hobby[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[faB] = faA;}int main() { int n, k, h; map<int, int> mp; for (int i = 0; i < maxn; i++) father[i] = i; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d: ", &k); for (int j = 0; j < k; j++) { scanf("%d", &h); hobby[h].push_back(i); } } for (int i = 1; i < maxn; i++) { if (hobby[i].size() > 1) { for (int j = 1; j < hobby[i].size(); j++) Union(hobby[i][0], hobby[i][j]); } } for (int i = 1; i <= n; i++) { //注意路径压缩 int F = findFather(i); if (mp.find(F) != mp.end()) mp[F]++; else mp[F] = 1; } int cnt = 0, ans[maxn]; for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { ans[cnt++] = it->second; } cout << cnt << endl; sort(ans, ans + cnt); for (int i = 0; i < cnt; i++) { cout << ans[cnt - i - 1]; if (i < cnt - 1) cout << ' '; else cout << endl; } return 0;}
0 0
- 1107. Social Clusters (30) 并查集
- 1107. Social Clusters (30)[并查集]
- 1107. Social Clusters (30) 并查集
- 1107. Social Clusters (30)并查集
- 1107. Social Clusters (并查集 )
- PAT - 甲级 - 1107. Social Clusters (30) (并查集)
- PAT 1107. Social Clusters (30) 并查集
- PAT 1107. Social Clusters (30) 特殊问题+并查集+数量信息并查集
- 并查集--PAT.A1107.Social Clusters
- PAT (Advanced Level) Practise 1107. Social Clusters (30) 并查集
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- RecyclerView嵌套后刷新View自动滑动解决
- s5pv210时钟系统详解
- Qt中定时器Timer使用
- Kernels in Machine Learning and SVM
- 设计模式实现——代理模式
- 1107. Social Clusters (30) 并查集
- Divisors
- OpenCV 系列---交互(二)鼠标
- Linux下的文件操作命令(五、文件内容统计命令)
- 前端面试的思路
- 【SSM 5】Mybatis分页插件的使用
- react开发思维方式入门
- hash
- C++智能指针简单剖析