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

 

题意:

  根据每个人的爱好,将不同的人划分为不同的社交圈。一个人可能有多种爱好,因此一个人可以把几种不同的圈子归并到一个大的圈子里。

思路:

  将爱好当做节点,一个人将不同的爱好归并到一个子集中,有相同爱好的人,也会把此人的其他爱好归并到这个子集中。我们可以让祖先结点的序号最小,一次来保证祖先结点只有一个。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int fa[1005];
 6 
 7 int findFather(int x) {
 8     int a = x;
 9     while (x != fa[x]) x = fa[x];
10     while (a != fa[a]) {
11         int z = a;
12         a = fa[a];
13         fa[z] = x;
14     }
15     return x;
16 }
17 
18 void Union(int x, int y) {
19     int faX = findFather(x);
20     int faY = findFather(y);
21     if (faX > faY)
22         fa[faX] = faY;
23     else
24         fa[faY] = faX;
25 }
26 
27 int main() {
28     int n, k;
29     cin >> n;
30     getchar();
31     int num;
32     vector<int> people;
33     for (int i = 0; i < 1005; ++i) fa[i] = i;
34     for (int i = 0; i < n; ++i) {
35         string temp, dummy;
36         getline(cin, temp);
37         int pos = temp.find(':');
38         int len = stoi(temp.substr(0, pos));
39         dummy = temp.substr(pos + 2);
40         vector<int> hobbies;
41         string str = "";
42         for (int j = 0; j <= dummy.size(); ++j) {
43             if (dummy[j] == ' ' || dummy[j] == '\0') {
44                 hobbies.push_back(stoi(str));
45                 str = "";
46             } else {
47                 str += dummy[j];
48             }
49         }
50         for (int j = 1; j < hobbies.size(); ++j) {
51             Union(hobbies[0], hobbies[j]);
52         }
53         people.push_back(hobbies[0]);
54     }
55     map<int, int> mp;
56     for (int i = 0; i < n; ++i) {
57         int r = findFather(people[i]);
58         mp[r]++;
59     }
60     vector<int> ans;
61     for (auto it : mp) ans.push_back(it.second);
62     sort(ans.begin(), ans.end(), greater<int>());
63     cout << ans.size() << endl;
64     cout << ans[0];
65     for (int i = 1; i < ans.size(); ++i) cout << " " << ans[i];
66     cout << endl;
67     return 0;
68 }

 

posted @ 2020-04-27 21:17  Veritas_des_Liberty  阅读(241)  评论(0编辑  收藏  举报