답안 #684308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684308 2023-01-20T20:57:54 Z finn__ 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
936 ms 69128 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int cmp[100001];
ll sz[100001], ans = 0;
set<int> child[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 child[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] * child[A].size() + sz[A] * child[B].size();

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

    // Merge children of B into A
    for (int i : child[B])
    {
        if (child[A].count(i))
            ans -= sz[A];
        else
            child[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;
        child[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 && !child[B].count(A))
        {
            // We insert A as A follower of B and add sz[find(B)] to the answer
            child[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 8 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 8 ms 14356 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14416 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 8 ms 14420 KB Output is correct
18 Correct 7 ms 14424 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14428 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14348 KB Output is correct
31 Correct 7 ms 14348 KB Output is correct
32 Correct 8 ms 14420 KB Output is correct
33 Correct 8 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 8 ms 14356 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14416 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 8 ms 14420 KB Output is correct
18 Correct 7 ms 14424 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14428 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14348 KB Output is correct
31 Correct 7 ms 14348 KB Output is correct
32 Correct 8 ms 14420 KB Output is correct
33 Correct 8 ms 14428 KB Output is correct
34 Correct 9 ms 14560 KB Output is correct
35 Correct 75 ms 20268 KB Output is correct
36 Correct 96 ms 22988 KB Output is correct
37 Correct 98 ms 23116 KB Output is correct
38 Correct 97 ms 22716 KB Output is correct
39 Correct 9 ms 14784 KB Output is correct
40 Correct 11 ms 15072 KB Output is correct
41 Correct 11 ms 15076 KB Output is correct
42 Correct 9 ms 14704 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 11 ms 14816 KB Output is correct
45 Correct 11 ms 14864 KB Output is correct
46 Correct 10 ms 14804 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 10 ms 14940 KB Output is correct
49 Correct 17 ms 15652 KB Output is correct
50 Correct 99 ms 23252 KB Output is correct
51 Correct 13 ms 15060 KB Output is correct
52 Correct 93 ms 21452 KB Output is correct
53 Correct 18 ms 15572 KB Output is correct
54 Correct 93 ms 22272 KB Output is correct
55 Correct 12 ms 15188 KB Output is correct
56 Correct 11 ms 15188 KB Output is correct
57 Correct 12 ms 15072 KB Output is correct
58 Correct 12 ms 15060 KB Output is correct
59 Correct 10 ms 15188 KB Output is correct
60 Correct 70 ms 20128 KB Output is correct
61 Correct 11 ms 15032 KB Output is correct
62 Correct 94 ms 22564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 8 ms 14356 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14416 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 8 ms 14420 KB Output is correct
18 Correct 7 ms 14424 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14428 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14348 KB Output is correct
31 Correct 7 ms 14348 KB Output is correct
32 Correct 8 ms 14420 KB Output is correct
33 Correct 8 ms 14428 KB Output is correct
34 Correct 9 ms 14560 KB Output is correct
35 Correct 75 ms 20268 KB Output is correct
36 Correct 96 ms 22988 KB Output is correct
37 Correct 98 ms 23116 KB Output is correct
38 Correct 97 ms 22716 KB Output is correct
39 Correct 9 ms 14784 KB Output is correct
40 Correct 11 ms 15072 KB Output is correct
41 Correct 11 ms 15076 KB Output is correct
42 Correct 9 ms 14704 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 11 ms 14816 KB Output is correct
45 Correct 11 ms 14864 KB Output is correct
46 Correct 10 ms 14804 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 10 ms 14940 KB Output is correct
49 Correct 17 ms 15652 KB Output is correct
50 Correct 99 ms 23252 KB Output is correct
51 Correct 13 ms 15060 KB Output is correct
52 Correct 93 ms 21452 KB Output is correct
53 Correct 18 ms 15572 KB Output is correct
54 Correct 93 ms 22272 KB Output is correct
55 Correct 12 ms 15188 KB Output is correct
56 Correct 11 ms 15188 KB Output is correct
57 Correct 12 ms 15072 KB Output is correct
58 Correct 12 ms 15060 KB Output is correct
59 Correct 10 ms 15188 KB Output is correct
60 Correct 70 ms 20128 KB Output is correct
61 Correct 11 ms 15032 KB Output is correct
62 Correct 94 ms 22564 KB Output is correct
63 Correct 500 ms 67908 KB Output is correct
64 Correct 504 ms 67992 KB Output is correct
65 Correct 508 ms 67996 KB Output is correct
66 Correct 214 ms 34064 KB Output is correct
67 Correct 403 ms 61128 KB Output is correct
68 Correct 194 ms 34080 KB Output is correct
69 Correct 380 ms 35200 KB Output is correct
70 Correct 243 ms 33996 KB Output is correct
71 Correct 216 ms 34148 KB Output is correct
72 Correct 451 ms 44948 KB Output is correct
73 Correct 424 ms 46708 KB Output is correct
74 Correct 936 ms 68592 KB Output is correct
75 Correct 561 ms 41880 KB Output is correct
76 Correct 758 ms 53972 KB Output is correct
77 Correct 746 ms 54148 KB Output is correct
78 Correct 227 ms 37580 KB Output is correct
79 Correct 408 ms 42120 KB Output is correct
80 Correct 198 ms 36580 KB Output is correct
81 Correct 330 ms 40256 KB Output is correct
82 Correct 762 ms 54656 KB Output is correct
83 Correct 782 ms 54640 KB Output is correct
84 Correct 590 ms 53324 KB Output is correct
85 Correct 599 ms 53352 KB Output is correct
86 Correct 309 ms 67020 KB Output is correct
87 Correct 328 ms 69128 KB Output is correct
88 Correct 428 ms 45904 KB Output is correct
89 Correct 725 ms 51992 KB Output is correct