PAT_A1107#Social Clusters

Source:

PAT A1107 Social Clusters (30 分)

Description:

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 (≤), 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?? (>) is the number of hobbies, and [ 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

Keys:

Code:

 1 /*
 2 time: 2019-06-23 14:07:12
 3 problem: PAT_A1107#Social Clusters
 4 AC: 34:25
 5
 6 题目大意:
 7 把一群具有相同爱好的人归为一个社交圈,找出所有的社交圈
 8 输入:
 9 第一行给出,总人数N<=1e3,编号从1~N
10 接下来N行,给出第i个人的,爱好总数K,各个爱好
11 输出:
12 第一行给出,社交圈总数
13 第二行给出,各个社交圈的人数,从多到少
14
15 基本思路:
16 基于兴趣做并查集操作,
17 输入每个人的兴趣,首个兴趣的Hash值+1,标记人数
18 统计父结点个数及其孩子的哈希值即可
19 */
20 #include<cstdio>
21 #include<set>
22 #include<algorithm>
23 using namespace std;
24 const int M=1e3+10;
25 int fa[M],man[M]={0},ans[M]={0};
26
27 int Father(int v)
28 {
29     int x=v,s;
30     while(fa[v] != v)
31         v = fa[v];
32     while(fa[x] != x){
33         s = fa[x];
34         fa[x] = v;
35         x = s;
36     }
37     return v;
38 }
39
40 void Union(int v1, int v2)
41 {
42     int f1 = Father(v1);
43     int f2 = Father(v2);
44     fa[f2] = f1;
45     Father(v2);
46 }
47
48 int main()
49 {
50 #ifdef ONLINE_JUDGE
51 #else
52     freopen("Test.txt", "r", stdin);
53 #endif // ONLINE_JUDGE
54
55     for(int i=0; i<M; i++)
56         fa[i]=i;
57
58     int n,m,h1,h2;
59     set<int> hobby,clster;
60     scanf("%d", &n);
61     for(int i=0; i<n; i++)
62     {
63         scanf("%d:%d", &m,&h1);
64         man[h1]++;
65         hobby.insert(h1);
66         for(int j=1; j<m; j++)
67         {
68             scanf("%d", &h2);
69             hobby.insert(h2);
70             Union(h1,h2);
71             h1=h2;
72         }
73     }
74     for(auto it=hobby.begin(); it!=hobby.end(); it++){
75         ans[Father(*it)] += man[*it];
76         clster.insert(Father(*it));
77     }
78     printf("%d\n", clster.size());
79     sort(ans, ans+M, greater<int>() );
80     for(int i=0; i<clster.size(); i++)
81         printf("%d%c", ans[i], i+1==clster.size()?‘\n‘:‘ ‘);
82
83     return 0;
84 }

原文地址:https://www.cnblogs.com/blue-lin/p/11072913.html

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

PAT_A1107#Social Clusters的相关文章

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. Y

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

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)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int N = 1010; int father[N]; int isRoot[N] = {0}; int course[N] = {0}; int findFather(int x){ int a = x; while(x != father[

A1107 Social Clusters (30分)

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

pat甲级1107

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. Y

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

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 suppo