Submission #945184

# Submission time Handle Problem Language Result Execution time Memory
945184 2024-03-13T13:41:46 Z michified Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1502 ms 171564 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;

inline bool isin(ll a, unordered_set<ll>& s) {
    return s.find(a) != s.end();
}

int main() {
    ll n, m, i, a, b, ans = 0;
    cin >> n >> m;
    vector<vector<ll>> queries(m, vector<ll>(2));
    for (i = 0; i < m; i++) cin >> queries[i][0] >> queries[i][1];
    vector<unordered_set<ll>> A(n + 1), AB(n + 1), f(n + 1), rf(n + 1);
    vector<ll> root(n + 1);
    queue<pair<ll, ll>> tomerge;
    ll cura, curb;
    for (i = 0; i <= n; i++) {
        A[i].insert(i);
        AB[i].insert(i);
        root[i] = i;
    }

    for (i = 0; i < m; i++) {
        a = queries[i][0];
        b = queries[i][1];
        f[root[a]].insert(root[b]);
        rf[root[b]].insert(root[a]);
        if (not isin(a, AB[root[b]])) {
            AB[root[b]].insert(a);
            ans += A[root[b]].size();
        }
        if (isin(root[a], f[root[b]]) and root[a] != root[b]) tomerge.push(make_pair(root[a], root[b]));
        while (not tomerge.empty()) {
            cura = root[tomerge.front().first];
            curb = root[tomerge.front().second];
            tomerge.pop();
            if (cura == curb) continue;
            ans -= A[cura].size() * (AB[cura].size() - 1);
            ans -= A[curb].size() * (AB[curb].size() - 1);
            if (AB[cura].size() < AB[curb].size()) swap(cura, curb);
            for (ll elem : AB[curb]) AB[cura].insert(elem);
            for (ll elem : A[curb]) A[cura].insert(elem);
            ans += A[cura].size() * (AB[cura].size() - 1);
            f[cura].erase(curb);
            for (ll elem : f[curb]) {
                if (elem == cura) {
                    rf[cura].erase(curb);
                    continue;
                }
                f[cura].insert(elem);
                rf[elem].insert(cura);
                rf[elem].erase(curb);
                if (isin(cura, f[elem])) tomerge.push(make_pair(elem, cura));
            }
            for (ll elem : rf[curb]) {
                if (elem == cura) continue;
                f[elem].insert(cura);
                f[elem].erase(curb);
                rf[cura].insert(elem);
                if (isin(elem, f[cura])) tomerge.push(make_pair(elem, cura));
            }
            for (ll elem : A[curb]) root[elem] = cura;
        }
        cout << ans << endl;
    }
    return 0;
}


