답안 #1116859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1116859 2024-11-22T13:32:47 Z byebye75 Mergers (JOI19_mergers) C++14
48 / 100
198 ms 262144 KB
#include <bits/stdc++.h>
//#include "bigint.h"
#define db(x) cerr << #x << ": " << x << endl
#define print cerr << "Ah shit, here we go again" << endl
#define int long long int
#define vii vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ff first
#define ss second

using namespace std;

const int N = 1e5 + 10;
const int INF = 1e9 + 10;

int n, k;
int a[N], c[N], sub[N], ans, b[N], root, cnt[N];
vii g[N];
vector<int> mark[N];

void dfs(int v, int p) {
    sub[v] = 1;
    mark[v][a[v]] = 1;
    for (auto u : g[v]) {
        if (u == p) continue;
        dfs(u, v);
        sub[v] += sub[u];
        cnt[v] += cnt[u];
        for (int i = 1; i <= k; i++) {
            mark[v][i] |= mark[u][i];
        }
    }
    int x = 0;
    for (int y = 1; y <= k; y++) {
        if (mark[v][y]) x += c[y];
    }
    if ((x == sub[v] && v != root)) {
        b[v] = 1;
        ++cnt[v];
    }
}

void dfs1(int v, int p) {
    if (b[v] == 1) {
        if (cnt[v] == 1 || cnt[v] == cnt[root]) ++ans;
    }
    for (auto u : g[v]) {
        if (u == p) continue;
        dfs1(u, v);
    }
}



signed main() {

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int v, u; cin >> v >> u;
        g[v].push_back(u);
        g[u].push_back(v);
    }
    for (int i = 1; i <= n; i++) {
        mark[i].resize(k + 1, 0);
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        ++c[a[i]];
    }
    root = 1;
    for (int i = 2; i <= n; i++) {
        if (g[i].size() > g[root].size()) root = i;
    }
    dfs(root, 0);
    dfs1(root, 0);
    cout << (ans + 1) / 2 << '\n';

    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9012 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8784 KB Output is correct
