Submission #1020372

# Submission time Handle Problem Language Result Execution time Memory
1020372 2024-07-12T02:43:14 Z khanhphucscratch Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
5 ms 10588 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
multiset<pair<int, int>> remainingEdges;
multiset<pair<int, bool>> ad[100005];
unordered_set<int> sus[100005];
int ans = 0, root[100005], sz[100005], deg[100005];
int getroot(int u)
{
    if(root[u] == u) return u;
    else return getroot(root[u]);
}
void unite(int u, int v)
{
    u = getroot(u); v = getroot(v);
    if(u == v) return;
    if(sz[u] < sz[v]) swap(u, v);
    root[v] = u; sz[u] += sz[v];
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; i++){
        root[i] = i; sz[i] = 1;
    }
    for(int test = 1; test <= m; test++){
        int u, v;
        cin>>u>>v;
        int reu = u;
        u = getroot(u); v = getroot(v);
        if(u == v || sus[v].count(reu) == 1){cout<<ans<<'\n'; continue;}
        int num = remainingEdges.count(make_pair(v, u));
        if(num > 0){
            //Merge two components
            ans += -num*sz[u] + 2*sz[u]*sz[v];
            deg[u] -= num;
        //cout<<"A"<<ans<<endl;
            remainingEdges.erase({v, u});
            ad[v].erase({u, 0});
            ad[u].erase({v, 1});
            if(sz[u] < sz[v]) swap(u, v);
            for(pair<int, bool> i : ad[v]){ //Merge to ad[u]
                ad[u].insert(i);
                pair<int, bool> j = make_pair(v, i.second^1);
            //cout<<"hihi "<<i.first<<" "<<j.first<<" "<<j.second<<" "<<ad[i.first].size()<<endl;
                ad[i.first].erase(ad[i.first].lower_bound(j));
                j.first = u;
                ad[i.first].insert(j);
                if(i.second == 0){
                    //cout<<v<<" "<<i.first<<endl;
                    remainingEdges.erase(remainingEdges.lower_bound({v, i.first}));
                    remainingEdges.insert({u, i.first});
                }
                else{
                    remainingEdges.erase(remainingEdges.lower_bound({i.first, v}));
                    remainingEdges.insert({i.first, u});
                }
            }
            for(int i : sus[v]) sus[u].insert(i);
            ans += deg[u]*sz[v] + deg[v]*sz[u];
            deg[u] += deg[v];
            ad[v].clear();
            unite(u, v);
        }
        else{
            ans += sz[v];
            remainingEdges.insert({u, v});
            ad[u].insert({v, 0});
            ad[v].insert({u, 1});
            sus[v].insert(reu);
            deg[v]++;
        }
        cout<<ans<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 10588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 10588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 10588 KB Output isn't correct
2 Halted 0 ms 0 KB -