PAT程序设计考题——甲级1107( Social clusters并查集) C++实现
来源:互联网 发布:ubuntu 14.04 源 2016 编辑:程序博客网 时间:2024/06/05 22:48
点击打开pat链接
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<vector>
using namespace std;
#define INF 100000000
#define maxn 100010
int father[1010];
int isroot[1010]={0};
int coarse[1010]={0};
void init(int n)
{
for(int i=0;i<=n;i++)
{
father[i]=i;
}
}
int findfather(int x)
{ int a=x;
while(x!=father[x])
{
x=father[x];
}
while(a!=father[a])
{int z=a;
a=father[a];
father[a]=x;
}
return x;
}
void Union(int x,int y)
{
int m=findfather(x);
int n=findfather(y);
if(m!=n) father[m]=n;
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,k,hobby;
cin>>n;init(n);
for(int i=1;i<=n;i++)
{
scanf("%d:",&k);
for(int j=0;j<k;j++)
{
cin>>hobby;
if(coarse[hobby]==0) coarse[hobby]=i;
Union(i,father[coarse[hobby]]);
}
}//主体的功能已经结束
for (int i=1;i<=n;i++)
{
isroot[findfather(father[i])]++;//同时统计人数
}
int num=0;
for (int i=1;i<=n;i++)
{
if(isroot[i]!=0) num++;
}
cout<<num<<endl;
sort(isroot+1,isroot+n+1,cmp);
for(int i=1;i<=num;i++)
{
cout<<isroot[i];
if(i<=num-1) cout<<" ";
}
return 0;
}
- PAT程序设计考题——甲级1107( Social clusters并查集) C++实现
- PAT - 甲级 - 1107. Social Clusters (30) (并查集)
- 并查集--PAT.A1107.Social Clusters
- PAT程序设计考题——甲级1065( A+B and C (64bit) ) C++实现
- PAT 1107. Social Clusters (30) 并查集
- PAT 1107. Social Clusters (30) 特殊问题+并查集+数量信息并查集
- PAT程序设计考题——甲级1007 (最大连续子序列和) C++实现
- PAT程序设计考题——甲级1045 (最长不下降子序列) C++实现
- PAT程序设计考题——甲级1045 (最长公共子序列) C++实现
- PAT程序设计考题——甲级1040 (最长回文串) C++实现
- PAT程序设计考题——甲级1068 (背包问题) C++实现
- PAT程序设计考题——甲级1042(洗牌机) C++实现
- PAT程序设计考题——甲级1046(循环最短距离) C++实现
- PAT程序设计考题——甲级1001( A+B Format ) C++实现
- PAT程序设计考题——甲级1002(A+B for Polynomials ) C++实现
- PAT程序设计考题——甲级1005( Spell It Right ) C++实现
- PAT程序设计考题——甲级1057( stock 分块查询) C++实现
- PAT程序设计考题——甲级1008( elevator) C++实现
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI
- Vim常用操作整理备份
- 加密方法以及原理笔记
- JavaWeb学习总结(十七)——JSP中的九个内置对象
- 多线程(3)
- PAT程序设计考题——甲级1107( Social clusters并查集) C++实现
- iOS MVVM 详解
- Hbulider创建项目做基于html5的五子棋游戏(编写五子棋简单版二)
- python 对List进行切片
- globs 的匹配规则
- 【并查集+平衡树启发式合并】LibreOJ β Round #2[DP一般看规律]题解
- JavaWeb学习总结(十八)——JSP属性范围
- 2017西安交大ACM小学期数据结构 [又是树状数组、异或]
- Web前端学习(6)_jQuery