13902022-08-30 14:40:20gtgSportos nyaraláscpp17Runtime error 4/4082ms127284 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> p;
vector<int> r;
vector<vector<int> > g, group;
vector<bool> vis;
int findl(int n)
{
    if(p[n] == 0) return n;
    return p[n] = findl(p[n]);
}
void unio(int a, int b)
{
    a = findl(a);
    b = findl(b);
    if(r[a] < r[b]) swap(a, b);
    p[b] = a;
    if(r[a] == r[b]) r[a]++;
}
void dfs(int n)
{
    vis[n] = 1;
    group[group.size() - 1].push_back(n);
    for(auto x : g[n])
    {
        if(!vis[x])
        {
            dfs(x);
        }
    }
}
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    p.resize(n + 1);
    r.resize(n + 1, 1);
    g.resize(n + 1);
    vis.resize(n + 1);
    int x, y;
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        unio(x, y);
    }
    for (int i = 0; i < k; i++)
    {
        cin >> x >> y;
        if(findl(x) == findl(y))
        {
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }
    group.resize(n + 1);
    for(int i = 1; i <= n; i++)
    {
        if(!vis[i])
        {
            group.push_back({});
            dfs(i);
        }
    }
    vector<int> ans(n + 1);
    for(int i = 0; i < group.size(); i++)
    {
        for (auto j : group[i])
        {
            ans[j] = group[i].size() - 1;
        }
    }
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base4/40
1Accepted0/03ms1808 KiB
2Runtime error0/082ms126136 KiB
3Accepted1/12ms2380 KiB
4Accepted1/12ms2584 KiB
5Accepted1/12ms2788 KiB
6Accepted1/12ms2976 KiB
7Runtime error0/167ms127048 KiB
8Wrong answer0/12ms3104 KiB
9Runtime error0/167ms127284 KiB
10Runtime error0/157ms127284 KiB
11Runtime error0/257ms127284 KiB
12Runtime error0/267ms127280 KiB
13Runtime error0/257ms127016 KiB
14Runtime error0/257ms127020 KiB
15Runtime error0/276ms126788 KiB
16Runtime error0/281ms127016 KiB
17Wrong answer0/359ms15424 KiB
18Runtime error0/375ms126492 KiB
19Runtime error0/276ms126272 KiB
20Runtime error0/282ms126144 KiB
21Runtime error0/271ms125964 KiB
22Runtime error0/278ms125960 KiB
23Runtime error0/371ms125964 KiB
24Runtime error0/382ms125960 KiB