답안 #379726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379726 2021-03-19T05:49:01 Z Jarif_Rahman 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int ttt = 0;
struct dsu{
    int n;
    vector<int> p;
    vector<ll> sz;
    vector<set<int>> aa, bb;
    vector<set<pair<int, int>>> cnt;
    vector<ll> cnt2;
    ll ans;
    dsu(int nn){
        n = nn;
        ans = 0;
        sz.resize(n, 1);
        p.resize(n);
        aa.resize(n);
        bb.resize(n);
        cnt.resize(n);
        cnt2.resize(n);
        for(int i = 0; i < n; i++) p[i] = i;
    }
    int get(int x){
        if(p[x] != x) p[x] = get(p[x]);
        return p[x];
    }
    void unite(int a, int b){
        ttt++;
        a = get(a), b  = get(b);
        if(a == b) return;
        if(sz[b] > sz[a]) swap(a, b);
        ans-=sz[a]*(sz[a]-1);
        ans-=cnt2[a]*sz[a];
        ans-=sz[b]*(sz[b]-1);
        ans-=cnt2[b]*sz[b];
        //if(ttt == 2) cerr << ans << " d\n";


        auto it = cnt[a].lower_bound({b, 0});
        if(it != cnt[a].end() && (*it).f == b){
            //ans-=sz[b]*(*it).sc;
            cnt2[a]-=(*it).sc;
            cnt[a].erase(it);
        }
        it = cnt[b].lower_bound({a, 0});
        if(it != cnt[b].end() && (*it).f == a){
            //ans+=sz[a]*(*it).sc;
            cnt[b].erase(it);
        }
        
        aa[a].erase(b);
        bb[a].erase(b);
        aa[b].erase(a);
        bb[b].erase(a);

        auto exc = [&](int x, int y, int a0, int b0){
            auto it = cnt[b0].lower_bound({x, 0});
            if(it == cnt[b0].end() || (*it).f != x) return;
            auto p = (*it);
            p.f = y;
            cnt2[x]-=p.sc;
            cnt2[y]+=p.sc;
            cnt[b0].erase(it);
            it = cnt[a0].lower_bound({y, 0});
            if(it == cnt[a0].end() || (*it).f != y){
                cnt[a0].insert(p);
            }
            else{
                p.sc+=(*it).sc;
                cnt[a0].erase(it);
                cnt[a0].insert(p);
            }
        };
        while(!aa[b].empty()){
            int x = *aa[b].begin();
            aa[b].erase(aa[b].begin());
            exc(x, x, a, b);
            aa[a].insert(x);
        }
        while(!bb[b].empty()){
            int x = *bb[b].begin();
            bb[b].erase(bb[b].begin());
            exc(b, a, x, x);
            aa[x].erase(b);
            aa[x].insert(a);
            bb[a].insert(x);
        }

        sz[a]+=sz[b];
        sz[b] = 0;
        p[b] = a;
        
        ans+=cnt2[a]*sz[a];
        ans+=sz[a]*(sz[a]-1);
    }
    void add_edge(int a, int b){
        a = get(a), b = get(b);
        if(a == b) return;
        if(aa[a].find(b) != aa[a].end()) return;
        aa[a].insert(b);
        bb[b].insert(a);
        ans+=sz[b];
        auto it = cnt[a].lower_bound({b, 0});
        cnt2[b]++;
        if(it == cnt[a].end() || (*it).f != b){
            cnt[a].insert({b, 1});
        }
        
        else{
            auto p = *it;
            p.sc++;
            cnt[a].erase(it);
            cnt[a].insert(p);
        }
        if(bb[a].find(b) != bb[a].end()) unite(a, b);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m; cin >> n >> m;
    dsu ds(n);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b; a--, b--;
        ds.add_edge(a, b);
        cout << ds.ans << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -