PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)

简介: PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)

题目链接:点击打开链接

题目大意:一个“社交集群”是指部分兴趣爱好相同的人的集合。你需要找出所有的社交集群。

解题思路:并查集思路,轮到第几个人时,以它的 id 为 root,然后吸收它自己所有的兴趣爱好编号,这样就可以达到有部分兴趣爱好相同的人在一个集群里的效果。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int pre[2009], vis[1009], cnt[2009];
void init()
{
    for(int i=1;i<=2009;i++) pre[i]=i;
}
int findx(int x)
{
    return pre[x]==x ? x : pre[x]=findx(pre[x]);
}
void join(int x,int y)
{
    int fx=findx(x), fy=findx(y);
    if(fx!=fy) pre[fx]=fy;
}
int main()
{
    init();
    int n,k,t,id;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        id=1000+i;
        scanf("%d:",&k);
        while(k--)
        {
            scanf("%d",&t);
            join(t,id);
        }
        vis[i]=t; // 记录其中一个点即可,因为都是连结在一起的
    }
    for(int i=1;i<=n;i++)
    {
        id=findx(vis[i]);
        cnt[id]++;
    }
    sort(cnt,cnt+2009,greater<int>());
    int l;
    for(l=0;;l++) if(cnt[l]==0) break;
    printf("%d\n",l);
    for(int i=0;i<l;i++) printf("%d%c",cnt[i],i==l-1?'\n':' ');
    return 0;
}
目录
相关文章
PAT (Advanced Level) Practice:1~3题
​ ✨欢迎您的订阅✨ PAT乙级专栏(更新完毕): 👉🏻【Basic Level】👈🏻 PAT甲级专栏(更新ing): 👉🏻【Advanced Level】👈🏻 ​
PAT (Advanced Level) Practice:1~3题
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
96 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
112 0
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
124 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
106 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
103 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
119 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
120 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
118 0