답안 #945189

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945189 2024-03-13T13:45:18 Z michified 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1484 ms 138740 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;

inline bool isin(ll a, 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<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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 1 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 616 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 4 ms 604 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 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 1 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 616 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 4 ms 604 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 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 15 ms 1120 KB Output is correct
35 Correct 464 ms 20716 KB Output is correct
36 Correct 501 ms 23524 KB Output is correct
37 Correct 498 ms 23412 KB Output is correct
38 Correct 495 ms 23016 KB Output is correct
39 Correct 8 ms 1628 KB Output is correct
40 Correct 12 ms 2352 KB Output is correct
41 Correct 10 ms 2140 KB Output is correct
42 Correct 8 ms 1624 KB Output is correct
43 Correct 9 ms 1628 KB Output is correct
44 Correct 9 ms 1572 KB Output is correct
45 Correct 8 ms 1628 KB Output is correct
46 Correct 8 ms 1628 KB Output is correct
47 Correct 10 ms 1832 KB Output is correct
48 Correct 10 ms 1884 KB Output is correct
49 Correct 23 ms 2652 KB Output is correct
50 Correct 502 ms 23968 KB Output is correct
51 Correct 20 ms 2140 KB Output is correct
52 Correct 478 ms 22096 KB Output is correct
53 Correct 22 ms 2652 KB Output is correct
54 Correct 484 ms 22744 KB Output is correct
55 Correct 10 ms 1884 KB Output is correct
56 Correct 10 ms 1812 KB Output is correct
57 Correct 13 ms 2140 KB Output is correct
58 Correct 10 ms 1884 KB Output is correct
59 Correct 11 ms 2396 KB Output is correct
60 Correct 474 ms 21052 KB Output is correct
61 Correct 11 ms 1884 KB Output is correct
62 Correct 500 ms 22868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 1 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 616 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 4 ms 604 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 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 15 ms 1120 KB Output is correct
35 Correct 464 ms 20716 KB Output is correct
36 Correct 501 ms 23524 KB Output is correct
37 Correct 498 ms 23412 KB Output is correct
38 Correct 495 ms 23016 KB Output is correct
39 Correct 8 ms 1628 KB Output is correct
40 Correct 12 ms 2352 KB Output is correct
41 Correct 10 ms 2140 KB Output is correct
42 Correct 8 ms 1624 KB Output is correct
43 Correct 9 ms 1628 KB Output is correct
44 Correct 9 ms 1572 KB Output is correct
45 Correct 8 ms 1628 KB Output is correct
46 Correct 8 ms 1628 KB Output is correct
47 Correct 10 ms 1832 KB Output is correct
48 Correct 10 ms 1884 KB Output is correct
49 Correct 23 ms 2652 KB Output is correct
50 Correct 502 ms 23968 KB Output is correct
51 Correct 20 ms 2140 KB Output is correct
52 Correct 478 ms 22096 KB Output is correct
53 Correct 22 ms 2652 KB Output is correct
54 Correct 484 ms 22744 KB Output is correct
55 Correct 10 ms 1884 KB Output is correct
56 Correct 10 ms 1812 KB Output is correct
57 Correct 13 ms 2140 KB Output is correct
58 Correct 10 ms 1884 KB Output is correct
59 Correct 11 ms 2396 KB Output is correct
60 Correct 474 ms 21052 KB Output is correct
61 Correct 11 ms 1884 KB Output is correct
62 Correct 500 ms 22868 KB Output is correct
63 Correct 934 ms 90260 KB Output is correct
64 Correct 929 ms 90204 KB Output is correct
65 Correct 927 ms 89892 KB Output is correct
66 Correct 549 ms 65908 KB Output is correct
67 Correct 814 ms 116044 KB Output is correct
68 Correct 571 ms 65804 KB Output is correct
69 Correct 766 ms 65776 KB Output is correct
70 Correct 563 ms 65872 KB Output is correct
71 Correct 566 ms 65876 KB Output is correct
72 Correct 847 ms 84588 KB Output is correct
73 Correct 813 ms 87892 KB Output is correct
74 Correct 1484 ms 110176 KB Output is correct
75 Correct 1085 ms 76468 KB Output is correct
76 Correct 1263 ms 91352 KB Output is correct
77 Correct 1228 ms 91416 KB Output is correct
78 Correct 508 ms 62860 KB Output is correct
79 Correct 954 ms 78748 KB Output is correct
80 Correct 471 ms 59472 KB Output is correct
81 Correct 883 ms 73388 KB Output is correct
82 Correct 1044 ms 79224 KB Output is correct
83 Correct 1052 ms 79520 KB Output is correct
84 Correct 906 ms 79216 KB Output is correct
85 Correct 892 ms 79064 KB Output is correct
86 Correct 713 ms 132176 KB Output is correct
87 Correct 880 ms 138740 KB Output is correct
88 Correct 834 ms 86276 KB Output is correct
89 Correct 1283 ms 89660 KB Output is correct