Submission #1133806

#TimeUsernameProblemLanguageResultExecution timeMemory
1133806Zbyszek99Making Friends on Joitter is Fun (JOI20_joitter2)C++20
0 / 100
81 ms176448 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

vi comp[300001];
int rep_[300001];
int siz_[300001];
ll in_count[300001];
int total_edges[300001];
set<int> in_edges[300001]; //vert vert
set<int> out_edges[300001]; //vert vert

set<int> edges_v[3000001]; // vert comp
map<pii,bool> edges; // vert vert
ll cur_ans = 0;

int fint(int v)
{
    if(rep_[v] == v) return v;
    rep_[v] = fint(rep_[v]);
    return rep_[v];
}

void onion(int a, int b)
{
    if(total_edges[a] + siz_[a] > total_edges[b] + siz_[b]) swap(a,b);
    cur_ans -= (ll)(siz_[a]) * (ll)(siz_[a]-1);
    cur_ans -= (ll)(siz_[b]) * (ll)(siz_[b]-1);
    
    cur_ans -= in_count[a] * (ll)siz_[a];
    cur_ans -= in_count[b] * (ll)siz_[b];
   // cout << cur_ans << " cur\n";
    forall(v,comp[a])
    {
       // cout << v << " v\n";
        comp[b].pb(v);
        vi del;
        forall(it,in_edges[v])
        {
            if(fint(it) == b)
            {
                out_edges[it].erase(out_edges[it].find(v));
                del.pb(it);
                in_count[a]--;
            }
            else
            {
                if(edges_v[it].find(b) != edges_v[it].end())
                {
                    in_count[a]--;
                }
                edges_v[it].erase(edges_v[it].find(a));
                edges_v[it].insert(b);
            }
        }
        while(!del.empty())
        {
            in_edges[v].erase(in_edges[v].find(del.back()));
            del.pop_back();
        }
        forall(it,out_edges[v])
        {
            if(fint(it) == b)
            {
                in_edges[it].erase(in_edges[it].find(v));
                del.pb(it);
                in_count[b]--;
            }
        }
        while(!del.empty())
        {
            out_edges[v].erase(out_edges[v].find(del.back()));
            del.pop_back();
        }
    }
    in_count[b] += in_count[a];
    rep_[a] = b;
    siz_[b] += siz_[a];
    cur_ans += (ll)(siz_[b]) * (ll)(siz_[b]-1);
    cur_ans += in_count[b] * (ll)siz_[b];
   // cout << in_count[b] << " " << siz_[b] << " res\n"; 
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n,m;
    cin >> n >> m;
    rep2(i,1,n)
    {
        rep_[i] = i;
        siz_[i] = 1; 
        comp[i] = {i};
    }
    rep(i,m)
    {
        int a,b;
        cin >> a >> b;
        int a2 = fint(a);
        int b2 = fint(b);
        if(a2 == b2) 
        {
            cout << cur_ans << "\n";
            continue;
        }
        //cout << a << " " << b << " " << edges[{b,a}] << " xd\n";
        if(edges[{b,a}] == 1)
        {
            onion(a2,b2);
        }
        else 
        {
            edges[{a,b}] = 1;
            if(edges_v[a].find(b2) == edges_v[a].end())
            {
                edges_v[a].insert(b2);
                out_edges[a].insert(b);
                in_edges[b].insert(a);
                cur_ans += siz_[b];
                in_count[b2]++;
                total_edges[a2] += 2;
                total_edges[b2] += 2;
            }
        }
        cout << cur_ans << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...