14142022-09-05 21:52:41ZsofiaKeresztelySportos nyaraláscpp14Accepted 40/40179ms21536 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back

void dfs(int v, const vector<vector<int> > &g, vector<int> &c, int comp){
    c[v] = comp;
    for (int x : g[v]){
        if (!c[x]){
            dfs(x, g, c, comp);
        }
    }
}

int main()
{
    int n, m, k, a, b;
    cin >> n >> m >> k;
    vector<vector<int> > ga(n+1);
    vector<vector<int> > gb(n+1);
    while (m--){
        cin >> a >> b;
        ga[a].pb(b);
        ga[b].pb(a);
    }
    while (k--){
        cin >> a >> b;
        gb[a].pb(b);
        gb[b].pb(a);
    }
    vector<int> ca(n+1, 0);
    int cnt = 1;
    for (int i=1; i<n+1; i++){
        if (!ca[i]){
            dfs(i, ga, ca, cnt);
            cnt++;
        }
    }
    vector<int> cb(n+1, 0);
    cnt = 1;
    for (int i=1; i<n+1; i++){
        if (!cb[i]){
            dfs(i, gb, cb, cnt);
            cnt++;
        }
    }
    map<pair<int, int>, int> ma;
    for (int i=1; i<n+1; i++){
        ma[{ca[i], cb[i]}]++;
    }
    for (int i=1; i<n+1; i++){
        cout << ma[{ca[i], cb[i]}] - 1 << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1996 KiB
2Accepted0/0136ms20520 KiB
3Accepted1/12ms2348 KiB
4Accepted1/12ms2548 KiB
5Accepted1/12ms2632 KiB
6Accepted1/12ms2740 KiB
7Accepted1/12ms2744 KiB
8Accepted1/12ms2764 KiB
9Accepted1/12ms2764 KiB
10Accepted1/14ms2836 KiB
11Accepted2/24ms3336 KiB
12Accepted2/24ms3240 KiB
13Accepted2/216ms3860 KiB
14Accepted2/213ms3740 KiB
15Accepted2/250ms12116 KiB
16Accepted2/268ms15236 KiB
17Accepted3/385ms18128 KiB
18Accepted3/389ms18356 KiB
19Accepted2/279ms11724 KiB
20Accepted2/2105ms15492 KiB
21Accepted2/2123ms16804 KiB
22Accepted2/2109ms18544 KiB
23Accepted3/3150ms19900 KiB
24Accepted3/3179ms21536 KiB