Submission #824181

# Submission time Handle Problem Language Result Execution time Memory
824181 2023-08-13T17:22:08 Z vjudge1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
515 ms 43732 KB
#include <bits/stdc++.h>
using namespace std;
// 123

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n, m;
        cin >> n >> m;
        vector<set<pair<int, int>>> g(n), rg(n);
        vector<int> par(n, -1);
        int64_t ans = 0;
        queue<pair<int, int>> q;

        function<int(int)> root = [&](int u) { return par[u] < 0 ? u : par[u] = root(par[u]); };
        function<int(set<pair<int, int>>&, int)> cnt = [&](set<pair<int, int>>& s, int u) {
                auto it = s.lower_bound(make_pair(u, -1));
                int res = 0;
                while (it != s.end() && it->first == u) {
                        res++, it = s.erase(it);
                }
                return res;
        };
        function<void(int, int)> check = [&](int u, int v) {
                auto it = g[u].lower_bound(make_pair(v, -1));
                if (it != g[u].end() && it->first == v) q.emplace(u, v);
        };
        function<void(int, int)> unite = [&](int u, int v) {
                u = root(u), v = root(v);
                if (u == v) return;

                cnt(g[u], v), cnt(g[v], u);

                ans += 1ll * cnt(rg[v], u) * par[v];
                ans += 1ll * cnt(rg[u], v) * par[u];
                ans += 2ll * par[u] * par[v];
                ans -= 1ll * rg[v].size() * par[u];
                ans -= 1ll * rg[u].size() * par[v];

                if (rg[u].size() + g[u].size() < rg[v].size() + g[v].size()) swap(u, v);
                for (auto x : rg[v]) {
                        if (rg[u].find(x) != rg[u].end()) ans += par[u] + par[v];
                }

                par[u] += par[v];
                par[v] = u;

                for (auto x : rg[v]) rg[u].emplace(x);
                for (auto x : g[v]) g[u].emplace(x);

                for (auto [x, y] : g[v]) {
                        auto it = rg[x].lower_bound(make_pair(v, -1));
                        while (it != rg[x].end() && it->first == v) {
                                rg[x].emplace(u, it->second);
                                it = rg[x].erase(it);
                        }
                }

                for (auto [x, y] : rg[v]) {
                        auto it = g[x].lower_bound(make_pair(v, -1));
                        while (it != g[x].end() && it->first == v) {
                                g[x].emplace(u, it->second);
                                it = g[x].erase(it);
                        }
                }

                for (auto [x, y] : g[v]) check(x, u);
                for (auto [x, y] : rg[v]) check(u, x);
        };

        for (int i = 0; i < m; i++) {
                int u, v;
                cin >> u >> v;
                u--, v--;
                int a = root(u), b = root(v);
                if (a != b) {
                        g[a].emplace(b, v);
                        ans -= rg[b].emplace(a, u).second * par[b];
                        check(b, a);

                        while (q.size()) {
                                auto [x, y] = q.front();
                                q.pop();
                                unite(x, y);
                        }
                }
                cout << ans << '\n';
        }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 58 ms 5752 KB Output is correct
36 Correct 78 ms 7848 KB Output is correct
37 Correct 82 ms 7928 KB Output is correct
38 Correct 75 ms 7696 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 3 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 2 ms 620 KB Output is correct
45 Correct 2 ms 556 KB Output is correct
46 Correct 2 ms 556 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 2 ms 596 KB Output is correct
49 Correct 7 ms 1288 KB Output is correct
50 Correct 79 ms 7996 KB Output is correct
51 Correct 5 ms 852 KB Output is correct
52 Correct 70 ms 6824 KB Output is correct
53 Correct 7 ms 1228 KB Output is correct
54 Correct 77 ms 7368 KB Output is correct
55 Correct 6 ms 980 KB Output is correct
56 Correct 4 ms 968 KB Output is correct
57 Correct 3 ms 852 KB Output is correct
58 Correct 3 ms 852 KB Output is correct
59 Correct 2 ms 592 KB Output is correct
60 Correct 68 ms 5396 KB Output is correct
61 Correct 3 ms 724 KB Output is correct
62 Correct 74 ms 7620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 58 ms 5752 KB Output is correct
36 Correct 78 ms 7848 KB Output is correct
37 Correct 82 ms 7928 KB Output is correct
38 Correct 75 ms 7696 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 3 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 2 ms 620 KB Output is correct
45 Correct 2 ms 556 KB Output is correct
46 Correct 2 ms 556 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 2 ms 596 KB Output is correct
49 Correct 7 ms 1288 KB Output is correct
50 Correct 79 ms 7996 KB Output is correct
51 Correct 5 ms 852 KB Output is correct
52 Correct 70 ms 6824 KB Output is correct
53 Correct 7 ms 1228 KB Output is correct
54 Correct 77 ms 7368 KB Output is correct
55 Correct 6 ms 980 KB Output is correct
56 Correct 4 ms 968 KB Output is correct
57 Correct 3 ms 852 KB Output is correct
58 Correct 3 ms 852 KB Output is correct
59 Correct 2 ms 592 KB Output is correct
60 Correct 68 ms 5396 KB Output is correct
61 Correct 3 ms 724 KB Output is correct
62 Correct 74 ms 7620 KB Output is correct
63 Correct 336 ms 43692 KB Output is correct
64 Correct 274 ms 43732 KB Output is correct
65 Correct 305 ms 43732 KB Output is correct
66 Correct 73 ms 14412 KB Output is correct
67 Correct 128 ms 18740 KB Output is correct
68 Correct 77 ms 14444 KB Output is correct
69 Correct 203 ms 19080 KB Output is correct
70 Correct 72 ms 14524 KB Output is correct
71 Correct 69 ms 14412 KB Output is correct
72 Correct 151 ms 19160 KB Output is correct
73 Correct 136 ms 19020 KB Output is correct
74 Correct 515 ms 37452 KB Output is correct
75 Correct 283 ms 24560 KB Output is correct
76 Correct 364 ms 30556 KB Output is correct
77 Correct 396 ms 30716 KB Output is correct
78 Correct 103 ms 19124 KB Output is correct
79 Correct 186 ms 22628 KB Output is correct
80 Correct 93 ms 19132 KB Output is correct
81 Correct 180 ms 22576 KB Output is correct
82 Correct 386 ms 33520 KB Output is correct
83 Correct 389 ms 33228 KB Output is correct
84 Correct 312 ms 32736 KB Output is correct
85 Correct 342 ms 32732 KB Output is correct
86 Correct 81 ms 13656 KB Output is correct
87 Correct 96 ms 15976 KB Output is correct
88 Correct 151 ms 19204 KB Output is correct
89 Correct 358 ms 29420 KB Output is correct