14192022-09-09 12:14:02kicsiboglarSportos nyaraláscpp11Accepted 40/4098ms24088 KiB
#include <iostream>
#include <vector>
#include <deque>
#include <map>

#define ll long long 

using namespace std;

ll n,i,a,mini=9999999,b,k;
struct element
{
    bool seen=false;
    vector <ll> bike,kayak;
    ll compbike,compk;
};
vector <element> x;
vector <ll> res;

void BFS (ll start,bool id)
{
    deque<ll> v;
    v.push_back(start);
    x[start].seen=true;
    ll curr;
    while (!v.empty())
    {
        curr=v[0];
        v.pop_front();
        if (id)
        {
            for (auto &e:x[curr].bike)
            {
                if (!x[e].seen)
                {
                    x[e].seen=true;
                    x[e].compbike=x[curr].compbike;
                    v.push_back(e);

                }
            }
        }
        else
        {
            for (auto &e:x[curr].kayak)
            {
                if (!x[e].seen)
                {
                    x[e].seen=true;
                    x[e].compk=x[curr].compk;
                    /*if (x[e].compbike==x[curr].compbike&&x[curr].compbike!=0)
                    {
                        res[e]++;
                        res[curr]++;
                    }*/
                    v.push_back(e);

                }
            }
        }
    }
}



int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n>>b>>k;
    x.resize(n+1);
    ll p,q;
    res.resize(n+1);
    for (i=1;i<=b;++i)
    {
        cin>>p>>q;
        x[p].bike.push_back(q);
        x[q].bike.push_back(p);
    }
    for (i=1;i<=k;++i)
    {
        cin>>p>>q;
        x[p].kayak.push_back(q);
        x[q].kayak.push_back(p);
    }

    ll nr=0;
    for (i=1;i<=n;++i)
    {
        if (!x[i].seen)
        {
            nr++;
            x[i].compbike=nr;
            BFS(i,true);
        }
    }
    for (i=1;i<=n;++i) x[i].seen=false;
    nr=0;

    for (i=1;i<=n;++i)
    {
        if(!x[i].seen)
        {
            nr++;
            x[i].compk=nr;
            BFS(i,false);
        }
    }
    map <pair<ll,ll> ,ll >components;
    pair<ll,ll> id;
    for (i=1;i<=n;++i) 
    {
        id.first=x[i].compbike;
        id.second=x[i].compk;
        components[id]++;
    }
   /* ll j;
    for (i=1;i<n;++i)
    {
        for (j=i+1;j<=n;++j)
        {
            if (x[i].compbike!=0&&x[i].compk!=0)
            {
                if (x[i].compbike==x[j].compbike&&x[i].compk==x[j].compk)
                {
                    res[i]++;
                    res[j]++;
                }
            }
        }
    }*/

    for (i=1;i<=n;++i) 
    {
        id.first=x[i].compbike;
        id.second=x[i].compk;
        cout<<components[id]-1<<" ";
    }
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1896 KiB
2Accepted0/086ms21484 KiB
3Accepted1/12ms2328 KiB
4Accepted1/12ms2432 KiB
5Accepted1/12ms2624 KiB
6Accepted1/12ms2824 KiB
7Accepted1/12ms2908 KiB
8Accepted1/12ms3008 KiB
9Accepted1/12ms2972 KiB
10Accepted1/13ms3356 KiB
11Accepted2/23ms3296 KiB
12Accepted2/24ms3520 KiB
13Accepted2/28ms4780 KiB
14Accepted2/27ms4404 KiB
15Accepted2/237ms13440 KiB
16Accepted2/250ms17076 KiB
17Accepted3/364ms20636 KiB
18Accepted3/364ms20488 KiB
19Accepted2/245ms13856 KiB
20Accepted2/259ms17804 KiB
21Accepted2/275ms20624 KiB
22Accepted2/271ms21128 KiB
23Accepted3/386ms21860 KiB
24Accepted3/398ms24088 KiB