Submission #968874

# Submission time Handle Problem Language Result Execution time Memory
968874 2024-04-24T08:15:01 Z angella Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
898 ms 100692 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;
        int pv=getPar(v), pu=getPar(u);
        if(getPar(v)!=getPar(u) && st[pu].find(v)==st[pu].end()) {
            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 11 ms 45660 KB Output is correct
2 Correct 20 ms 43612 KB Output is correct
3 Correct 12 ms 43612 KB Output is correct
4 Correct 15 ms 43612 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 13 ms 43608 KB Output is correct
7 Correct 13 ms 43612 KB Output is correct
8 Correct 14 ms 43664 KB Output is correct
9 Correct 13 ms 43608 KB Output is correct
10 Correct 14 ms 43736 KB Output is correct
11 Correct 13 ms 43612 KB Output is correct
12 Correct 13 ms 43612 KB Output is correct
13 Correct 13 ms 43724 KB Output is correct
14 Correct 13 ms 43612 KB Output is correct
15 Correct 12 ms 43612 KB Output is correct
16 Correct 14 ms 43612 KB Output is correct
17 Correct 14 ms 43612 KB Output is correct
18 Correct 18 ms 43608 KB Output is correct
19 Correct 10 ms 45660 KB Output is correct
20 Correct 10 ms 45760 KB Output is correct
21 Correct 12 ms 45660 KB Output is correct
22 Correct 13 ms 43724 KB Output is correct
23 Correct 17 ms 43612 KB Output is correct
24 Correct 14 ms 43704 KB Output is correct
25 Correct 16 ms 43608 KB Output is correct
26 Correct 16 ms 43612 KB Output is correct
27 Correct 14 ms 44112 KB Output is correct
28 Correct 13 ms 43728 KB Output is correct
29 Correct 14 ms 43612 KB Output is correct
30 Correct 15 ms 43608 KB Output is correct
31 Correct 14 ms 43608 KB Output is correct
32 Correct 13 ms 43612 KB Output is correct
33 Correct 14 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 45660 KB Output is correct
2 Correct 20 ms 43612 KB Output is correct
3 Correct 12 ms 43612 KB Output is correct
4 Correct 15 ms 43612 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 13 ms 43608 KB Output is correct
7 Correct 13 ms 43612 KB Output is correct
8 Correct 14 ms 43664 KB Output is correct
9 Correct 13 ms 43608 KB Output is correct
10 Correct 14 ms 43736 KB Output is correct
11 Correct 13 ms 43612 KB Output is correct
12 Correct 13 ms 43612 KB Output is correct
13 Correct 13 ms 43724 KB Output is correct
14 Correct 13 ms 43612 KB Output is correct
15 Correct 12 ms 43612 KB Output is correct
16 Correct 14 ms 43612 KB Output is correct
17 Correct 14 ms 43612 KB Output is correct
18 Correct 18 ms 43608 KB Output is correct
19 Correct 10 ms 45660 KB Output is correct
20 Correct 10 ms 45760 KB Output is correct
21 Correct 12 ms 45660 KB Output is correct
22 Correct 13 ms 43724 KB Output is correct
23 Correct 17 ms 43612 KB Output is correct
24 Correct 14 ms 43704 KB Output is correct
25 Correct 16 ms 43608 KB Output is correct
26 Correct 16 ms 43612 KB Output is correct
27 Correct 14 ms 44112 KB Output is correct
28 Correct 13 ms 43728 KB Output is correct
29 Correct 14 ms 43612 KB Output is correct
30 Correct 15 ms 43608 KB Output is correct
31 Correct 14 ms 43608 KB Output is correct
32 Correct 13 ms 43612 KB Output is correct
33 Correct 14 ms 43612 KB Output is correct
34 Correct 15 ms 43876 KB Output is correct
35 Correct 83 ms 49468 KB Output is correct
36 Correct 92 ms 52264 KB Output is correct
37 Correct 90 ms 54416 KB Output is correct
38 Correct 98 ms 52128 KB Output is correct
39 Correct 15 ms 43864 KB Output is correct
40 Correct 17 ms 44376 KB Output is correct
41 Correct 17 ms 44320 KB Output is correct
42 Correct 20 ms 44008 KB Output is correct
43 Correct 15 ms 43868 KB Output is correct
44 Correct 16 ms 43864 KB Output is correct
45 Correct 20 ms 44124 KB Output is correct
46 Correct 15 ms 43988 KB Output is correct
47 Correct 16 ms 44124 KB Output is correct
48 Correct 13 ms 46168 KB Output is correct
49 Correct 21 ms 46940 KB Output is correct
50 Correct 91 ms 54460 KB Output is correct
51 Correct 15 ms 46424 KB Output is correct
52 Correct 84 ms 50604 KB Output is correct
53 Correct 19 ms 44888 KB Output is correct
54 Correct 83 ms 51540 KB Output is correct
55 Correct 17 ms 44380 KB Output is correct
56 Correct 16 ms 44496 KB Output is correct
57 Correct 14 ms 44380 KB Output is correct
58 Correct 14 ms 44380 KB Output is correct
59 Correct 13 ms 44496 KB Output is correct
60 Correct 67 ms 51252 KB Output is correct
61 Correct 17 ms 44124 KB Output is correct
62 Correct 91 ms 51668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 45660 KB Output is correct
2 Correct 20 ms 43612 KB Output is correct
3 Correct 12 ms 43612 KB Output is correct
4 Correct 15 ms 43612 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 13 ms 43608 KB Output is correct
7 Correct 13 ms 43612 KB Output is correct
8 Correct 14 ms 43664 KB Output is correct
9 Correct 13 ms 43608 KB Output is correct
10 Correct 14 ms 43736 KB Output is correct
11 Correct 13 ms 43612 KB Output is correct
12 Correct 13 ms 43612 KB Output is correct
13 Correct 13 ms 43724 KB Output is correct
14 Correct 13 ms 43612 KB Output is correct
15 Correct 12 ms 43612 KB Output is correct
16 Correct 14 ms 43612 KB Output is correct
17 Correct 14 ms 43612 KB Output is correct
18 Correct 18 ms 43608 KB Output is correct
19 Correct 10 ms 45660 KB Output is correct
20 Correct 10 ms 45760 KB Output is correct
21 Correct 12 ms 45660 KB Output is correct
22 Correct 13 ms 43724 KB Output is correct
23 Correct 17 ms 43612 KB Output is correct
24 Correct 14 ms 43704 KB Output is correct
25 Correct 16 ms 43608 KB Output is correct
26 Correct 16 ms 43612 KB Output is correct
27 Correct 14 ms 44112 KB Output is correct
28 Correct 13 ms 43728 KB Output is correct
29 Correct 14 ms 43612 KB Output is correct
30 Correct 15 ms 43608 KB Output is correct
31 Correct 14 ms 43608 KB Output is correct
32 Correct 13 ms 43612 KB Output is correct
33 Correct 14 ms 43612 KB Output is correct
34 Correct 15 ms 43876 KB Output is correct
35 Correct 83 ms 49468 KB Output is correct
36 Correct 92 ms 52264 KB Output is correct
37 Correct 90 ms 54416 KB Output is correct
38 Correct 98 ms 52128 KB Output is correct
39 Correct 15 ms 43864 KB Output is correct
40 Correct 17 ms 44376 KB Output is correct
41 Correct 17 ms 44320 KB Output is correct
42 Correct 20 ms 44008 KB Output is correct
43 Correct 15 ms 43868 KB Output is correct
44 Correct 16 ms 43864 KB Output is correct
45 Correct 20 ms 44124 KB Output is correct
46 Correct 15 ms 43988 KB Output is correct
47 Correct 16 ms 44124 KB Output is correct
48 Correct 13 ms 46168 KB Output is correct
49 Correct 21 ms 46940 KB Output is correct
50 Correct 91 ms 54460 KB Output is correct
51 Correct 15 ms 46424 KB Output is correct
52 Correct 84 ms 50604 KB Output is correct
53 Correct 19 ms 44888 KB Output is correct
54 Correct 83 ms 51540 KB Output is correct
55 Correct 17 ms 44380 KB Output is correct
56 Correct 16 ms 44496 KB Output is correct
57 Correct 14 ms 44380 KB Output is correct
58 Correct 14 ms 44380 KB Output is correct
59 Correct 13 ms 44496 KB Output is correct
60 Correct 67 ms 51252 KB Output is correct
61 Correct 17 ms 44124 KB Output is correct
62 Correct 91 ms 51668 KB Output is correct
63 Correct 501 ms 96848 KB Output is correct
64 Correct 468 ms 98660 KB Output is correct
65 Correct 450 ms 96848 KB Output is correct
66 Correct 225 ms 62804 KB Output is correct
67 Correct 425 ms 90700 KB Output is correct
68 Correct 211 ms 62800 KB Output is correct
69 Correct 328 ms 64276 KB Output is correct
70 Correct 225 ms 62796 KB Output is correct
71 Correct 221 ms 62952 KB Output is correct
72 Correct 421 ms 76488 KB Output is correct
73 Correct 448 ms 78244 KB Output is correct
74 Correct 898 ms 100692 KB Output is correct
75 Correct 473 ms 72060 KB Output is correct
76 Correct 665 ms 85436 KB Output is correct
77 Correct 664 ms 85612 KB Output is correct
78 Correct 215 ms 67960 KB Output is correct
79 Correct 343 ms 72256 KB Output is correct
80 Correct 193 ms 66624 KB Output is correct
81 Correct 316 ms 70428 KB Output is correct
82 Correct 579 ms 84312 KB Output is correct
83 Correct 584 ms 84308 KB Output is correct
84 Correct 551 ms 83516 KB Output is correct
85 Correct 543 ms 83512 KB Output is correct
86 Correct 290 ms 96952 KB Output is correct
87 Correct 343 ms 99348 KB Output is correct
88 Correct 437 ms 77548 KB Output is correct
89 Correct 615 ms 83432 KB Output is correct