L3-003 社交集群

#include <bits/stdc++.h>

using namespace std;

const int N = 1010;
int f[N];
bool havef[N];
int Size[N];

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    for (int i = 1; i <= 1000; i++) f[i] = i, Size[i] = 0;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string k;
        cin >> k;
        k.pop_back();
        int kk = stoi(k);
        vector<int> son(kk, 0);
        for (int j = 0; j < kk; j++) {
            cin >> son[j];
        }
        int fa = find(son[0]);
        Size[fa] += 1;
        for (int j = 1; j < kk; j++) {
            int fj = find(son[j]);
            if (fa == fj) continue;
            Size[fa] += Size[fj];
            f[fj] = fa;
        }
    }    

    vector<int> res;
    for (int i = 1; i <= 1000; i++) {
        if (f[i] == i && Size[i] != 0) {
            res.push_back(Size[i]);
        }
    }

    sort(res.begin(), res.end());
    
    cout << res.size() << "\n";
    for (int i = res.size() - 1; i >= 0; i--) {
        cout << res[i] << " "[i == 0];
    }

    return 0;
}
posted @ 2022-04-19 08:31  Xxaj5  阅读(22)  评论(0编辑  收藏  举报