# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106173 | 2019-04-17T02:48:59 Z | mzhao | Mergers (JOI19_mergers) | C++11 | 87 ms | 60664 KB |
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG #define D(x...) printf(x) #else #define D(x...) #endif #define MN 500100 #define x first #define y second int N, K, S[MN]; vector<int> adj[MN]; int tot[MN], cnt[MN], ans, dep[MN], par[MN]; int cur[3100][3100]; bool bad[MN]; int g[3100]; int f(int x) { if (g[x] != x) g[x] = f(g[x]); return g[x]; } void merge(int a, int b) { if (f(a) != f(b)) g[f(a)] = f(b); } vector<int> adj2[MN]; void dfs(int n, int p, int d) { dep[n] = d; par[n] = p; for (int i = 1; i <= K; i++) cur[n][i] = cnt[i]; cnt[S[n]]++; for (int i : adj[n]) if (i != p) { dfs(i, n, d+1); } for (int i = 1; i <= K; i++) cur[n][i] = cnt[i]-cur[n][i]; if (n == 1) return; bool f = 1; for (int i = 1; i <= K; i++) if (cur[n][i] && cur[n][i] != tot[i]) { f = 0; break; } bad[n] = f; if (!f) merge(n, p); } int leaves; void dfs2(int n, int p) { if (adj2[n].size() == 1) leaves++; for (int i : adj2[n]) if (i != p) dfs2(i, n); } int main() { scanf("%d%d", &N, &K); assert(N <= 3000); for (int i = 1, A, B; i < N; i++) { scanf("%d%d", &A, &B); adj[A].push_back(B); adj[B].push_back(A); } for (int i = 1; i <= N; i++) { scanf("%d", &S[i]); tot[S[i]]++; g[i] = i; } dfs(1, 0, 0); for (int i = 1; i <= N; i++) { for (int j : adj[i]) { int a = f(i), b = f(j); if (a != b) { adj2[a].push_back(b); D("edge between %d %d\n", a, b); } } } dfs2(f(1), 0); printf("%d\n", (leaves+1)/2); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 24 ms | 23936 KB | Output is correct |
2 | Correct | 30 ms | 24192 KB | Output is correct |
3 | Correct | 27 ms | 24320 KB | Output is correct |
4 | Correct | 26 ms | 23936 KB | Output is correct |
5 | Correct | 33 ms | 23928 KB | Output is correct |
6 | Correct | 27 ms | 24192 KB | Output is correct |
7 | Correct | 25 ms | 23936 KB | Output is correct |
8 | Correct | 25 ms | 24284 KB | Output is correct |
9 | Correct | 23 ms | 23928 KB | Output is correct |
10 | Correct | 23 ms | 23808 KB | Output is correct |
11 | Correct | 26 ms | 24320 KB | Output is correct |
12 | Correct | 26 ms | 24184 KB | Output is correct |
13 | Correct | 21 ms | 23928 KB | Output is correct |
14 | Correct | 25 ms | 24192 KB | Output is correct |
15 | Correct | 27 ms | 24312 KB | Output is correct |
16 | Correct | 24 ms | 23936 KB | Output is correct |
17 | Correct | 25 ms | 23936 KB | Output is correct |
18 | Correct | 28 ms | 24320 KB | Output is correct |
19 | Correct | 28 ms | 23800 KB | Output is correct |
20 | Correct | 22 ms | 23808 KB | Output is correct |
21 | Correct | 25 ms | 24320 KB | Output is correct |
22 | Correct | 25 ms | 24184 KB | Output is correct |
23 | Correct | 30 ms | 23936 KB | Output is correct |
24 | Correct | 25 ms | 23936 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 24 ms | 23936 KB | Output is correct |
2 | Correct | 30 ms | 24192 KB | Output is correct |
3 | Correct | 27 ms | 24320 KB | Output is correct |
4 | Correct | 26 ms | 23936 KB | Output is correct |
5 | Correct | 33 ms | 23928 KB | Output is correct |
6 | Correct | 27 ms | 24192 KB | Output is correct |
7 | Correct | 25 ms | 23936 KB | Output is correct |
8 | Correct | 25 ms | 24284 KB | Output is correct |
9 | Correct | 23 ms | 23928 KB | Output is correct |
10 | Correct | 23 ms | 23808 KB | Output is correct |
11 | Correct | 26 ms | 24320 KB | Output is correct |
12 | Correct | 26 ms | 24184 KB | Output is correct |
13 | Correct | 21 ms | 23928 KB | Output is correct |
14 | Correct | 25 ms | 24192 KB | Output is correct |
15 | Correct | 27 ms | 24312 KB | Output is correct |
16 | Correct | 24 ms | 23936 KB | Output is correct |
17 | Correct | 25 ms | 23936 KB | Output is correct |
18 | Correct | 28 ms | 24320 KB | Output is correct |
19 | Correct | 28 ms | 23800 KB | Output is correct |
20 | Correct | 22 ms | 23808 KB | Output is correct |
21 | Correct | 25 ms | 24320 KB | Output is correct |
22 | Correct | 25 ms | 24184 KB | Output is correct |
23 | Correct | 30 ms | 23936 KB | Output is correct |
24 | Correct | 25 ms | 23936 KB | Output is correct |
25 | Correct | 25 ms | 24056 KB | Output is correct |
26 | Correct | 84 ms | 59612 KB | Output is correct |
27 | Correct | 36 ms | 36600 KB | Output is correct |
28 | Correct | 83 ms | 60584 KB | Output is correct |
29 | Correct | 85 ms | 60536 KB | Output is correct |
30 | Correct | 45 ms | 38392 KB | Output is correct |
31 | Correct | 23 ms | 24064 KB | Output is correct |
32 | Correct | 69 ms | 60536 KB | Output is correct |
33 | Correct | 23 ms | 24164 KB | Output is correct |
34 | Correct | 38 ms | 36600 KB | Output is correct |
35 | Correct | 83 ms | 60536 KB | Output is correct |
36 | Correct | 38 ms | 36600 KB | Output is correct |
37 | Correct | 56 ms | 53624 KB | Output is correct |
38 | Correct | 68 ms | 24312 KB | Output is correct |
39 | Correct | 50 ms | 53624 KB | Output is correct |
40 | Correct | 35 ms | 36088 KB | Output is correct |
41 | Correct | 35 ms | 36096 KB | Output is correct |
42 | Correct | 57 ms | 53656 KB | Output is correct |
43 | Correct | 41 ms | 36216 KB | Output is correct |
44 | Correct | 26 ms | 24440 KB | Output is correct |
45 | Correct | 86 ms | 60536 KB | Output is correct |
46 | Correct | 51 ms | 53240 KB | Output is correct |
47 | Correct | 24 ms | 24320 KB | Output is correct |
48 | Correct | 45 ms | 43128 KB | Output is correct |
49 | Correct | 70 ms | 60536 KB | Output is correct |
50 | Correct | 87 ms | 60664 KB | Output is correct |
51 | Correct | 48 ms | 43036 KB | Output is correct |
52 | Correct | 38 ms | 36600 KB | Output is correct |
53 | Correct | 51 ms | 53624 KB | Output is correct |
54 | Correct | 36 ms | 36608 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 24 ms | 23936 KB | Output is correct |
2 | Correct | 30 ms | 24192 KB | Output is correct |
3 | Correct | 27 ms | 24320 KB | Output is correct |
4 | Correct | 26 ms | 23936 KB | Output is correct |
5 | Correct | 33 ms | 23928 KB | Output is correct |
6 | Correct | 27 ms | 24192 KB | Output is correct |
7 | Correct | 25 ms | 23936 KB | Output is correct |
8 | Correct | 25 ms | 24284 KB | Output is correct |
9 | Correct | 23 ms | 23928 KB | Output is correct |
10 | Correct | 23 ms | 23808 KB | Output is correct |
11 | Correct | 26 ms | 24320 KB | Output is correct |
12 | Correct | 26 ms | 24184 KB | Output is correct |
13 | Correct | 21 ms | 23928 KB | Output is correct |
14 | Correct | 25 ms | 24192 KB | Output is correct |
15 | Correct | 27 ms | 24312 KB | Output is correct |
16 | Correct | 24 ms | 23936 KB | Output is correct |
17 | Correct | 25 ms | 23936 KB | Output is correct |
18 | Correct | 28 ms | 24320 KB | Output is correct |
19 | Correct | 28 ms | 23800 KB | Output is correct |
20 | Correct | 22 ms | 23808 KB | Output is correct |
21 | Correct | 25 ms | 24320 KB | Output is correct |
22 | Correct | 25 ms | 24184 KB | Output is correct |
23 | Correct | 30 ms | 23936 KB | Output is correct |
24 | Correct | 25 ms | 23936 KB | Output is correct |
25 | Correct | 22 ms | 24192 KB | Output is correct |
26 | Runtime error | 46 ms | 47480 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
27 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 45 ms | 47488 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 24 ms | 23936 KB | Output is correct |
2 | Correct | 30 ms | 24192 KB | Output is correct |
3 | Correct | 27 ms | 24320 KB | Output is correct |
4 | Correct | 26 ms | 23936 KB | Output is correct |
5 | Correct | 33 ms | 23928 KB | Output is correct |
6 | Correct | 27 ms | 24192 KB | Output is correct |
7 | Correct | 25 ms | 23936 KB | Output is correct |
8 | Correct | 25 ms | 24284 KB | Output is correct |
9 | Correct | 23 ms | 23928 KB | Output is correct |
10 | Correct | 23 ms | 23808 KB | Output is correct |
11 | Correct | 26 ms | 24320 KB | Output is correct |
12 | Correct | 26 ms | 24184 KB | Output is correct |
13 | Correct | 21 ms | 23928 KB | Output is correct |
14 | Correct | 25 ms | 24192 KB | Output is correct |
15 | Correct | 27 ms | 24312 KB | Output is correct |
16 | Correct | 24 ms | 23936 KB | Output is correct |
17 | Correct | 25 ms | 23936 KB | Output is correct |
18 | Correct | 28 ms | 24320 KB | Output is correct |
19 | Correct | 28 ms | 23800 KB | Output is correct |
20 | Correct | 22 ms | 23808 KB | Output is correct |
21 | Correct | 25 ms | 24320 KB | Output is correct |
22 | Correct | 25 ms | 24184 KB | Output is correct |
23 | Correct | 30 ms | 23936 KB | Output is correct |
24 | Correct | 25 ms | 23936 KB | Output is correct |
25 | Correct | 25 ms | 24056 KB | Output is correct |
26 | Correct | 84 ms | 59612 KB | Output is correct |
27 | Correct | 36 ms | 36600 KB | Output is correct |
28 | Correct | 83 ms | 60584 KB | Output is correct |
29 | Correct | 85 ms | 60536 KB | Output is correct |
30 | Correct | 45 ms | 38392 KB | Output is correct |
31 | Correct | 23 ms | 24064 KB | Output is correct |
32 | Correct | 69 ms | 60536 KB | Output is correct |
33 | Correct | 23 ms | 24164 KB | Output is correct |
34 | Correct | 38 ms | 36600 KB | Output is correct |
35 | Correct | 83 ms | 60536 KB | Output is correct |
36 | Correct | 38 ms | 36600 KB | Output is correct |
37 | Correct | 56 ms | 53624 KB | Output is correct |
38 | Correct | 68 ms | 24312 KB | Output is correct |
39 | Correct | 50 ms | 53624 KB | Output is correct |
40 | Correct | 35 ms | 36088 KB | Output is correct |
41 | Correct | 35 ms | 36096 KB | Output is correct |
42 | Correct | 57 ms | 53656 KB | Output is correct |
43 | Correct | 41 ms | 36216 KB | Output is correct |
44 | Correct | 26 ms | 24440 KB | Output is correct |
45 | Correct | 86 ms | 60536 KB | Output is correct |
46 | Correct | 51 ms | 53240 KB | Output is correct |
47 | Correct | 24 ms | 24320 KB | Output is correct |
48 | Correct | 45 ms | 43128 KB | Output is correct |
49 | Correct | 70 ms | 60536 KB | Output is correct |
50 | Correct | 87 ms | 60664 KB | Output is correct |
51 | Correct | 48 ms | 43036 KB | Output is correct |
52 | Correct | 38 ms | 36600 KB | Output is correct |
53 | Correct | 51 ms | 53624 KB | Output is correct |
54 | Correct | 36 ms | 36608 KB | Output is correct |
55 | Correct | 22 ms | 24192 KB | Output is correct |
56 | Runtime error | 46 ms | 47480 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
57 | Halted | 0 ms | 0 KB | - |