/*
8 14
1 2
2 1
3 4
4 3
5 6
6 5
7 8
8 7
1 5
1 3
6 3
4 8
7 6
3 2
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 432 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 432 KB Output is correct
21 Correct 4 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 436 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 432 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 432 KB Output is correct
21 Correct 4 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 436 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 616 KB Output is correct
34 Correct 15 ms 1120 KB Output is correct
35 Correct 448 ms 23120 KB Output is correct
36 Correct 476 ms 26192 KB Output is correct
37 Correct 475 ms 26192 KB Output is correct
38 Correct 465 ms 25832 KB Output is correct
39 Correct 10 ms 2396 KB Output is correct
40 Correct 14 ms 2904 KB Output is correct
41 Correct 12 ms 2908 KB Output is correct
42 Correct 10 ms 2396 KB Output is correct
43 Correct 10 ms 2396 KB Output is correct
44 Correct 10 ms 2316 KB Output is correct
45 Correct 10 ms 2436 KB Output is correct
46 Correct 10 ms 2392 KB Output is correct
47 Correct 11 ms 2652 KB Output is correct
48 Correct 11 ms 2652 KB Output is correct
49 Correct 22 ms 3264 KB Output is correct
50 Correct 477 ms 26292 KB Output is correct
51 Correct 20 ms 2904 KB Output is correct
52 Correct 466 ms 24924 KB Output is correct
53 Correct 21 ms 3160 KB Output is correct
54 Correct 462 ms 25420 KB Output is correct
55 Correct 11 ms 2648 KB Output is correct
56 Correct 11 ms 2652 KB Output is correct
57 Correct 15 ms 2580 KB Output is correct
58 Correct 11 ms 2652 KB Output is correct
59 Correct 12 ms 3264 KB Output is correct
60 Correct 452 ms 24532 KB Output is correct
61 Correct 12 ms 2652 KB Output is correct
62 Correct 483 ms 25748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 432 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 432 KB Output is correct
21 Correct 4 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 436 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 616 KB Output is correct
34 Correct 15 ms 1120 KB Output is correct
35 Correct 448 ms 23120 KB Output is correct
36 Correct 476 ms 26192 KB Output is correct
37 Correct 475 ms 26192 KB Output is correct
38 Correct 465 ms 25832 KB Output is correct
39 Correct 10 ms 2396 KB Output is correct
40 Correct 14 ms 2904 KB Output is correct
41 Correct 12 ms 2908 KB Output is correct
42 Correct 10 ms 2396 KB Output is correct
43 Correct 10 ms 2396 KB Output is correct
44 Correct 10 ms 2316 KB Output is correct
45 Correct 10 ms 2436 KB Output is correct
46 Correct 10 ms 2392 KB Output is correct
47 Correct 11 ms 2652 KB Output is correct
48 Correct 11 ms 2652 KB Output is correct
49 Correct 22 ms 3264 KB Output is correct
50 Correct 477 ms 26292 KB Output is correct
51 Correct 20 ms 2904 KB Output is correct
52 Correct 466 ms 24924 KB Output is correct
53 Correct 21 ms 3160 KB Output is correct
54 Correct 462 ms 25420 KB Output is correct
55 Correct 11 ms 2648 KB Output is correct
56 Correct 11 ms 2652 KB Output is correct
57 Correct 15 ms 2580 KB Output is correct
58 Correct 11 ms 2652 KB Output is correct
59 Correct 12 ms 3264 KB Output is correct
60 Correct 452 ms 24532 KB Output is correct
61 Correct 12 ms 2652 KB Output is correct
62 Correct 483 ms 25748 KB Output is correct
63 Correct 1060 ms 122180 KB Output is correct
64 Correct 1053 ms 122232 KB Output is correct
65 Correct 1020 ms 122388 KB Output is correct
66 Correct 622 ms 106944 KB Output is correct
67 Correct 1046 ms 149132 KB Output is correct
68 Correct 605 ms 106988 KB Output is correct
69 Correct 725 ms 108060 KB Output is correct
70 Correct 610 ms 106952 KB Output is correct
71 Correct 603 ms 106752 KB Output is correct
72 Correct 981 ms 121444 KB Output is correct
73 Correct 996 ms 124476 KB Output is correct
74 Correct 1502 ms 143156 KB Output is correct
75 Correct 963 ms 118072 KB Output is correct
76 Correct 1221 ms 128056 KB Output is correct
77 Correct 1250 ms 128056 KB Output is correct
78 Correct 518 ms 98472 KB Output is correct
79 Correct 876 ms 119220 KB Output is correct
80 Correct 573 ms 95792 KB Output is correct
81 Correct 849 ms 115240 KB Output is correct
82 Correct 902 ms 117048 KB Output is correct
83 Correct 901 ms 116844 KB Output is correct
84 Correct 812 ms 116172 KB Output is correct
85 Correct 809 ms 116092 KB Output is correct
86 Correct 965 ms 163668 KB Output is correct
87 Correct 1112 ms 171564 KB Output is correct
88 Correct 1014 ms 123208 KB Output is correct
89 Correct 1172 ms 126732 KB Output is correct