Submission #1074809

# Submission time Handle Problem Language Result Execution time Memory
1074809 2024-08-25T14:20:07 Z underwaterkillerwhale Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
2060 ms 212820 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 3e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;

int n, m;
map<pii,int> cnt, dd;
queue<pii> pq;
set<int> inS[N], outS[N], in[N];
ll numin[N];
ll res = 0;

struct Disjoin_set {
    ll lab[N], sz[N];

    void init (int n) {
        rep (i, 1, n) lab[i] = i, sz[i] = 1;
    }

    int Find (int u) {
        return u == lab[u] ? u : lab[u] = Find(lab[u]);
    }

    void add_edge (int u, int v) {
        int uu = Find(u), vv = Find(v);
        if (uu == vv) return;
        if (dd[{u, vv}] == 0) {
            dd[{u, vv}] = 1;
            cnt[{uu, vv}]++;
            numin[vv]++;
            res += sz[vv];
        }
        if (inS[uu].find(vv) != inS[uu].end()) pq.push({uu, vv});
        inS[vv].insert(uu);
        outS[uu].insert(vv);
        in[vv].insert(u);
    }

    void Join (int u, int v) {
        u = Find(u), v = Find(v);
        if (u == v) return;
        if (SZ(in[u]) + SZ(outS[u]) + SZ(inS[u]) < SZ(in[v]) + SZ(outS[v]) + SZ(inS[v])) swap(u, v);
//        cout << u <<" "<<v<<" hihi\n";
        numin[u] -= cnt[{v, u}];
        numin[v] -= cnt[{u, v}];
        res -= sz[u] * (sz[u] - 1) + sz[v] * (sz[v] - 1);
        res -= sz[v] * cnt[{u, v}] + sz[u] * cnt[{v, u}];
//        cout << res<<" "<<cnt[{3, 1}]<<" "<<cnt[{1, 3}]<<"\n";
        res += (sz[u] + sz[v]) * (sz[u] + sz[v] - 1);
        res += numin[v] * sz[u] + numin[u] * sz[v];
        numin[u] += numin[v];
        iter (&id, in[v]) {
            if (Find(id) == v || Find(id) == u) continue;
            if (in[u].find(id) != in[u].end()) {
                res -= sz[u] + sz[v];
                numin[u] --;
            }
            if (dd[{id, u}] == 0) {
                dd[{id, u}] = 1;
                cnt[{Find(id), u}]++;
            }
        }
        iter (&id, inS[v]) {
            if (id == u) continue;
            if (outS[u].find(id) != outS[u].end()) pq.push({id, u});
            outS[id].erase(v), outS[id].insert(u);
            inS[u].insert(id);
        }
        iter (&id, outS[v]) {
            if (id == u) continue;
            if (inS[u].find(id) != inS[u].end()) pq.push({id, u});
            cnt[{u, id}] += cnt[{v, id}];
            inS[id].erase(v), inS[id].insert(u);
            outS[u].insert(id);
        }
        iter (&id, in[v]) {
            if (Find(id) == v || Find(id) == u) continue;
            dd[{id, u}] = 1;
            in[u].insert(id);
        }
//        set<int> ().swap(inS[v]);
//        set<int> ().swap(outS[v]);

        sz[u] += sz[v];
        lab[v] = u;
    }

    void simulate () {
        while (!pq.empty()) {
            pii u = pq.front();
            pq.pop();
            Join(u.fs, u.se);
        }
    }

}DSU;

