답안 #1116862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1116862 2024-11-22T13:38:24 Z byebye75 Mergers (JOI19_mergers) C++14
48 / 100
177 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;
    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 8784 KB Output is correct
5 Correct 3 ms 8952 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 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 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 8784 KB Output is correct
15 Correct 3 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 8784 KB Output is correct
22 Correct 3 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 8784 KB Output is correct
5 Correct 3 ms 8952 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 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 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 8784 KB Output is correct
15 Correct 3 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 8784 KB Output is correct
22 Correct 3 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 64 ms 55876 KB Output is correct
27 Correct 4 ms 10064 KB Output is correct
28 Correct 82 ms 79296 KB Output is correct
29 Correct 81 ms 79432 KB Output is correct
30 Correct 15 ms 13816 KB Output is correct
31 Correct 2 ms 8784 KB Output is correct
32 Correct 85 ms 79420 KB Output is correct
33 Correct 2 ms 8784 KB Output is correct
34 Correct 4 ms 10064 KB Output is correct
35 Correct 80 ms 79408 KB Output is correct
36 Correct 4 ms 10064 KB Output is correct
37 Correct 51 ms 44124 KB Output is correct
38 Correct 2 ms 8784 KB Output is correct
39 Correct 60 ms 44324 KB Output is correct
40 Correct 3 ms 9040 KB Output is correct
41 Correct 3 ms 9040 KB Output is correct
42 Correct 59 ms 44184 KB Output is correct
43 Correct 4 ms 9040 KB Output is correct
44 Correct 2 ms 8784 KB Output is correct
45 Correct 80 ms 77092 KB Output is correct
46 Correct 52 ms 39760 KB Output is correct
47 Correct 4 ms 5200 KB Output is correct
48 Correct 34 ms 19532 KB Output is correct
49 Correct 80 ms 75692 KB Output is correct
50 Correct 83 ms 75960 KB Output is correct
51 Correct 35 ms 19456 KB Output is correct
52 Correct 5 ms 6480 KB Output is correct
53 Correct 50 ms 40476 KB Output is correct
54 Correct 10 ms 6480 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 8784 KB Output is correct
5 Correct 3 ms 8952 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 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 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 8784 KB Output is correct
15 Correct 3 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 8784 KB Output is correct
22 Correct 3 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 4 ms 5200 KB Output is correct
26 Correct 99 ms 51356 KB Output is correct
27 Correct 116 ms 51784 KB Output is correct
28 Correct 4 ms 10064 KB Output is correct
29 Correct 3 ms 8784 KB Output is correct
30 Correct 2 ms 8784 KB Output is correct
31 Correct 100 ms 53280 KB Output is correct
32 Correct 4 ms 10064 KB Output is correct
33 Correct 112 ms 60172 KB Output is correct
34 Correct 103 ms 53304 KB Output is correct
35 Correct 4 ms 10064 KB Output is correct
36 Correct 100 ms 53576 KB Output is correct
37 Correct 3 ms 9040 KB Output is correct
38 Correct 3 ms 9244 KB Output is correct
39 Correct 80 ms 53560 KB Output is correct
40 Correct 3 ms 9040 KB Output is correct
41 Correct 45 ms 15688 KB Output is correct
42 Correct 113 ms 55112 KB Output is correct
43 Correct 2 ms 8784 KB Output is correct
44 Correct 48 ms 22672 KB Output is correct
45 Correct 104 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 86 ms 53436 KB Output is correct
2 Runtime error 177 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 8784 KB Output is correct
5 Correct 3 ms 8952 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 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 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 8784 KB Output is correct
15 Correct 3 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 8784 KB Output is correct
22 Correct 3 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 64 ms 55876 KB Output is correct
27 Correct 4 ms 10064 KB Output is correct
28 Correct 82 ms 79296 KB Output is correct
29 Correct 81 ms 79432 KB Output is correct
30 Correct 15 ms 13816 KB Output is correct
31 Correct 2 ms 8784 KB Output is correct
32 Correct 85 ms 79420 KB Output is correct
33 Correct 2 ms 8784 KB Output is correct
34 Correct 4 ms 10064 KB Output is correct
35 Correct 80 ms 79408 KB Output is correct
36 Correct 4 ms 10064 KB Output is correct
37 Correct 51 ms 44124 KB Output is correct
38 Correct 2 ms 8784 KB Output is correct
39 Correct 60 ms 44324 KB Output is correct
40 Correct 3 ms 9040 KB Output is correct
41 Correct 3 ms 9040 KB Output is correct
42 Correct 59 ms 44184 KB Output is correct
43 Correct 4 ms 9040 KB Output is correct
44 Correct 2 ms 8784 KB Output is correct
45 Correct 80 ms 77092 KB Output is correct
46 Correct 52 ms 39760 KB Output is correct
47 Correct 4 ms 5200 KB Output is correct
48 Correct 34 ms 19532 KB Output is correct
49 Correct 80 ms 75692 KB Output is correct
50 Correct 83 ms 75960 KB Output is correct
51 Correct 35 ms 19456 KB Output is correct
52 Correct 5 ms 6480 KB Output is correct
53 Correct 50 ms 40476 KB Output is correct
54 Correct 10 ms 6480 KB Output is correct
55 Correct 4 ms 5200 KB Output is correct
56 Correct 99 ms 51356 KB Output is correct
57 Correct 116 ms 51784 KB Output is correct
58 Correct 4 ms 10064 KB Output is correct
59 Correct 3 ms 8784 KB Output is correct
60 Correct 2 ms 8784 KB Output is correct
61 Correct 100 ms 53280 KB Output is correct
62 Correct 4 ms 10064 KB Output is correct
63 Correct 112 ms 60172 KB Output is correct
64 Correct 103 ms 53304 KB Output is correct
65 Correct 4 ms 10064 KB Output is correct
66 Correct 100 ms 53576 KB Output is correct
67 Correct 3 ms 9040 KB Output is correct
68 Correct 3 ms 9244 KB Output is correct
69 Correct 80 ms 53560 KB Output is correct
70 Correct 3 ms 9040 KB Output is correct
71 Correct 45 ms 15688 KB Output is correct
72 Correct 113 ms 55112 KB Output is correct
73 Correct 2 ms 8784 KB Output is correct
74 Correct 48 ms 22672 KB Output is correct
75 Correct 104 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 86 ms 53436 KB Output is correct
79 Runtime error 177 ms 262144 KB Execution killed with signal 9
80 Halted 0 ms 0 KB -