1107. Social Clusters (30)解题报告
来源:互联网 发布:中国省市区数据库 编辑:程序博客网 时间:2024/05/20 01:11
使用并查集和STL中的集合容器。
只要两个集合有一个兴趣相同就要合并。
#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <set>#include <algorithm>#include <vector>using namespace std;struct node {set<int> s;int father, index;};void setUnion(node &n1, node &n2);bool merge(node &n1, node &n2);bool comp(int v1, int v2);int main(void){int N, k, h, i, j;node arr[1000];set<int> s;scanf("%d", &N);for (i = 0; i < N; i++) {arr[i].father = -1;arr[i].index = i;scanf("%d:", &k);for (j = 0; j < k; j++) {scanf("%d", &h);s.insert(h);}arr[i].s.insert(s.begin(), s.end());s.clear();}for (i = 0; i < N - 1; i++) {if (arr[i].father < 0) {for (j = i + 1; j < N; j++) {if (arr[j].father < 0) {if (merge(arr[i], arr[j])) {break;}}}}}vector<int> v;for (i = 0; i < N; i++) {if (arr[i].father < 0) {v.push_back(-(arr[i].father));}}sort(v.begin(), v.end(), comp);printf("%d\n", v.size());if (v.size()) {printf("%d", v[0]);for (i = 1; i < v.size(); i++) {printf(" %d", v[i]);}}putchar('\n');return 0;}bool comp(int v1, int v2) {return v1 > v2;}void setUnion(node &n1, node &n2) {n2.father = n1.father + n2.father;n1.father = n2.index;n2.s.insert(n1.s.begin(), n1.s.end());return;}bool merge(node &n1, node &n2) {set<int>::iterator it1, it2;bool flag = false;it1 = n1.s.begin();it2 = n2.s.begin();while (it1 != n1.s.end() && it2 != n2.s.end()) {if (*it1 > *it2) {it2++;}else if (*it1 < *it2) {it1++;}else {flag = true;break;}}if (flag) {setUnion(n1, n2);}return flag;}
0 0
- 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)
- 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)
- SPFA模版
- Java操作Excel表格
- Android中的文件存储
- 我给媳妇解释设计模式:第一部分
- 笔记:boost中split函数的用法
- 1107. Social Clusters (30)解题报告
- 04-树5 Root of AVL Tree (25分)
- linux 内核移植和根文件系统的制作
- 指尖菜谱App从0到1-精选模块实现(1)
- MFC简易音乐播放器
- 每天对着点电脑坐久了
- SSH框架部署到服务器上js中url中文乱码问题
- RERAN:安卓系统的定时和点击的录制和回放——(5)
- spring boot 配合前端实现跨域