32142023-02-22 17:07:33zsomborSportos nyaraláscpp17Accepted 40/40207ms20048 KiB
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int n, m1, m2, a, b, cnt;
vector <vector <int>> g1(5e4 + 1);
vector <vector <int>> g2(5e4 + 1);
vector <int> k1(5e4 + 1, 0);
vector <int> k2(5e4 + 1, 0);
map <pair <int, int>, int> m;

void dfs1(int x) {
    if (k1[x]) return;
    k1[x] = cnt;
    for (int i : g1[x]) dfs1(i);
}

void dfs2(int x) {
    if (k2[x]) return;
    k2[x] = cnt;
    for (int i : g2[x]) dfs2(i);
}

int main()
{
    cin >> n >> m1 >> m2;
    for (int i = 0; i < m1; i++) {
        cin >> a >> b;
        g1[a].push_back(b);
        g1[b].push_back(a);
    }
    for (int i = 0; i < m2; i++) {
        cin >> a >> b;
        g2[a].push_back(b);
        g2[b].push_back(a);
    }
    cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (k1[i]) continue;
        cnt++;
        dfs1(i);
    }
    cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (k2[i]) continue;
        cnt++;
        dfs2(i);
    }
    for (int i = 1; i <= n; i++) m[{k1[i], k2[i]}]++;
    for (int i = 1; i <= n; i++) cout << m[{k1[i], k2[i]}] - 1 << " ";
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/04ms7316 KiB
2Accepted0/0131ms18860 KiB
3Accepted1/14ms7832 KiB
4Accepted1/14ms8048 KiB
5Accepted1/14ms8260 KiB
6Accepted1/14ms8472 KiB
7Accepted1/14ms8692 KiB
8Accepted1/14ms8644 KiB
9Accepted1/14ms8644 KiB
10Accepted1/17ms8900 KiB
11Accepted2/27ms9144 KiB
12Accepted2/28ms9224 KiB
13Accepted2/219ms10016 KiB
14Accepted2/216ms9988 KiB
15Accepted2/252ms14804 KiB
16Accepted2/271ms17004 KiB
17Accepted3/390ms18816 KiB
18Accepted3/392ms18816 KiB
19Accepted2/282ms14764 KiB
20Accepted2/2107ms16336 KiB
21Accepted2/2126ms17676 KiB
22Accepted2/2111ms18492 KiB
23Accepted3/3151ms18816 KiB
24Accepted3/3207ms20048 KiB