답안 #346618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346618 2021-01-10T12:21:34 Z dolphingarlic 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1277 ms 66156 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int cmp[100001];
ll sz[100001], ans = 0;
set<int> followers[100001], graph[100001], rgraph[100001];
queue<pair<int, int>> to_merge;

void insert_weak_connection(int A, int B) {
    graph[A].insert(B);
    rgraph[B].insert(A);
    // If there's A new strong connection between A's and B's components, merge them
    if (graph[B].count(A)) to_merge.push({A, B});
}

int dsu_size(int A) { return followers[A].size() + graph[A].size() + rgraph[A].size(); }

int find(int A) { return (A == cmp[A] ? A : cmp[A] = find(cmp[A])); }

void onion(int A, int B) {
    if (A == B) return;
    // Merge the smaller component into the larger
    if (dsu_size(A) < dsu_size(B)) swap(A, B);

    // Add new contribution
    ans += sz[B] * followers[A].size() + sz[A] * followers[B].size();

    // DSU stuff
    cmp[B] = A;
    sz[A] += sz[B];

    // Merge followers of B into A
    for (int i : followers[B]) {
        if (followers[A].count(i)) ans -= sz[A];
        else followers[A].insert(i);
    }
    // Erase the connections
    graph[A].erase(B), rgraph[A].erase(B);
    graph[B].erase(A), rgraph[B].erase(A);
    // Merge the weak connections to other components
    for (int i : graph[B]) {
        rgraph[i].erase(B);
        insert_weak_connection(A, i);
    }
    for (int i : rgraph[B]) {
        graph[i].erase(B);
        insert_weak_connection(i, A);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cmp[i] = i;
        sz[i] = 1;
        followers[i].insert(i);
    }
    while (m--) {
        int A, B;
        cin >> A >> B;

        B = find(B);
        // If A isn't in B's component and doesn't already follow someone in B's component...
        if (find(A) != B && !followers[B].count(A)) {
            // We insert A as A follower of B and add sz[find(B)] to the answer
            followers[B].insert(A);
            ans += sz[B];

            A = find(A);
            // Add connections between components
            insert_weak_connection(A, B);
            // We may have to merge multiple components for each new event
            while (to_merge.size()) {
                tie(A, B) = to_merge.front();
                to_merge.pop();
                onion(find(A), find(B));
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14444 KB Output is correct
9 Correct 11 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
12 Correct 10 ms 14444 KB Output is correct
13 Correct 10 ms 14444 KB Output is correct
14 Correct 10 ms 14444 KB Output is correct
15 Correct 10 ms 14444 KB Output is correct
16 Correct 10 ms 14444 KB Output is correct
17 Correct 10 ms 14444 KB Output is correct
18 Correct 10 ms 14444 KB Output is correct
19 Correct 10 ms 14444 KB Output is correct
20 Correct 10 ms 14444 KB Output is correct
21 Correct 11 ms 14444 KB Output is correct
22 Correct 10 ms 14444 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14444 KB Output is correct
25 Correct 11 ms 14444 KB Output is correct
26 Correct 10 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 10 ms 14416 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14444 KB Output is correct
9 Correct 11 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
12 Correct 10 ms 14444 KB Output is correct
13 Correct 10 ms 14444 KB Output is correct
14 Correct 10 ms 14444 KB Output is correct
15 Correct 10 ms 14444 KB Output is correct
16 Correct 10 ms 14444 KB Output is correct
17 Correct 10 ms 14444 KB Output is correct
18 Correct 10 ms 14444 KB Output is correct
19 Correct 10 ms 14444 KB Output is correct
20 Correct 10 ms 14444 KB Output is correct
21 Correct 11 ms 14444 KB Output is correct
22 Correct 10 ms 14444 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14444 KB Output is correct
25 Correct 11 ms 14444 KB Output is correct
26 Correct 10 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 10 ms 14416 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14444 KB Output is correct
34 Correct 13 ms 14572 KB Output is correct
35 Correct 100 ms 18412 KB Output is correct
36 Correct 139 ms 20972 KB Output is correct
37 Correct 133 ms 20972 KB Output is correct
38 Correct 129 ms 20716 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 15 ms 15212 KB Output is correct
41 Correct 15 ms 15212 KB Output is correct
42 Correct 13 ms 14828 KB Output is correct
43 Correct 14 ms 14828 KB Output is correct
44 Correct 14 ms 14848 KB Output is correct
45 Correct 13 ms 14828 KB Output is correct
46 Correct 13 ms 14828 KB Output is correct
47 Correct 15 ms 14956 KB Output is correct
48 Correct 15 ms 14956 KB Output is correct
49 Correct 22 ms 15724 KB Output is correct
50 Correct 137 ms 21100 KB Output is correct
51 Correct 19 ms 15084 KB Output is correct
52 Correct 115 ms 19436 KB Output is correct
53 Correct 22 ms 15596 KB Output is correct
54 Correct 127 ms 20128 KB Output is correct
55 Correct 17 ms 15212 KB Output is correct
56 Correct 18 ms 15212 KB Output is correct
57 Correct 16 ms 15212 KB Output is correct
58 Correct 16 ms 15212 KB Output is correct
59 Correct 15 ms 15212 KB Output is correct
60 Correct 97 ms 17900 KB Output is correct
61 Correct 15 ms 15084 KB Output is correct
62 Correct 127 ms 20460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14444 KB Output is correct
9 Correct 11 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
12 Correct 10 ms 14444 KB Output is correct
13 Correct 10 ms 14444 KB Output is correct
14 Correct 10 ms 14444 KB Output is correct
15 Correct 10 ms 14444 KB Output is correct
16 Correct 10 ms 14444 KB Output is correct
17 Correct 10 ms 14444 KB Output is correct
18 Correct 10 ms 14444 KB Output is correct
19 Correct 10 ms 14444 KB Output is correct
20 Correct 10 ms 14444 KB Output is correct
21 Correct 11 ms 14444 KB Output is correct
22 Correct 10 ms 14444 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14444 KB Output is correct
25 Correct 11 ms 14444 KB Output is correct
26 Correct 10 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 10 ms 14416 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14444 KB Output is correct
34 Correct 13 ms 14572 KB Output is correct
35 Correct 100 ms 18412 KB Output is correct
36 Correct 139 ms 20972 KB Output is correct
37 Correct 133 ms 20972 KB Output is correct
38 Correct 129 ms 20716 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 15 ms 15212 KB Output is correct
41 Correct 15 ms 15212 KB Output is correct
42 Correct 13 ms 14828 KB Output is correct
43 Correct 14 ms 14828 KB Output is correct
44 Correct 14 ms 14848 KB Output is correct
45 Correct 13 ms 14828 KB Output is correct
46 Correct 13 ms 14828 KB Output is correct
47 Correct 15 ms 14956 KB Output is correct
48 Correct 15 ms 14956 KB Output is correct
49 Correct 22 ms 15724 KB Output is correct
50 Correct 137 ms 21100 KB Output is correct
51 Correct 19 ms 15084 KB Output is correct
52 Correct 115 ms 19436 KB Output is correct
53 Correct 22 ms 15596 KB Output is correct
54 Correct 127 ms 20128 KB Output is correct
55 Correct 17 ms 15212 KB Output is correct
56 Correct 18 ms 15212 KB Output is correct
57 Correct 16 ms 15212 KB Output is correct
58 Correct 16 ms 15212 KB Output is correct
59 Correct 15 ms 15212 KB Output is correct
60 Correct 97 ms 17900 KB Output is correct
61 Correct 15 ms 15084 KB Output is correct
62 Correct 127 ms 20460 KB Output is correct
63 Correct 669 ms 65276 KB Output is correct
64 Correct 659 ms 65004 KB Output is correct
65 Correct 649 ms 65164 KB Output is correct
66 Correct 298 ms 32364 KB Output is correct
67 Correct 568 ms 59244 KB Output is correct
68 Correct 289 ms 32196 KB Output is correct
69 Correct 587 ms 33260 KB Output is correct
70 Correct 327 ms 32236 KB Output is correct
71 Correct 340 ms 32280 KB Output is correct
72 Correct 599 ms 43296 KB Output is correct
73 Correct 602 ms 44908 KB Output is correct
74 Correct 1277 ms 65644 KB Output is correct
75 Correct 814 ms 38936 KB Output is correct
76 Correct 1002 ms 50984 KB Output is correct
77 Correct 1014 ms 51288 KB Output is correct
78 Correct 327 ms 36332 KB Output is correct
79 Correct 595 ms 39232 KB Output is correct
80 Correct 316 ms 35224 KB Output is correct
81 Correct 487 ms 37100 KB Output is correct
82 Correct 1110 ms 52856 KB Output is correct
83 Correct 1130 ms 52972 KB Output is correct
84 Correct 847 ms 51692 KB Output is correct
85 Correct 855 ms 51436 KB Output is correct
86 Correct 437 ms 65004 KB Output is correct
87 Correct 474 ms 66156 KB Output is correct
88 Correct 615 ms 44268 KB Output is correct
89 Correct 977 ms 48896 KB Output is correct