답안 #1070275

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1070275 2024-08-22T12:41:44 Z juicy 우호 조약 체결 (JOI14_friends) C++17
100 / 100
59 ms 11864 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;
    while (m--) {
        int u, v; cin >> u >> v;
        g[u].push_back(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 (int i = 1; i <= n; ++i) {
        if (pr[i] < 0) {
            if (pr[i] == -1) {
                res += g[i].size();
            } else {
                res += (long long) -pr[i] * (-pr[i] - 1);
            }
        }
    }
    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2812 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 2648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2836 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 2808 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2656 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2948 KB Output is correct
2 Correct 1 ms 2872 KB Output is correct
3 Correct 5 ms 3168 KB Output is correct
4 Correct 23 ms 5060 KB Output is correct
5 Correct 25 ms 4584 KB Output is correct
6 Correct 40 ms 5664 KB Output is correct
7 Correct 1 ms 2880 KB Output is correct
8 Correct 3 ms 2920 KB Output is correct
9 Correct 5 ms 3168 KB Output is correct
10 Correct 8 ms 3900 KB Output is correct
11 Correct 42 ms 5720 KB Output is correct
12 Correct 5 ms 3176 KB Output is correct
13 Correct 2 ms 2920 KB Output is correct
14 Correct 2 ms 2920 KB Output is correct
15 Correct 3 ms 2912 KB Output is correct
16 Correct 8 ms 3432 KB Output is correct
17 Correct 28 ms 5620 KB Output is correct
18 Correct 2 ms 2920 KB Output is correct
19 Correct 2 ms 3176 KB Output is correct
20 Correct 29 ms 5720 KB Output is correct
21 Correct 2 ms 2920 KB Output is correct
22 Correct 3 ms 2924 KB Output is correct
23 Correct 2 ms 3056 KB Output is correct
24 Correct 2 ms 2920 KB Output is correct
25 Correct 2 ms 2920 KB Output is correct
26 Correct 3 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 4848 KB Output is correct
2 Correct 40 ms 8472 KB Output is correct
3 Correct 40 ms 8524 KB Output is correct
4 Correct 23 ms 5772 KB Output is correct
5 Correct 17 ms 4956 KB Output is correct
6 Correct 42 ms 7508 KB Output is correct
7 Correct 37 ms 7508 KB Output is correct
8 Correct 39 ms 7496 KB Output is correct
9 Correct 19 ms 5976 KB Output is correct
10 Correct 29 ms 6528 KB Output is correct
11 Correct 52 ms 8412 KB Output is correct
12 Correct 39 ms 9312 KB Output is correct
13 Correct 20 ms 7016 KB Output is correct
14 Correct 32 ms 5984 KB Output is correct
15 Correct 10 ms 4968 KB Output is correct
16 Correct 1 ms 3432 KB Output is correct
17 Correct 1 ms 3164 KB Output is correct
18 Correct 40 ms 7508 KB Output is correct
19 Correct 59 ms 7536 KB Output is correct
20 Correct 21 ms 5460 KB Output is correct
21 Correct 17 ms 4692 KB Output is correct
22 Correct 2 ms 3164 KB Output is correct
23 Correct 14 ms 4440 KB Output is correct
24 Correct 22 ms 5980 KB Output is correct
25 Correct 23 ms 7056 KB Output is correct
26 Correct 26 ms 11864 KB Output is correct
27 Correct 58 ms 10188 KB Output is correct
28 Correct 18 ms 6228 KB Output is correct
29 Correct 18 ms 6232 KB Output is correct
30 Correct 36 ms 8532 KB Output is correct
31 Correct 26 ms 5468 KB Output is correct
32 Correct 24 ms 6848 KB Output is correct
33 Correct 20 ms 6904 KB Output is correct
34 Correct 24 ms 6164 KB Output is correct
35 Correct 19 ms 6968 KB Output is correct
36 Correct 23 ms 8760 KB Output is correct
37 Correct 18 ms 6232 KB Output is correct