Submission #956790

# Submission time Handle Problem Language Result Execution time Memory
956790 2024-04-02T13:08:54 Z Sharky Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1505 ms 87348 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define siz(x) (int)x.size()
const int N = 100005;

int p[N], sz[N], ans = 0;
map<pair<int, int>, int> mp;
set<int> st[N], g1[N], g2[N];
queue<pair<int, int>> q;

int find(int u) {
    return p[u] == u ? u : p[u] = find(p[u]);
}

void uni(int u, int v) {
    if (u == v) return;
    mp[{u, v}]++;
    g1[u].insert(v);
    g2[v].insert(u);
    if (g1[v].count(u)) q.push({u, v});
}

void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    if (siz(g1[u]) + siz(g2[u]) + siz(st[u]) < siz(g1[v]) + siz(g2[v]) + siz(st[v])) swap(u, v);
    ans += sz[u] * siz(st[v]) + sz[v] * siz(st[u]);
    p[v] = u;
    sz[u] += sz[v];
    for (int i : g1[v]) {
        g2[i].erase(v);
        uni(u, i);
    }
    g1[v].clear();
    for (int i : g2[v]) {
        g1[i].erase(v);
        uni(i, u);
    }
    g2[v].clear();
    for (int i : st[v]) {
        if (st[u].count(i)) ans -= sz[u];
        else st[u].insert(i);
    }
    st[v].clear();
}

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        p[i] = i, sz[i] = 1;
        st[i].insert(i);
    }
    while (m--) {
        int u, v;
        cin >> u >> v;
        if (st[find(v)].count(u)) { // need to delete?
            cout << ans << '\n';
            continue;
        }
        if (find(u) == find(v)) {
            cout << ans << '\n';
            continue;
        }
        st[find(v)].insert(u);
        u = find(u), v = find(v);
        ans += sz[v];
        uni(u, v);
        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            merge(x, y);
        }
        cout << ans << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 5 ms 14776 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 4 ms 15008 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14948 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 15000 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14992 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15192 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 15004 KB Output is correct
29 Correct 3 ms 14960 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 4 ms 15040 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 5 ms 15008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 5 ms 14776 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 4 ms 15008 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14948 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 15000 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14992 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15192 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 15004 KB Output is correct
29 Correct 3 ms 14960 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 4 ms 15040 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 5 ms 15008 KB Output is correct
34 Correct 5 ms 15192 KB Output is correct
35 Correct 102 ms 21328 KB Output is correct
36 Correct 124 ms 25180 KB Output is correct
37 Correct 118 ms 25424 KB Output is correct
38 Correct 141 ms 24656 KB Output is correct
39 Correct 6 ms 15196 KB Output is correct
40 Correct 11 ms 15960 KB Output is correct
41 Correct 7 ms 15452 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 6 ms 15468 KB Output is correct
45 Correct 7 ms 15192 KB Output is correct
46 Correct 6 ms 15196 KB Output is correct
47 Correct 7 ms 15496 KB Output is correct
48 Correct 8 ms 15452 KB Output is correct
49 Correct 15 ms 16476 KB Output is correct
50 Correct 121 ms 25556 KB Output is correct
51 Correct 10 ms 15708 KB Output is correct
52 Correct 102 ms 22888 KB Output is correct
53 Correct 14 ms 16472 KB Output is correct
54 Correct 107 ms 24088 KB Output is correct
55 Correct 9 ms 15964 KB Output is correct
56 Correct 9 ms 15964 KB Output is correct
57 Correct 9 ms 15964 KB Output is correct
58 Correct 9 ms 15964 KB Output is correct
59 Correct 6 ms 15452 KB Output is correct
60 Correct 78 ms 20308 KB Output is correct
61 Correct 8 ms 15708 KB Output is correct
62 Correct 114 ms 24644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 5 ms 14776 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 4 ms 15008 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14948 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 15000 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14992 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15192 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 15004 KB Output is correct
29 Correct 3 ms 14960 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 4 ms 15040 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 5 ms 15008 KB Output is correct
34 Correct 5 ms 15192 KB Output is correct
35 Correct 102 ms 21328 KB Output is correct
36 Correct 124 ms 25180 KB Output is correct
37 Correct 118 ms 25424 KB Output is correct
38 Correct 141 ms 24656 KB Output is correct
39 Correct 6 ms 15196 KB Output is correct
40 Correct 11 ms 15960 KB Output is correct
41 Correct 7 ms 15452 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 6 ms 15468 KB Output is correct
45 Correct 7 ms 15192 KB Output is correct
46 Correct 6 ms 15196 KB Output is correct
47 Correct 7 ms 15496 KB Output is correct
48 Correct 8 ms 15452 KB Output is correct
49 Correct 15 ms 16476 KB Output is correct
50 Correct 121 ms 25556 KB Output is correct
51 Correct 10 ms 15708 KB Output is correct
52 Correct 102 ms 22888 KB Output is correct
53 Correct 14 ms 16472 KB Output is correct
54 Correct 107 ms 24088 KB Output is correct
55 Correct 9 ms 15964 KB Output is correct
56 Correct 9 ms 15964 KB Output is correct
57 Correct 9 ms 15964 KB Output is correct
58 Correct 9 ms 15964 KB Output is correct
59 Correct 6 ms 15452 KB Output is correct
60 Correct 78 ms 20308 KB Output is correct
61 Correct 8 ms 15708 KB Output is correct
62 Correct 114 ms 24644 KB Output is correct
63 Correct 804 ms 87348 KB Output is correct
64 Correct 735 ms 87304 KB Output is correct
65 Correct 749 ms 87308 KB Output is correct
66 Correct 254 ms 37716 KB Output is correct
67 Correct 631 ms 44368 KB Output is correct
68 Correct 239 ms 37716 KB Output is correct
69 Correct 439 ms 41684 KB Output is correct
70 Correct 286 ms 37536 KB Output is correct
71 Correct 275 ms 37496 KB Output is correct
72 Correct 584 ms 44312 KB Output is correct
73 Correct 614 ms 44416 KB Output is correct
74 Correct 1505 ms 75564 KB Output is correct
75 Correct 721 ms 49468 KB Output is correct
76 Correct 1225 ms 61952 KB Output is correct
77 Correct 1177 ms 62036 KB Output is correct
78 Correct 254 ms 42164 KB Output is correct
79 Correct 494 ms 47188 KB Output is correct
80 Correct 292 ms 42068 KB Output is correct
81 Correct 495 ms 46812 KB Output is correct
82 Correct 1186 ms 69336 KB Output is correct
83 Correct 1321 ms 70472 KB Output is correct
84 Correct 1008 ms 66644 KB Output is correct
85 Correct 930 ms 66904 KB Output is correct
86 Correct 357 ms 38344 KB Output is correct
87 Correct 452 ms 40736 KB Output is correct
88 Correct 626 ms 44776 KB Output is correct
89 Correct 1092 ms 59580 KB Output is correct