9 Correct 2 ms 8812 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8952 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 8784 KB Output is correct
20 Correct 2 ms 8784 KB Output is correct
21 Correct 2 ms 8636 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9012 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8784 KB Output is correct
9 Correct 2 ms 8812 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8952 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 8784 KB Output is correct
20 Correct 2 ms 8784 KB Output is correct
21 Correct 2 ms 8636 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 59 ms 55892 KB Output is correct
27 Correct 5 ms 10064 KB Output is correct
28 Correct 93 ms 79420 KB Output is correct
29 Correct 73 ms 79436 KB Output is correct
30 Correct 14 ms 13648 KB Output is correct
31 Correct 2 ms 8784 KB Output is correct
32 Correct 82 ms 79432 KB Output is correct
33 Correct 2 ms 8784 KB Output is correct
34 Correct 4 ms 10172 KB Output is correct
35 Correct 81 ms 79320 KB Output is correct
36 Correct 5 ms 10064 KB Output is correct
37 Correct 52 ms 44112 KB Output is correct
38 Correct 2 ms 8784 KB Output is correct
39 Correct 49 ms 44276 KB Output is correct
40 Correct 4 ms 9040 KB Output is correct
41 Correct 4 ms 9040 KB Output is correct
42 Correct 50 ms 44080 KB Output is correct
43 Correct 3 ms 9296 KB Output is correct
44 Correct 2 ms 8956 KB Output is correct
45 Correct 77 ms 77156 KB Output is correct
46 Correct 60 ms 43648 KB Output is correct
47 Correct 2 ms 8784 KB Output is correct
48 Correct 33 ms 23080 KB Output is correct
49 Correct 78 ms 79432 KB Output is correct
50 Correct 84 ms 79436 KB Output is correct
51 Correct 36 ms 23120 KB Output is correct
52 Correct 4 ms 10064 KB Output is correct
53 Correct 56 ms 44112 KB Output is correct
54 Correct 5 ms 10064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9012 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8784 KB Output is correct
9 Correct 2 ms 8812 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8952 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 8784 KB Output is correct
20 Correct 2 ms 8784 KB Output is correct
21 Correct 2 ms 8636 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 92 ms 53468 KB Output is correct
27 Correct 101 ms 53308 KB Output is correct
28 Correct 4 ms 10064 KB Output is correct
29 Correct 2 ms 8784 KB Output is correct
30 Correct 2 ms 8956 KB Output is correct
31 Correct 104 ms 53404 KB Output is correct
32 Correct 5 ms 10064 KB Output is correct
33 Correct 134 ms 61172 KB Output is correct
34 Correct 108 ms 54344 KB Output is correct
35 Correct 5 ms 10064 KB Output is correct
36 Correct 106 ms 53652 KB Output is correct
37 Correct 3 ms 9040 KB Output is correct
38 Correct 3 ms 9212 KB Output is correct
39 Correct 92 ms 53660 KB Output is correct
40 Correct 3 ms 9208 KB Output is correct
41 Correct 48 ms 15696 KB Output is correct
42 Correct 112 ms 56648 KB Output is correct
43 Correct 2 ms 8784 KB Output is correct
44 Correct 56 ms 23776 KB Output is correct
45 Correct 117 ms 57672 KB Output is correct
46 Correct 4 ms 10064 KB Output is correct
47 Correct 4 ms 10064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 53708 KB Output is correct
2 Runtime error 198 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9012 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8784 KB Output is correct
9 Correct 2 ms 8812 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8952 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 8784 KB Output is correct
20 Correct 2 ms 8784 KB Output is correct
21 Correct 2 ms 8636 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 59 ms 55892 KB Output is correct
27 Correct 5 ms 10064 KB Output is correct
28 Correct 93 ms 79420 KB Output is correct
29 Correct 73 ms 79436 KB Output is correct
30 Correct 14 ms 13648 KB Output is correct
31 Correct 2 ms 8784 KB Output is correct
32 Correct 82 ms 79432 KB Output is correct
33 Correct 2 ms 8784 KB Output is correct
34 Correct 4 ms 10172 KB Output is correct
35 Correct 81 ms 79320 KB Output is correct
36 Correct 5 ms 10064 KB Output is correct
37 Correct 52 ms 44112 KB Output is correct
38 Correct 2 ms 8784 KB Output is correct
39 Correct 49 ms 44276 KB Output is correct
40 Correct 4 ms 9040 KB Output is correct
41 Correct 4 ms 9040 KB Output is correct
42 Correct 50 ms 44080 KB Output is correct
43 Correct 3 ms 9296 KB Output is correct
44 Correct 2 ms 8956 KB Output is correct
45 Correct 77 ms 77156 KB Output is correct
46 Correct 60 ms 43648 KB Output is correct
47 Correct 2 ms 8784 KB Output is correct
48 Correct 33 ms 23080 KB Output is correct
49 Correct 78 ms 79432 KB Output is correct
50 Correct 84 ms 79436 KB Output is correct
51 Correct 36 ms 23120 KB Output is correct
52 Correct 4 ms 10064 KB Output is correct
53 Correct 56 ms 44112 KB Output is correct
54 Correct 5 ms 10064 KB Output is correct
55 Correct 2 ms 8784 KB Output is correct
56 Correct 92 ms 53468 KB Output is correct
57 Correct 101 ms 53308 KB Output is correct
58 Correct 4 ms 10064 KB Output is correct
59 Correct 2 ms 8784 KB Output is correct
60 Correct 2 ms 8956 KB Output is correct
61 Correct 104 ms 53404 KB Output is correct
62 Correct 5 ms 10064 KB Output is correct
63 Correct 134 ms 61172 KB Output is correct
64 Correct 108 ms 54344 KB Output is correct
65 Correct 5 ms 10064 KB Output is correct
66 Correct 106 ms 53652 KB Output is correct
67 Correct 3 ms 9040 KB Output is correct
68 Correct 3 ms 9212 KB Output is correct
69 Correct 92 ms 53660 KB Output is correct
70 Correct 3 ms 9208 KB Output is correct
71 Correct 48 ms 15696 KB Output is correct
72 Correct 112 ms 56648 KB Output is correct
73 Correct 2 ms 8784 KB Output is correct
74 Correct 56 ms 23776 KB Output is correct
75 Correct 117 ms 57672 KB Output is correct
76 Correct 4 ms 10064 KB Output is correct
77 Correct 4 ms 10064 KB Output is correct
78 Correct 102 ms 53708 KB Output is correct
79 Runtime error 198 ms 262144 KB Execution killed with signal 9
80 Halted 0 ms 0 KB -