답안 #1070264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1070264 2024-08-22T12:35:34 Z juicy 우호 조약 체결 (JOI14_friends) C++17
100 / 100
58 ms 13008 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5;

int n, m;
int pr[N];
bool vis[N];
vector<int> g[N];

int find(int u) {
    return pr[u] < 0 ? u : pr[u] = find(pr[u]);
}

void mrg(int u, int v) {
    if ((u = find(u)) == (v = find(v))) {
        return;
    }
    if (pr[u] > pr[v]) {
        swap(u, v);
    }
    pr[u] += pr[v];
    pr[v] = u;
} 

void dfs(int u, int root) {
    vis[u] = 1;
    for (int v : g[u]) {
        mrg(v, root);
        if (!vis[v]) {
            dfs(v, root);
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);

    cin >> n >> m;
    vector<array<int, 2>> edges;
    while (m--) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        edges.push_back({u, v});
    }
    fill(pr + 1, pr + n + 1, -1);
    for (int i = 1; i <= n; ++i) {
        if (g[i].size() > 1) {
            int root = g[i][0];
            for (int v : g[i]) {
                mrg(v, root);
                if (!vis[v]) {
                    dfs(v, root);
                }
            }
        }
    }
    long long res = 0;
    for (auto [u, v] : edges) {
        res += find(u) != find(v);
    }
    for (int i = 1; i <= n; ++i) {
        if (pr[i] < 0) {
            res += (long long) -pr[i] * (-pr[i] - 1);
        }
    }
    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2812 KB Output is correct
8 Correct 1 ms 2828 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2664 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 4 ms 3544 KB Output is correct
4 Correct 20 ms 7132 KB Output is correct
5 Correct 16 ms 5580 KB Output is correct
6 Correct 29 ms 7884 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 2 ms 3160 KB Output is correct
9 Correct 6 ms 3544 KB Output is correct
10 Correct 8 ms 4308 KB Output is correct
11 Correct 27 ms 7764 KB Output is correct
12 Correct 3 ms 3420 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2908 KB Output is correct
15 Correct 2 ms 3220 KB Output is correct
16 Correct 6 ms 3620 KB Output is correct
17 Correct 29 ms 7728 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 3164 KB Output is correct
20 Correct 27 ms 7884 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 3164 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2904 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 1 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 7248 KB Output is correct
2 Correct 40 ms 10956 KB Output is correct
3 Correct 40 ms 10944 KB Output is correct
4 Correct 27 ms 7116 KB Output is correct
5 Correct 12 ms 5332 KB Output is correct
6 Correct 37 ms 10168 KB Output is correct
7 Correct 35 ms 9976 KB Output is correct
8 Correct 34 ms 9924 KB Output is correct
9 Correct 20 ms 7292 KB Output is correct
10 Correct 28 ms 8648 KB Output is correct
11 Correct 40 ms 11196 KB Output is correct
12 Correct 58 ms 11968 KB Output is correct
13 Correct 19 ms 8140 KB Output is correct
14 Correct 30 ms 8132 KB Output is correct
15 Correct 11 ms 5328 KB Output is correct
16 Correct 2 ms 3204 KB Output is correct
17 Correct 1 ms 3164 KB Output is correct
18 Correct 38 ms 10096 KB Output is correct
19 Correct 41 ms 10188 KB Output is correct
20 Correct 21 ms 6860 KB Output is correct
21 Correct 11 ms 5076 KB Output is correct
22 Correct 2 ms 3164 KB Output is correct
23 Correct 16 ms 5324 KB Output is correct
24 Correct 23 ms 7108 KB Output is correct
25 Correct 19 ms 8144 KB Output is correct
26 Correct 22 ms 13008 KB Output is correct
27 Correct 41 ms 12480 KB Output is correct
28 Correct 22 ms 7372 KB Output is correct
29 Correct 22 ms 7376 KB Output is correct
30 Correct 41 ms 11116 KB Output is correct
31 Correct 22 ms 6748 KB Output is correct
32 Correct 19 ms 8140 KB Output is correct
33 Correct 20 ms 8136 KB Output is correct
34 Correct 21 ms 7120 KB Output is correct
35 Correct 18 ms 8140 KB Output is correct
36 Correct 21 ms 9936 KB Output is correct
37 Correct 20 ms 7364 KB Output is correct