1107 Social Clusters——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 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[Ki]
where Ki (>0) is the number of hobbies, and hi[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

题目大意:

  1. 给出一个正整数N代表社交网络上的总人数,然后每个人的编号从1~N,接下来的N行,每行想给出一个数字K,接下来给出K个数h[i] ~h[k]代表每个人的爱好的编号
  2. 对于每一个测试数据先输出一共有多少个社交群体,然后按按照非递减序列输出每个社交群体一共有所少人数。ps: 有相同爱好的人在同一个社交群体

大致思路:

  1. 由题目可知,这道题让你对一组数据按照题目要求划分成不同的集合并统计每一个集合的人数,很明显,这道题要求我们用并查集去做
  2. 用一个数组fa[n]来表示每个集合的编号,用一个数组size用来记录每个集合的人数。初始时初始化为1~N,size初始化为1。当要进行合并操作实,size就用当前所在集合的人数加上合并集合的人数。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
int fa[N], sizes[N];
int n;

int find(int x) {
    if (x != fa[x]) fa[x] = find(fa[x]);
    return fa[x];
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i <= n + 1; i++) {
        fa[i] = i;  //先初始化
        sizes[i] = 1;
    }
    vector<vector<int> > v(n + 1);
    for (int i = 1; i <= n; i++) {
        int k;
        scanf("%d:", &k);
        for (int j = 1; j <= k; j++) {
            int x;
            scanf("%d", &x);
            v[i].push_back(x);
        }
    }
    for (int i = 1; i <= n - 1; i++) {
        for (int p = i + 1; p <= n; p++) {
            if (find(p) == find(i)) continue;
            for (int j = 0; j < v[i].size(); j++) {
                if (find(v[p].begin(), v[p].end(), v[i][j]) != v[p].end()) {
                    sizes[find(p)] += sizes[find(i)];
                    fa[find(i)] = find(p);
                    break;
                }
            }
        }
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
        if (find(i) == i) {
            ans.push_back(sizes[i]);
            // cnt++;
        }
    }
    cout << ans.size() << endl;
    sort(ans.begin(), ans.end());
    for (int i = ans.size() - 1; i >= 0; i--) {
        printf("%d", ans[i]);
        if (i != 0)
            cout << " ";
        else
            cout << endl;
    }
    return 0;
}
上一篇:Latest Version Porsche Piwis Tester II V14.75 with CF30 Laptop


下一篇:《人力资源数字化转型的Tech Leadership模型》主题分享