void solution () {
    cin >> n >> m;
    DSU.init(n);
    rep (i, 1, m) {
        int u, v;
        cin >> u >> v;
        DSU.add_edge(u, v);
        DSU.simulate();
        cout << res <<"\n";
    }
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +4
mình có muốn dùng mảng này không?
4 5
1 3
4 1
1 2
3 2
2 1
return->break
*/
# Verdict Execution time Memory Grader output
1 Correct 12 ms 42588 KB Output is correct
2 Correct 13 ms 42760 KB Output is correct
3 Correct 11 ms 42588 KB Output is correct
4 Correct 13 ms 42588 KB Output is correct
5 Correct 12 ms 42736 KB Output is correct
6 Correct 12 ms 42588 KB Output is correct
7 Correct 12 ms 42764 KB Output is correct
8 Correct 14 ms 42844 KB Output is correct
9 Correct 13 ms 42588 KB Output is correct
10 Correct 12 ms 42776 KB Output is correct
11 Correct 14 ms 42688 KB Output is correct
12 Correct 15 ms 42844 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 12 ms 42600 KB Output is correct
15 Correct 12 ms 42556 KB Output is correct
16 Correct 13 ms 42588 KB Output is correct
17 Correct 14 ms 42776 KB Output is correct
18 Correct 14 ms 42588 KB Output is correct
19 Correct 17 ms 42588 KB Output is correct
20 Correct 15 ms 42616 KB Output is correct
21 Correct 14 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 18 ms 42588 KB Output is correct
24 Correct 13 ms 42608 KB Output is correct
25 Correct 13 ms 42588 KB Output is correct
26 Correct 14 ms 42588 KB Output is correct
27 Correct 13 ms 42584 KB Output is correct
28 Correct 13 ms 42584 KB Output is correct
29 Correct 14 ms 42608 KB Output is correct
30 Correct 14 ms 42696 KB Output is correct
31 Correct 14 ms 42596 KB Output is correct
32 Correct 13 ms 42592 KB Output is correct
33 Correct 13 ms 42588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 42588 KB Output is correct
2 Correct 13 ms 42760 KB Output is correct
3 Correct 11 ms 42588 KB Output is correct
4 Correct 13 ms 42588 KB Output is correct
5 Correct 12 ms 42736 KB Output is correct
6 Correct 12 ms 42588 KB Output is correct
7 Correct 12 ms 42764 KB Output is correct
8 Correct 14 ms 42844 KB Output is correct
9 Correct 13 ms 42588 KB Output is correct
10 Correct 12 ms 42776 KB Output is correct
11 Correct 14 ms 42688 KB Output is correct
12 Correct 15 ms 42844 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 12 ms 42600 KB Output is correct
15 Correct 12 ms 42556 KB Output is correct
16 Correct 13 ms 42588 KB Output is correct
17 Correct 14 ms 42776 KB Output is correct
18 Correct 14 ms 42588 KB Output is correct
19 Correct 17 ms 42588 KB Output is correct
20 Correct 15 ms 42616 KB Output is correct
21 Correct 14 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 18 ms 42588 KB Output is correct
24 Correct 13 ms 42608 KB Output is correct
25 Correct 13 ms 42588 KB Output is correct
26 Correct 14 ms 42588 KB Output is correct
27 Correct 13 ms 42584 KB Output is correct
28 Correct 13 ms 42584 KB Output is correct
29 Correct 14 ms 42608 KB Output is correct
30 Correct 14 ms 42696 KB Output is correct
31 Correct 14 ms 42596 KB Output is correct
32 Correct 13 ms 42592 KB Output is correct
33 Correct 13 ms 42588 KB Output is correct
34 Correct 14 ms 42844 KB Output is correct
35 Correct 76 ms 49988 KB Output is correct
36 Correct 115 ms 54720 KB Output is correct
37 Correct 122 ms 54868 KB Output is correct
38 Correct 105 ms 54096 KB Output is correct
39 Correct 22 ms 43868 KB Output is correct
40 Correct 23 ms 44892 KB Output is correct
41 Correct 24 ms 44892 KB Output is correct
42 Correct 18 ms 43932 KB Output is correct
43 Correct 19 ms 43708 KB Output is correct
44 Correct 21 ms 43868 KB Output is correct
45 Correct 22 ms 43772 KB Output is correct
46 Correct 17 ms 43864 KB Output is correct
47 Correct 24 ms 44380 KB Output is correct
48 Correct 22 ms 44368 KB Output is correct
49 Correct 27 ms 45560 KB Output is correct
50 Correct 115 ms 55104 KB Output is correct
51 Correct 29 ms 44360 KB Output is correct
52 Correct 95 ms 51796 KB Output is correct
53 Correct 31 ms 45412 KB Output is correct
54 Correct 103 ms 53360 KB Output is correct
55 Correct 20 ms 44576 KB Output is correct
56 Correct 28 ms 44456 KB Output is correct
57 Correct 23 ms 44380 KB Output is correct
58 Correct 23 ms 44380 KB Output is correct
59 Correct 22 ms 45240 KB Output is correct
60 Correct 69 ms 50004 KB Output is correct
61 Correct 22 ms 44376 KB Output is correct
62 Correct 101 ms 53900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 42588 KB Output is correct
2 Correct 13 ms 42760 KB Output is correct
3 Correct 11 ms 42588 KB Output is correct
4 Correct 13 ms 42588 KB Output is correct
5 Correct 12 ms 42736 KB Output is correct
6 Correct 12 ms 42588 KB Output is correct
7 Correct 12 ms 42764 KB Output is correct
8 Correct 14 ms 42844 KB Output is correct
9 Correct 13 ms 42588 KB Output is correct
10 Correct 12 ms 42776 KB Output is correct
11 Correct 14 ms 42688 KB Output is correct
12 Correct 15 ms 42844 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 12 ms 42600 KB Output is correct
15 Correct 12 ms 42556 KB Output is correct
16 Correct 13 ms 42588 KB Output is correct
17 Correct 14 ms 42776 KB Output is correct
18 Correct 14 ms 42588 KB Output is correct
19 Correct 17 ms 42588 KB Output is correct
20 Correct 15 ms 42616 KB Output is correct
21 Correct 14 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 18 ms 42588 KB Output is correct
24 Correct 13 ms 42608 KB Output is correct
25 Correct 13 ms 42588 KB Output is correct
26 Correct 14 ms 42588 KB Output is correct
27 Correct 13 ms 42584 KB Output is correct
28 Correct 13 ms 42584 KB Output is correct
29 Correct 14 ms 42608 KB Output is correct
30 Correct 14 ms 42696 KB Output is correct
31 Correct 14 ms 42596 KB Output is correct
32 Correct 13 ms 42592 KB Output is correct
33 Correct 13 ms 42588 KB Output is correct
34 Correct 14 ms 42844 KB Output is correct
35 Correct 76 ms 49988 KB Output is correct
36 Correct 115 ms 54720 KB Output is correct
37 Correct 122 ms 54868 KB Output is correct
38 Correct 105 ms 54096 KB Output is correct
39 Correct 22 ms 43868 KB Output is correct
40 Correct 23 ms 44892 KB Output is correct
41 Correct 24 ms 44892 KB Output is correct
42 Correct 18 ms 43932 KB Output is correct
43 Correct 19 ms 43708 KB Output is correct
44 Correct 21 ms 43868 KB Output is correct
45 Correct 22 ms 43772 KB Output is correct
46 Correct 17 ms 43864 KB Output is correct
47 Correct 24 ms 44380 KB Output is correct
48 Correct 22 ms 44368 KB Output is correct
49 Correct 27 ms 45560 KB Output is correct
50 Correct 115 ms 55104 KB Output is correct
51 Correct 29 ms 44360 KB Output is correct
52 Correct 95 ms 51796 KB Output is correct
53 Correct 31 ms 45412 KB Output is correct
54 Correct 103 ms 53360 KB Output is correct
55 Correct 20 ms 44576 KB Output is correct
56 Correct 28 ms 44456 KB Output is correct
57 Correct 23 ms 44380 KB Output is correct
58 Correct 23 ms 44380 KB Output is correct
59 Correct 22 ms 45240 KB Output is correct
60 Correct 69 ms 50004 KB Output is correct
61 Correct 22 ms 44376 KB Output is correct
62 Correct 101 ms 53900 KB Output is correct
63 Correct 781 ms 130080 KB Output is correct
64 Correct 776 ms 129872 KB Output is correct
65 Correct 807 ms 129872 KB Output is correct
66 Correct 506 ms 102616 KB Output is correct
67 Correct 1532 ms 188516 KB Output is correct
68 Correct 489 ms 101608 KB Output is correct
69 Correct 618 ms 101632 KB Output is correct
70 Correct 550 ms 101716 KB Output is correct
71 Correct 595 ms 101740 KB Output is correct
72 Correct 1196 ms 135820 KB Output is correct
73 Correct 1236 ms 141116 KB Output is correct
74 Correct 2060 ms 185260 KB Output is correct
75 Correct 831 ms 113136 KB Output is correct
76 Correct 1349 ms 143060 KB Output is correct
77 Correct 1400 ms 143208 KB Output is correct
78 Correct 406 ms 92476 KB Output is correct
79 Correct 678 ms 110044 KB Output is correct
80 Correct 363 ms 89656 KB Output is correct
81 Correct 659 ms 102996 KB Output is correct
82 Correct 1160 ms 132268 KB Output is correct
83 Correct 1164 ms 132436 KB Output is correct
84 Correct 1020 ms 132384 KB Output is correct
85 Correct 1080 ms 132240 KB Output is correct
86 Correct 1437 ms 211796 KB Output is correct
87 Correct 1420 ms 212820 KB Output is correct
88 Correct 1077 ms 137212 KB Output is correct
89 Correct 1254 ms 139244 KB Output is correct