답안 #939336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939336 2024-03-06T09:26:47 Z groshi 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
3 ms 8792 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
int ojc[200000];
int wiel[200000];
map<int,int> mapka[100010];
vector<int> nalezy[100010];
int wynik=0;
int find_u(int x)
{
    if(ojc[x]==x)
        return x;
    else return ojc[x]=find_u(ojc[x]);
}
void union_f(int x,int y)
{
    x=find_u(x);
    y=find_u(y);
    if(mapka[x].size()>mapka[y].size())
        swap(x,y);
    ojc[x]=y;
    /*cout<<"srodek\n";
    cout<<wynik<<"\n";
    cout<<x<<": "<<mapka[x].size()<<"\n";
    cout<<y<<": "<<mapka[y].size()<<"\n";*/
    wynik-=mapka[x].size()*wiel[x];
    wynik-=mapka[y].size()*wiel[y];
    wynik-=wiel[x]*(wiel[x]-1);
    wynik-=wiel[y]*(wiel[y]-1);
    wiel[y]+=wiel[x];
    for(auto it=mapka[x].begin();it!=mapka[x].end();it++)
    {
        if(find_u(it->first)!=y)
            mapka[y][it->first]=1;
    }
    for(int i=0;i<nalezy[x].size();i++)
    {
        mapka[y].erase(nalezy[x][i]);
        nalezy[y].push_back(nalezy[x][i]);
    }
    //cout<<"po "<<mapka[y].size()<<"\n";
    //cout<<mapka[y].begin()->first<<"\n";
    wynik+=wiel[y]*mapka[y].size();
    wynik+=wiel[y]*(wiel[y]-1);
    mapka[x].clear();
    //cout<<"po srodku\n";
}
int32_t main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        ojc[i]=i,wiel[i]=1,nalezy[i].push_back(i);
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        if(find_u(x)==find_u(y))
        {
            cout<<wynik<<"\n";
            continue;
        }
        if(mapka[find_u(x)].find(y)!=mapka[find_u(x)].end())
        {
            ///jest juz krawedz od y do x, robi sie petla
            union_f(x,y);
        }
        else{
            if(mapka[find_u(y)].find(x)==mapka[find_u(y)].end())
                wynik+=wiel[find_u(y)];
            mapka[find_u(y)][x]=1;
        }
        cout<<wynik<<"\n";
    }
    return 0;
}

Compilation message

joitter2.cpp: In function 'void union_f(long long int, long long int)':
joitter2.cpp:36:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<nalezy[x].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8792 KB Output isn't correct
2 Halted 0 ms 0 KB -