답안 #895319

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895319 2023-12-29T18:44:01 Z box 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
865 ms 73220 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
using pii = pair<int, int>;

const int MAXN = 1e5;

int N, M;
int sz[MAXN], parent[MAXN];
list<int> vertices[MAXN];
set<int> out[MAXN], in[MAXN];
set<int> in_v[MAXN];
i64 num_edges;

int find(int i) {
    return parent[i] == i ? i : parent[i] = find(parent[i]);
}

void add_edge(int i, int j) {
    int x = find(i);
    int y = find(j);
    if (x == y) return;
    if (in_v[y].count(i)) return;
    in_v[y].insert(i);
    num_edges += sz[y];
    queue<pii> mrg;
    auto ins = [&](int x, int y) {
        out[x].insert(y);
        in[y].insert(x);
        if (out[y].count(x)) mrg.push({x, y});
    };
    ins(x, y);
    while (sz(mrg)) {
        auto [x, y] = mrg.front(); mrg.pop();
        x = find(x);
        y = find(y);
        if (x != y) {
            if (sz[x] < sz[y]) swap(x, y);
            num_edges += i64(sz(in_v[x])) * sz[y];
            num_edges += i64(sz(in_v[y])) * sz[x];
            out[x].erase(y);
            in[x].erase(y);
            for (int i : exchange(in_v[y], {})) {
                if (in_v[x].count(i)) num_edges -= sz[x] + sz[y];
                else in_v[x].insert(i);
            }
            for (int i : exchange(out[y], {})) {
                if (i == x) continue;
                in[i].erase(y);
                ins(x, i);
            }
            for (int i : exchange(in[y], {})) {
                if (i == x) continue;
                out[i].erase(y);
                ins(i, x);
            }
            vertices[x].splice(end(vertices[x]), vertices[y]);
            parent[y] = x;
            sz[x] += sz[y];
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        sz[i] = 1;
        parent[i] = i;
        vertices[i] = {i};
        in_v[i] = {i};
    }
    while (M--) {
        int i, j;
        cin >> i >> j, i--, j--;
        add_edge(i, j);
        cout << num_edges << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 17240 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16988 KB Output is correct
8 Correct 4 ms 17084 KB Output is correct
9 Correct 4 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 17044 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 5 ms 17252 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 3 ms 16988 KB Output is correct
16 Correct 3 ms 17032 KB Output is correct
17 Correct 4 ms 17044 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16984 KB Output is correct
25 Correct 5 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 3 ms 16988 KB Output is correct
28 Correct 3 ms 16840 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 17060 KB Output is correct
32 Correct 3 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 17240 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16988 KB Output is correct
8 Correct 4 ms 17084 KB Output is correct
9 Correct 4 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 17044 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 5 ms 17252 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 3 ms 16988 KB Output is correct
16 Correct 3 ms 17032 KB Output is correct
17 Correct 4 ms 17044 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16984 KB Output is correct
25 Correct 5 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 3 ms 16988 KB Output is correct
28 Correct 3 ms 16840 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 17060 KB Output is correct
32 Correct 3 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
34 Correct 6 ms 16984 KB Output is correct
35 Correct 61 ms 22612 KB Output is correct
36 Correct 88 ms 25676 KB Output is correct
37 Correct 81 ms 25424 KB Output is correct
38 Correct 77 ms 25060 KB Output is correct
39 Correct 6 ms 17240 KB Output is correct
40 Correct 7 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 6 ms 17240 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 6 ms 17244 KB Output is correct
46 Correct 5 ms 17240 KB Output is correct
47 Correct 7 ms 17244 KB Output is correct
48 Correct 9 ms 17332 KB Output is correct
49 Correct 12 ms 17864 KB Output is correct
50 Correct 81 ms 25564 KB Output is correct
51 Correct 9 ms 17500 KB Output is correct
52 Correct 70 ms 23892 KB Output is correct
53 Correct 11 ms 18012 KB Output is correct
54 Correct 83 ms 24784 KB Output is correct
55 Correct 8 ms 17836 KB Output is correct
56 Correct 8 ms 17756 KB Output is correct
57 Correct 8 ms 17784 KB Output is correct
58 Correct 10 ms 17828 KB Output is correct
59 Correct 6 ms 17244 KB Output is correct
60 Correct 56 ms 22096 KB Output is correct
61 Correct 7 ms 17244 KB Output is correct
62 Correct 76 ms 25016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 17240 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16988 KB Output is correct
8 Correct 4 ms 17084 KB Output is correct
9 Correct 4 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 17044 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 5 ms 17252 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 3 ms 16988 KB Output is correct
16 Correct 3 ms 17032 KB Output is correct
17 Correct 4 ms 17044 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16984 KB Output is correct
25 Correct 5 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 3 ms 16988 KB Output is correct
28 Correct 3 ms 16840 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 17060 KB Output is correct
32 Correct 3 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
34 Correct 6 ms 16984 KB Output is correct
35 Correct 61 ms 22612 KB Output is correct
36 Correct 88 ms 25676 KB Output is correct
37 Correct 81 ms 25424 KB Output is correct
38 Correct 77 ms 25060 KB Output is correct
39 Correct 6 ms 17240 KB Output is correct
40 Correct 7 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 6 ms 17240 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 6 ms 17244 KB Output is correct
46 Correct 5 ms 17240 KB Output is correct
47 Correct 7 ms 17244 KB Output is correct
48 Correct 9 ms 17332 KB Output is correct
49 Correct 12 ms 17864 KB Output is correct
50 Correct 81 ms 25564 KB Output is correct
51 Correct 9 ms 17500 KB Output is correct
52 Correct 70 ms 23892 KB Output is correct
53 Correct 11 ms 18012 KB Output is correct
54 Correct 83 ms 24784 KB Output is correct
55 Correct 8 ms 17836 KB Output is correct
56 Correct 8 ms 17756 KB Output is correct
57 Correct 8 ms 17784 KB Output is correct
58 Correct 10 ms 17828 KB Output is correct
59 Correct 6 ms 17244 KB Output is correct
60 Correct 56 ms 22096 KB Output is correct
61 Correct 7 ms 17244 KB Output is correct
62 Correct 76 ms 25016 KB Output is correct
63 Correct 496 ms 73220 KB Output is correct
64 Correct 499 ms 73044 KB Output is correct
65 Correct 487 ms 73040 KB Output is correct
66 Correct 215 ms 29776 KB Output is correct
67 Correct 396 ms 36180 KB Output is correct
68 Correct 189 ms 29904 KB Output is correct
69 Correct 329 ms 36912 KB Output is correct
70 Correct 187 ms 29780 KB Output is correct
71 Correct 224 ms 29868 KB Output is correct
72 Correct 372 ms 36180 KB Output is correct
73 Correct 393 ms 36132 KB Output is correct
74 Correct 865 ms 47140 KB Output is correct
75 Correct 482 ms 43064 KB Output is correct
76 Correct 664 ms 47188 KB Output is correct
77 Correct 652 ms 47076 KB Output is correct
78 Correct 204 ms 35668 KB Output is correct
79 Correct 337 ms 38992 KB Output is correct
80 Correct 199 ms 35672 KB Output is correct
81 Correct 294 ms 38908 KB Output is correct
82 Correct 708 ms 57572 KB Output is correct
83 Correct 746 ms 57476 KB Output is correct
84 Correct 597 ms 57236 KB Output is correct
85 Correct 674 ms 57220 KB Output is correct
86 Correct 403 ms 30656 KB Output is correct
87 Correct 436 ms 32780 KB Output is correct
88 Correct 503 ms 36296 KB Output is correct
89 Correct 843 ms 46608 KB Output is correct