48322023-03-31 13:09:33Error42Sportos nyaraláscpp17Accepted 40/40181ms22820 KiB
#include <array>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

void dfs(
    vector<vector<int>> const& graph,
    vector<bool>& done,
    vector<int>& component,
    int const cur,
    int const start
) {
    done[cur] = true;
    component[cur] = start;

    for (int const& neigh : graph[cur]) {
        if (done[neigh])
            continue;

        dfs(graph, done, component, neigh, start);
    }
}

int main() {
    array<int, 2> edge_count;

    int n;
    cin >> n >> edge_count[0] >> edge_count[1];

    vector graph(2, vector(n, vector<int>()));

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < edge_count[i]; j++) {
            int u, v;
            cin >> u >> v;
            u--; v--;

            graph[i][u].push_back(v);
            graph[i][v].push_back(u);
        }
    }

    vector components(2, vector<int>(n));

    for (int i = 0; i < 2; i++) {
        vector<bool> done(n);

        for (int j = 0; j < n; j++)
            if (!done[j])
                dfs(graph[i], done, components[i], j, j);
    }

    map<pair<int, int>, int> cp;

    for (int i = 0; i < n; i++)
        cp[{ components[0][i], components[1][i] }]++;

    for (int i = 0; i < n; i++)
        cout << cp[{ components[0][i], components[1][i] }] - 1 << " ";
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1848 KiB
2Accepted0/0136ms21284 KiB
3Accepted1/13ms2128 KiB
4Accepted1/13ms2328 KiB
5Accepted1/13ms2532 KiB
6Accepted1/13ms2620 KiB
7Accepted1/13ms2752 KiB
8Accepted1/13ms2964 KiB
9Accepted1/13ms3052 KiB
10Accepted1/14ms3608 KiB
11Accepted2/24ms3592 KiB
12Accepted2/24ms3668 KiB
13Accepted2/217ms4576 KiB
14Accepted2/213ms4348 KiB
15Accepted2/252ms12828 KiB
16Accepted2/274ms16172 KiB
17Accepted3/392ms19204 KiB
18Accepted3/393ms19600 KiB
19Accepted2/282ms13192 KiB
20Accepted2/2109ms17144 KiB
21Accepted2/2127ms18032 KiB
22Accepted2/2115ms19556 KiB
23Accepted3/3170ms21276 KiB
24Accepted3/3181ms22820 KiB