PAT-1107 Social Clusters (30 分) 并查集模板

1107 Social Clusters (30 分)

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

Ki: hi[1] hi[2] ... hi[*K**i*]

where *K**i* (>0) is the number of hobbies, and *h**i[j] is the index of the j*-th hobby, which is an integer in [1, 1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4

Sample Output:

3
4 3 1

题目大意:有n个人,每个人喜欢k个活动,如果两个人有任意一个活动相同,就称为他们处于同一个社交网络。求这n个人一共形成了多少个社交网络。

第一个喜欢这个活动的称为教主,之后喜欢这个活动的人和教主unite即可.

1.并查集写错了,返回一个等号写成了两个等号.

2.找每组属于谁的时候可能还没有压缩路径,所以应该统计find(i)而不是par[i],,找分为几组的时候才可以用par[i]==i

#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std;

const int maxn=1000+5;
const int inf=0x3f3f3f3f;

int par[maxn];
int Rank[maxn];
int jiaozhu[maxn];
multiset<int>MS;
void init(int n)
{
    for(int i=1;i<=n;i++)///初始化小错误
    {
        par[i]=i;
        Rank[i]=0;
    }
}
int find(int x)
{
    return par[x]==x?x:par[x]=find(par[x]);///并查集的父节点满足par[x]==x,如果不是的话也要把par[x]更新,返回的是find(par[x])同时进行了路径的压缩
}
priority_queue<int>Q;
void unite(int x,int y)
{
    //de(x);
    //de(y);
    x=find(x);
    y=find(y);

    if(x==y)return;
    if(Rank[x]<Rank[y])
    {
        par[x]=y;///军衔小,认y当爹
    }
    else
    {
        par[y]=x;
        if(Rank[x]==Rank[y])Rank[x]++;///本来是兄弟,x却当了爹
    }
}
int root_cnt[maxn];
/**
8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4
*/
int ans[maxn];
int cur=0;
int cmp(int a,int b){return a>b;}
int main()
{
    int n;
    scanf("%d",&n);
    init(n);
    cur=0;
    each(i,1,n)
    {
        int k;
        scanf("%d: ",&k);
        while(k--)
        {
            int temp;
            scanf("%d",&temp);
            if(jiaozhu[temp]==0)
            {
                jiaozhu[temp]=i;
            }
            else
            {
                unite(jiaozhu[temp],i);

            }
        }
    }
    each(i,1,n)
    {
        root_cnt[find(i)]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(root_cnt[i]!=0)
        {
            ans[cur++]=root_cnt[i];
        }
    }
    sort(ans,ans+cur,cmp);
    printf("%d\n",cur);
    each(i,0,cur-1)
    {
        printf("%d",ans[i]);
        printf(i==cur-1?"\n":" ");
    }
}

原文地址:https://www.cnblogs.com/Tony100K/p/11773848.html

时间: 2024-10-27 18:42:01

PAT-1107 Social Clusters (30 分) 并查集模板的相关文章

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to

A1107 Social Clusters (30分)

一.技术总结 这是一道并查集的题目,这里无非就是明白题目的含义,然后将套路用上基本上问题不大. 一个是father数组用于存储父亲结点,isRoot用于存储根结点,如果题目需要,还可以建立一个course数组,用于存储,例如这个题目就是用于判断该该兴趣是否有人涉及. findFather函数,用于查找结点的父亲结点,同时包含压缩路径. Union函数,用于合并. 初始化函数. 二.参考代码 #include<bits/stdc++.h> using namespace std; const i

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre

1107. Social Clusters (30)

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to fi

PAT 1107 Social Clusters

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all

并查集模板

//普通并查集模板 #include<iostream> using namespace std; const int MAX=10004; int fat[MAX];//存放每个节点的根节点 //找x的根节点并且把路径上的每个节点的父节点改成根节点 int find(int x) //while找根节点 { int rt=x; while(fat[rt]!=rt) rt=fat[rt]; int i=x,j; while(i!=rt){ j=fat[i]; fat[i]=rt; i=j; }

并查集 模板

const int MAX=1010;  //元素个数的最大值,根据题目修改int p[MAX];void init(int n) //n为实有元素个数{    for (int i=1; i<=n; i++)  p[i]=i;  }int find(int x) //查找{   if (x==p[x]) return x;    else  return  p[x]=find(p[x]);} void merge(int x,int y) //合并{   int px,py;    px=fi

【并查集模板】并查集模板 luogu-3367

题目描述 简单的并查集模板 输入描述 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1时,将Xi与Yi所在的集合合并 当Zi=2时,输出Xi与Yi是否在同一集合内,是的话输出Y:否则话输出N. 分析 简单的模板,解释留到算法微解读 AC代码 #include <bits/stdc++.h> using namespace std; int n,m; int fa[10000+5]; inline int read(){ int X

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No