Submission #968870

# Submission time Handle Problem Language Result Execution time Memory
968870 2024-04-24T08:11:11 Z angella Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
19 ms 45740 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

ll n, m, ans, sz[N], par[N];
set<pii> glob;
set<int> eout[N], ein[N], st[N];

int getPar(int v) {return (par[v]==0 ? v : par[v]=getPar(par[v]));}

void merge(int v, int u) {
    v=getPar(v), u=getPar(u);
    if(u == v) return;
    if(sz[v] < sz[u]) swap(v,u);
    ans += (sz[v]*size(st[u]) + sz[u]*size(st[v]));
    // fuck(ans);
    par[u] = v;
    sz[v] += sz[u];

    for(auto it : st[u]) {
        if(st[v].find(it)!=st[v].end()) ans-=sz[v];
        else st[v].insert(it);
    }
    eout[u].erase(v);
    eout[v].erase(u);
    ein[v].erase(u);
    ein[u].erase(v);
    for(auto it : eout[u]) {
        ein[it].erase(u);
        ein[it].insert(v);
        eout[v].insert(it);
        if(eout[it].find(v) != eout[it].end()) glob.insert({it,v});
    }
    for(auto it : ein[u]) {
        eout[it].erase(u);
        eout[it].insert(v);
        ein[v].insert(it);
        if(ein[it].find(v) != ein[it].end()) glob.insert({it,v});
    }
}

int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n>>m;
    fill(sz+1, sz+n+1, 1);
    for(int i=1; i<=n; i++) st[i].insert(i);
    for(int v,u,i=1; i<=m; i++) {
        cin>>v>>u;
        if(getPar(v)!=getPar(u)) {
            int pv=getPar(v), pu=getPar(u);
            eout[pv].insert(pu);
            ein[pu].insert(pv);
            st[pu].insert(v);
            ans += sz[pu];
            if(eout[pu].find(pv) != eout[pu].end()) {
                glob.insert({v, u});
                while(size(glob) > 0) {
                    pii tmp = *(glob.begin());
                    if(getPar(tmp.F)!=getPar(tmp.S))merge(getPar(tmp.F), getPar(tmp.S));
                    glob.erase(tmp);
                }
            }
        }
        cout<<ans<<endl;
        // if(i == 8) {
        //     for(int j=1; j<=n; j++) fuck(getPar(j));
        // }
    }

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 45660 KB Output is correct
2 Incorrect 10 ms 45740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 45660 KB Output is correct
2 Incorrect 10 ms 45740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 45660 KB Output is correct
2 Incorrect 10 ms 45740 KB Output isn't correct
3 Halted 0 ms 0 KB -