답안 #605213

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
605213 2022-07-25T14:05:56 Z DJeniUp 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
9 ms 19096 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll>pairll;
typedef pair<ll,pairll>pairlll;
typedef unsigned long long ull;
typedef long double ld;

#define fr first
#define sc second
#define pb push_back
#define INF 1000000007
#define N 200007
#define endl '\n'

ll n,M,f[100007],res,sz[100007],a[100007];

set<ll>s[100007],p[100007],d[100007];
set<ll>k;

map<ll,ll>m[100007];

ll P(ll x){
    if(f[x]!=x)f[x]=P(f[f[x]]);
    return f[x];
}

void A(ll x,ll y){
    if(p[x].size()<p[y].size()){
        swap(x,y);
    }
    //cout<<x<<" "<<y<<" "<<a[x]<<" "<<a[y]<<" "<<sz[x]<<" "<<sz[y]<<endl;
    res-=a[x]*sz[x];
    res-=a[y]*sz[y];
    res-=sz[x]*(sz[x]-1);
    res-=sz[y]*(sz[y]-1);
    sz[x]+=sz[y];
    res+=sz[x]*(sz[x]-1);
    k.clear();
    for(auto it:s[x]){
        if(P(it)!=y)k.insert(P(it));
    }
    swap(k,s[x]);
    for(auto it:s[y]){
        if(P(it)!=x){
            s[x].insert(P(it));
        }
    }
    k.clear();
    a[x]=0;
    for(auto it:p[x]){
        if(P(it)!=x && P(it)!=y){
            a[x]++;
            k.insert(it);
        }
    }
    swap(p[x],k);
    for(auto it:p[y]){
        if(p[x].count(it)==0 && P(it)!=x && P(it)!=y){
            a[x]++;
            p[x].insert(it);
        }
    }
    k.clear();
    for(auto it:d[x]){
        if(P(it)!=x){
            s[P(it)].insert(x);
            k.insert(P(it));
        }
    }
    swap(d[x],k);
    for(auto it:d[y]){
        if(P(it)!=x){
            s[P(it)].insert(x);
            d[x].insert(P(it));
        }
    }
    res+=a[x]*sz[x];
    f[y]=x;
    return ;
}

int main()
{
    cin>>n>>M;
    for(int i=1;i<=n;i++){
        f[i]=i;
        sz[i]=1;
    }
    for(int i=1;i<=M;i++){
        ll x,y;
        cin>>x>>y;
        if(P(x)!=P(y)){
            //cout<<P(y)<<" "<<P(x)<<" "<<s[P(x)].count(P(y))<<endl;
            if(s[P(x)].count(P(y))==1)A(P(x),P(y));
            else{
                if(p[P(y)].count(x)==0){
                    res+=sz[P(y)];
                    p[P(y)].insert(x);
                    a[P(y)]++;
                }
                m[P(x)][P(y)]++;
                s[P(y)].insert(P(x));
                d[P(x)].insert(P(y));
            }
        }
        cout<<res<<endl;

    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19096 KB Output is correct
3 Incorrect 9 ms 19088 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19096 KB Output is correct
3 Incorrect 9 ms 19088 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19096 KB Output is correct
3 Incorrect 9 ms 19088 KB Output isn't correct
4 Halted 0 ms 0 KB -