PAT A1107——并查集

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 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:

K?i??: h?i??[1] h?i??[2] ... h?i??[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个人,每个人喜欢若干项活动,如果两个人有任意一项活动相同,那么就称他们处于同一个社交网络(若A和B属于同一个社交网络,B和C属于同一个社交网络,那么A、B、C属于同一个社交网络)。求这N个人总共形成多少个社交网络。参考代码:
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int N = 1010;
 5 int father[N];  //存放父亲结点
 6 int isRoot[N] = {0};    //记录每个结点是否作为某个集合的根结点
 7 int course[N] = {0};
 8 int findFather(int x){  //查找x所在集合的根结点
 9     int a = x;
10     while(x!=father[x]){
11         x = father[x];
12     }
13     //路径压缩
14     while(a != father[a]){
15         int z = a;
16         a = father[a];
17         father[z] = x;
18     }
19     return x;
20 }
21
22 void Union(int a,int b){    //合并a和b所在的集合
23     int faA = findFather(a);
24     int faB = findFather(b);
25     if(faA != faB){
26         father[faA] = faB;
27     }
28 }
29 void init(int n){   //初始化father[i]为i,且flag[i]为false
30     for(int i=1;i<=n;i++){
31         father[i] = i;
32         isRoot[i] = false;
33     }
34 }
35
36 bool cmp(int a,int b){  //将isRoot数组从大到小排序
37     return a > b;
38 }
39
40 int main(){
41 int n,k,h;
42 scanf("%d",&n); //人数
43 init(n);
44 for(int i=1;i<=n;i++){  //对每个人
45     scanf("%d:",&k); //活动个数
46     for(int j=0;j<k;j++){   //对每个活动
47         scanf("%d",&h);    //输入i号人喜欢的活动h
48         if(course[h] == 0){ //如果活动h第一次有人喜欢
49             course[h] = i;  //令i喜欢活动h
50         }
51         Union(i,findFather(course[h])); //合并
52     }
53 }
54
55 for(int i=1;i<=n;i++){
56     isRoot[findFather(i)]++;    //i的跟结点是findFzther(i),人数加1
57 }
58 int ans = 0;    //记录集合数目
59 for(int i=1;i<=n;i++){
60     if(isRoot[i] != 0){
61         ans++;  //只统计isRoot[i]不为0的
62     }
63 }
64 printf("%d\n",ans); //输出集合个数
65 sort(isRoot+1,isRoot+n+1,cmp);
66 for(int i=1;i<=ans;i++){    //依次输出每个集合内的人数
67     printf("%d",isRoot[i]);
68     if(i<ans)printf(" ");
69 }
70 return 0;
71 }

原文地址:https://www.cnblogs.com/mxj961116/p/10586321.html

时间: 2024-10-09 12:56:43

PAT A1107——并查集的相关文章

PAT甲题题解-1114. Family Property (25)-(并查集模板题)

题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h

PAT 1114 Family Property[并查集][难]

1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average

PAT甲级——1118 Birds in Forest (并查集)

此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. Yo

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls mad

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

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

【bzoj3674】 可持久化并查集加强版

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Solution 其实就是用主席树的叶子节点维护并查集的可持久化数组fa[]. 细节 终于认识到了按秩合并的强大,单纯写个路径压缩Re飞,写了路径压缩+按秩合并比单纯的按秩合并每快多少→_→ 代码 // bzoj3674 #include<algorithm> #include<iostream>