PAT1107 甲级
代码如下
#include&nbsp;<iostream>
#include&nbsp;<algorithm>
using&nbsp;namespace&nbsp;std;
int&nbsp;father[1001];
int&nbsp;hobby[1001]={0};
int&nbsp;cnt[1001]={0};
bool&nbsp;compare(int&nbsp;a,int&nbsp;b)
{
return&nbsp;a>b;
}
int&nbsp;findfather(int&nbsp;x)
{
int&nbsp;v;
if(x==father[x]){return&nbsp;x;}
else{
v=findfather(father[x]);
father[x]=v;
return&nbsp;v;
}
}
void&nbsp;unionset(int&nbsp;a,int&nbsp;b)
{
int&nbsp;fathera=findfather(a);
int&nbsp;fatherb=findfather(b);
if(fathera&nbsp;!=&nbsp;fatherb)
{
father[fatherb]=fathera;
}
}
int&nbsp;main()
{
int&nbsp;numpeople;
int&nbsp;eachnum;
for(int&nbsp;p=0;p<1001;p++)
{
father[p]=p;
}
int&nbsp;index;
scanf("%d",&numpeople);
for(int&nbsp;i=1;i<=numpeople;i++)
{
scanf("%d:",&eachnum);
for(int&nbsp;j=0;j<eachnum;j++)
{
cin>>index;
if(hobby[index]==0)
{
hobby[index]=i;
}
unionset(i,hobby[index]);
}
}
for(int&nbsp;i=1;i<=numpeople;i++)
{
cnt[findfather(i)]++;
}
int&nbsp;count=0;
for(int&nbsp;i=0;i<1001;i++)
{
if(cnt[i]!=0)
{
count++;
}
}
sort(cnt,cnt+1001,compare);
cout<<count<<endl;
for(int&nbsp;q=0;q<count-1;q++)
{
cout<<cnt[q]<<"&nbsp;";
}
cout<<cnt[count-1];
return&nbsp;0;
}
全部评论

相关推荐

本神尊:看来是没招到小红薯上的人
点赞 评论 收藏
分享
06-25 16:00
武汉大学 Java
工科研究生底薪工资就开3k啊??
机械打工仔:写文章提成的岗位工资低,你怪工科?
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-26 14:50
人力小鱼姐:有后面墨迹那两句的时间问题早回答完了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务