답안 #346615

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346615 2021-01-10T12:07:40 Z dolphingarlic 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1342 ms 71200 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 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 (sz[A] < sz[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 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14572 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 14572 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 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 10 ms 14592 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 12 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 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14572 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 14572 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 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 10 ms 14592 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 12 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 102 ms 20332 KB Output is correct
36 Correct 139 ms 23148 KB Output is correct
37 Correct 170 ms 23276 KB Output is correct
38 Correct 137 ms 22892 KB Output is correct
39 Correct 13 ms 14828 KB Output is correct
40 Correct 14 ms 15212 KB Output is correct
41 Correct 14 ms 15232 KB Output is correct
42 Correct 14 ms 14828 KB Output is correct
43 Correct 16 ms 14828 KB Output is correct
44 Correct 14 ms 14828 KB Output is correct
45 Correct 13 ms 14860 KB Output is correct
46 Correct 13 ms 14828 KB Output is correct
47 Correct 18 ms 15084 KB Output is correct
48 Correct 15 ms 15084 KB Output is correct
49 Correct 22 ms 15852 KB Output is correct
50 Correct 135 ms 23404 KB Output is correct
51 Correct 18 ms 15212 KB Output is correct
52 Correct 114 ms 21632 KB Output is correct
53 Correct 23 ms 15724 KB Output is correct
54 Correct 124 ms 22380 KB Output is correct
55 Correct 17 ms 15400 KB Output is correct
56 Correct 16 ms 15212 KB Output is correct
57 Correct 15 ms 15212 KB Output is correct
58 Correct 16 ms 15340 KB Output is correct
59 Correct 14 ms 15212 KB Output is correct
60 Correct 98 ms 20204 KB Output is correct
61 Correct 15 ms 15084 KB Output is correct
62 Correct 129 ms 22636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14572 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 14572 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 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 10 ms 14592 KB Output is correct
29 Correct 10 ms 14444 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 12 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 102 ms 20332 KB Output is correct
36 Correct 139 ms 23148 KB Output is correct
37 Correct 170 ms 23276 KB Output is correct
38 Correct 137 ms 22892 KB Output is correct
39 Correct 13 ms 14828 KB Output is correct
40 Correct 14 ms 15212 KB Output is correct
41 Correct 14 ms 15232 KB Output is correct
42 Correct 14 ms 14828 KB Output is correct
43 Correct 16 ms 14828 KB Output is correct
44 Correct 14 ms 14828 KB Output is correct
45 Correct 13 ms 14860 KB Output is correct
46 Correct 13 ms 14828 KB Output is correct
47 Correct 18 ms 15084 KB Output is correct
48 Correct 15 ms 15084 KB Output is correct
49 Correct 22 ms 15852 KB Output is correct
50 Correct 135 ms 23404 KB Output is correct
51 Correct 18 ms 15212 KB Output is correct
52 Correct 114 ms 21632 KB Output is correct
53 Correct 23 ms 15724 KB Output is correct
54 Correct 124 ms 22380 KB Output is correct
55 Correct 17 ms 15400 KB Output is correct
56 Correct 16 ms 15212 KB Output is correct
57 Correct 15 ms 15212 KB Output is correct
58 Correct 16 ms 15340 KB Output is correct
59 Correct 14 ms 15212 KB Output is correct
60 Correct 98 ms 20204 KB Output is correct
61 Correct 15 ms 15084 KB Output is correct
62 Correct 129 ms 22636 KB Output is correct
63 Correct 658 ms 68208 KB Output is correct
64 Correct 677 ms 68204 KB Output is correct
65 Correct 656 ms 68240 KB Output is correct
66 Correct 307 ms 34156 KB Output is correct
67 Correct 610 ms 61932 KB Output is correct
68 Correct 292 ms 34212 KB Output is correct
69 Correct 572 ms 35308 KB Output is correct
70 Correct 350 ms 34156 KB Output is correct
71 Correct 337 ms 34156 KB Output is correct
72 Correct 635 ms 46700 KB Output is correct
73 Correct 643 ms 48236 KB Output is correct
74 Correct 1342 ms 71200 KB Output is correct
75 Correct 843 ms 41964 KB Output is correct
76 Correct 1042 ms 55672 KB Output is correct
77 Correct 1004 ms 55660 KB Output is correct
78 Correct 376 ms 37740 KB Output is correct
79 Correct 574 ms 42220 KB Output is correct
80 Correct 291 ms 36588 KB Output is correct
81 Correct 468 ms 40300 KB Output is correct
82 Correct 1091 ms 54764 KB Output is correct
83 Correct 1080 ms 54636 KB Output is correct
84 Correct 843 ms 53484 KB Output is correct
85 Correct 826 ms 53484 KB Output is correct
86 Correct 437 ms 66924 KB Output is correct
87 Correct 472 ms 69228 KB Output is correct
88 Correct 655 ms 47596 KB Output is correct
89 Correct 985 ms 53720 KB Output is correct