# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
128922 | 2019-07-11T10:57:48 Z | win11905 | Mergers (JOI19_mergers) | C++11 | 887 ms | 91068 KB |
#include <bits/stdc++.h> #define pii pair<int, int> #define x first #define y second using namespace std; const int N = 5e5+5; int n, k; vector<int> g[N]; vector<pii> E; int dep[N], dsu[N], deg[N], par[N]; vector<int> col[N]; int find(int u) { return dsu[u] = dsu[u] == u ? u : find(dsu[u]); } void init_dep(int u, int p) { dep[u] = dep[p] + 1, par[u] = p; for(int v : g[u]) if(v != p) init_dep(v, u); } int main() { iota(dsu, dsu+N, 0); scanf("%d %d", &n, &k); for(int i = 1, u, v; i < n; ++i) { scanf("%d %d", &u, &v); g[u].emplace_back(v), g[v].emplace_back(u); E.emplace_back(u, v); } for(int i = 1, v; i <= n; ++i) { scanf("%d", &v); col[v].emplace_back(i); } init_dep(1, 0); for(int i = 1; i <= k; ++i) if(col[i].size() > 1) { for(int j = 1; j < col[i].size(); ++j) { int a = find(col[i][j-1]), b = find(col[i][j]); while(a != b) { if(dep[a] < dep[b]) swap(a, b); dsu[a] = find(par[a]); a = dsu[a]; } } } for(auto x : E) { int a = find(x.x), b = find(x.y); if(a != b) deg[a]++, deg[b]++; } int cnt = 0; for(int i = 1; i <= n; ++i) if(deg[i] == 1) cnt++; printf("%d\n", (cnt+1) / 2); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 25852 KB | Output is correct |
2 | Correct | 25 ms | 25848 KB | Output is correct |
3 | Correct | 25 ms | 25720 KB | Output is correct |
4 | Correct | 25 ms | 25848 KB | Output is correct |
5 | Correct | 25 ms | 25720 KB | Output is correct |
6 | Correct | 25 ms | 25848 KB | Output is correct |
7 | Correct | 25 ms | 25848 KB | Output is correct |
8 | Correct | 25 ms | 25848 KB | Output is correct |
9 | Correct | 25 ms | 25852 KB | Output is correct |
10 | Correct | 25 ms | 25720 KB | Output is correct |
11 | Correct | 25 ms | 25848 KB | Output is correct |
12 | Correct | 25 ms | 25848 KB | Output is correct |
13 | Correct | 31 ms | 25860 KB | Output is correct |
14 | Correct | 25 ms | 25848 KB | Output is correct |
15 | Correct | 25 ms | 25720 KB | Output is correct |
16 | Correct | 27 ms | 25852 KB | Output is correct |
17 | Correct | 29 ms | 25848 KB | Output is correct |
18 | Correct | 26 ms | 25720 KB | Output is correct |
19 | Correct | 31 ms | 25848 KB | Output is correct |
20 | Correct | 25 ms | 25848 KB | Output is correct |
21 | Correct | 31 ms | 25808 KB | Output is correct |
22 | Correct | 26 ms | 25720 KB | Output is correct |
23 | Correct | 25 ms | 25848 KB | Output is correct |
24 | Correct | 25 ms | 25848 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 25852 KB | Output is correct |
2 | Correct | 25 ms | 25848 KB | Output is correct |
3 | Correct | 25 ms | 25720 KB | Output is correct |
4 | Correct | 25 ms | 25848 KB | Output is correct |
5 | Correct | 25 ms | 25720 KB | Output is correct |
6 | Correct | 25 ms | 25848 KB | Output is correct |
7 | Correct | 25 ms | 25848 KB | Output is correct |
8 | Correct | 25 ms | 25848 KB | Output is correct |
9 | Correct | 25 ms | 25852 KB | Output is correct |
10 | Correct | 25 ms | 25720 KB | Output is correct |
11 | Correct | 25 ms | 25848 KB | Output is correct |
12 | Correct | 25 ms | 25848 KB | Output is correct |
13 | Correct | 31 ms | 25860 KB | Output is correct |
14 | Correct | 25 ms | 25848 KB | Output is correct |
15 | Correct | 25 ms | 25720 KB | Output is correct |
16 | Correct | 27 ms | 25852 KB | Output is correct |
17 | Correct | 29 ms | 25848 KB | Output is correct |
18 | Correct | 26 ms | 25720 KB | Output is correct |
19 | Correct | 31 ms | 25848 KB | Output is correct |
20 | Correct | 25 ms | 25848 KB | Output is correct |
21 | Correct | 31 ms | 25808 KB | Output is correct |
22 | Correct | 26 ms | 25720 KB | Output is correct |
23 | Correct | 25 ms | 25848 KB | Output is correct |
24 | Correct | 25 ms | 25848 KB | Output is correct |
25 | Correct | 24 ms | 25720 KB | Output is correct |
26 | Correct | 27 ms | 26020 KB | Output is correct |
27 | Correct | 26 ms | 25976 KB | Output is correct |
28 | Correct | 27 ms | 26076 KB | Output is correct |
29 | Correct | 27 ms | 25976 KB | Output is correct |
30 | Correct | 27 ms | 25976 KB | Output is correct |
31 | Correct | 25 ms | 25848 KB | Output is correct |
32 | Correct | 27 ms | 26104 KB | Output is correct |
33 | Correct | 25 ms | 25848 KB | Output is correct |
34 | Correct | 26 ms | 25976 KB | Output is correct |
35 | Correct | 27 ms | 25976 KB | Output is correct |
36 | Correct | 27 ms | 26104 KB | Output is correct |
37 | Correct | 27 ms | 25980 KB | Output is correct |
38 | Correct | 25 ms | 25848 KB | Output is correct |
39 | Correct | 27 ms | 25976 KB | Output is correct |
40 | Correct | 27 ms | 25976 KB | Output is correct |
41 | Correct | 27 ms | 25976 KB | Output is correct |
42 | Correct | 27 ms | 25976 KB | Output is correct |
43 | Correct | 27 ms | 26104 KB | Output is correct |
44 | Correct | 25 ms | 25720 KB | Output is correct |
45 | Correct | 27 ms | 25976 KB | Output is correct |
46 | Correct | 27 ms | 25976 KB | Output is correct |
47 | Correct | 25 ms | 25980 KB | Output is correct |
48 | Correct | 29 ms | 26104 KB | Output is correct |
49 | Correct | 27 ms | 25976 KB | Output is correct |
50 | Correct | 30 ms | 26104 KB | Output is correct |
51 | Correct | 28 ms | 25976 KB | Output is correct |
52 | Correct | 27 ms | 25976 KB | Output is correct |
53 | Correct | 26 ms | 25976 KB | Output is correct |
54 | Correct | 27 ms | 25976 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 25852 KB | Output is correct |
2 | Correct | 25 ms | 25848 KB | Output is correct |
3 | Correct | 25 ms | 25720 KB | Output is correct |
4 | Correct | 25 ms | 25848 KB | Output is correct |
5 | Correct | 25 ms | 25720 KB | Output is correct |
6 | Correct | 25 ms | 25848 KB | Output is correct |
7 | Correct | 25 ms | 25848 KB | Output is correct |
8 | Correct | 25 ms | 25848 KB | Output is correct |
9 | Correct | 25 ms | 25852 KB | Output is correct |
10 | Correct | 25 ms | 25720 KB | Output is correct |
11 | Correct | 25 ms | 25848 KB | Output is correct |
12 | Correct | 25 ms | 25848 KB | Output is correct |
13 | Correct | 31 ms | 25860 KB | Output is correct |
14 | Correct | 25 ms | 25848 KB | Output is correct |
15 | Correct | 25 ms | 25720 KB | Output is correct |
16 | Correct | 27 ms | 25852 KB | Output is correct |
17 | Correct | 29 ms | 25848 KB | Output is correct |
18 | Correct | 26 ms | 25720 KB | Output is correct |
19 | Correct | 31 ms | 25848 KB | Output is correct |
20 | Correct | 25 ms | 25848 KB | Output is correct |
21 | Correct | 31 ms | 25808 KB | Output is correct |
22 | Correct | 26 ms | 25720 KB | Output is correct |
23 | Correct | 25 ms | 25848 KB | Output is correct |
24 | Correct | 25 ms | 25848 KB | Output is correct |
25 | Correct | 25 ms | 25720 KB | Output is correct |
26 | Correct | 95 ms | 31364 KB | Output is correct |
27 | Correct | 120 ms | 31152 KB | Output is correct |
28 | Correct | 32 ms | 25976 KB | Output is correct |
29 | Correct | 28 ms | 25720 KB | Output is correct |
30 | Correct | 30 ms | 25720 KB | Output is correct |
31 | Correct | 136 ms | 31204 KB | Output is correct |
32 | Correct | 26 ms | 25976 KB | Output is correct |
33 | Correct | 135 ms | 35448 KB | Output is correct |
34 | Correct | 114 ms | 31160 KB | Output is correct |
35 | Correct | 27 ms | 25976 KB | Output is correct |
36 | Correct | 130 ms | 31596 KB | Output is correct |
37 | Correct | 26 ms | 25976 KB | Output is correct |
38 | Correct | 26 ms | 25976 KB | Output is correct |
39 | Correct | 93 ms | 31340 KB | Output is correct |
40 | Correct | 26 ms | 26104 KB | Output is correct |
41 | Correct | 109 ms | 31468 KB | Output is correct |
42 | Correct | 123 ms | 32492 KB | Output is correct |
43 | Correct | 24 ms | 25720 KB | Output is correct |
44 | Correct | 131 ms | 35948 KB | Output is correct |
45 | Correct | 124 ms | 33256 KB | Output is correct |
46 | Correct | 26 ms | 25976 KB | Output is correct |
47 | Correct | 26 ms | 25976 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 95 ms | 31456 KB | Output is correct |
2 | Correct | 109 ms | 34436 KB | Output is correct |
3 | Correct | 27 ms | 25976 KB | Output is correct |
4 | Correct | 27 ms | 25976 KB | Output is correct |
5 | Correct | 25 ms | 25820 KB | Output is correct |
6 | Correct | 24 ms | 25768 KB | Output is correct |
7 | Correct | 27 ms | 25992 KB | Output is correct |
8 | Correct | 146 ms | 32572 KB | Output is correct |
9 | Correct | 27 ms | 25976 KB | Output is correct |
10 | Correct | 122 ms | 31536 KB | Output is correct |
11 | Correct | 25 ms | 25840 KB | Output is correct |
12 | Correct | 134 ms | 31512 KB | Output is correct |
13 | Correct | 144 ms | 32432 KB | Output is correct |
14 | Correct | 143 ms | 34112 KB | Output is correct |
15 | Correct | 100 ms | 31436 KB | Output is correct |
16 | Correct | 27 ms | 25976 KB | Output is correct |
17 | Correct | 24 ms | 25720 KB | Output is correct |
18 | Correct | 127 ms | 34024 KB | Output is correct |
19 | Correct | 143 ms | 36432 KB | Output is correct |
20 | Correct | 27 ms | 25956 KB | Output is correct |
21 | Correct | 24 ms | 25720 KB | Output is correct |
22 | Correct | 112 ms | 34524 KB | Output is correct |
23 | Correct | 27 ms | 25976 KB | Output is correct |
24 | Correct | 135 ms | 33356 KB | Output is correct |
25 | Correct | 147 ms | 37340 KB | Output is correct |
26 | Correct | 27 ms | 26212 KB | Output is correct |
27 | Correct | 27 ms | 26084 KB | Output is correct |
28 | Correct | 27 ms | 25964 KB | Output is correct |
29 | Correct | 26 ms | 25964 KB | Output is correct |
30 | Correct | 27 ms | 26076 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 25852 KB | Output is correct |
2 | Correct | 25 ms | 25848 KB | Output is correct |
3 | Correct | 25 ms | 25720 KB | Output is correct |
4 | Correct | 25 ms | 25848 KB | Output is correct |
5 | Correct | 25 ms | 25720 KB | Output is correct |
6 | Correct | 25 ms | 25848 KB | Output is correct |
7 | Correct | 25 ms | 25848 KB | Output is correct |
8 | Correct | 25 ms | 25848 KB | Output is correct |
9 | Correct | 25 ms | 25852 KB | Output is correct |
10 | Correct | 25 ms | 25720 KB | Output is correct |
11 | Correct | 25 ms | 25848 KB | Output is correct |
12 | Correct | 25 ms | 25848 KB | Output is correct |
13 | Correct | 31 ms | 25860 KB | Output is correct |
14 | Correct | 25 ms | 25848 KB | Output is correct |
15 | Correct | 25 ms | 25720 KB | Output is correct |
16 | Correct | 27 ms | 25852 KB | Output is correct |
17 | Correct | 29 ms | 25848 KB | Output is correct |
18 | Correct | 26 ms | 25720 KB | Output is correct |
19 | Correct | 31 ms | 25848 KB | Output is correct |
20 | Correct | 25 ms | 25848 KB | Output is correct |
21 | Correct | 31 ms | 25808 KB | Output is correct |
22 | Correct | 26 ms | 25720 KB | Output is correct |
23 | Correct | 25 ms | 25848 KB | Output is correct |
24 | Correct | 25 ms | 25848 KB | Output is correct |
25 | Correct | 24 ms | 25720 KB | Output is correct |
26 | Correct | 27 ms | 26020 KB | Output is correct |
27 | Correct | 26 ms | 25976 KB | Output is correct |
28 | Correct | 27 ms | 26076 KB | Output is correct |
29 | Correct | 27 ms | 25976 KB | Output is correct |
30 | Correct | 27 ms | 25976 KB | Output is correct |
31 | Correct | 25 ms | 25848 KB | Output is correct |
32 | Correct | 27 ms | 26104 KB | Output is correct |
33 | Correct | 25 ms | 25848 KB | Output is correct |
34 | Correct | 26 ms | 25976 KB | Output is correct |
35 | Correct | 27 ms | 25976 KB | Output is correct |
36 | Correct | 27 ms | 26104 KB | Output is correct |
37 | Correct | 27 ms | 25980 KB | Output is correct |
38 | Correct | 25 ms | 25848 KB | Output is correct |
39 | Correct | 27 ms | 25976 KB | Output is correct |
40 | Correct | 27 ms | 25976 KB | Output is correct |
41 | Correct | 27 ms | 25976 KB | Output is correct |
42 | Correct | 27 ms | 25976 KB | Output is correct |
43 | Correct | 27 ms | 26104 KB | Output is correct |
44 | Correct | 25 ms | 25720 KB | Output is correct |
45 | Correct | 27 ms | 25976 KB | Output is correct |
46 | Correct | 27 ms | 25976 KB | Output is correct |
47 | Correct | 25 ms | 25980 KB | Output is correct |
48 | Correct | 29 ms | 26104 KB | Output is correct |
49 | Correct | 27 ms | 25976 KB | Output is correct |
50 | Correct | 30 ms | 26104 KB | Output is correct |
51 | Correct | 28 ms | 25976 KB | Output is correct |
52 | Correct | 27 ms | 25976 KB | Output is correct |
53 | Correct | 26 ms | 25976 KB | Output is correct |
54 | Correct | 27 ms | 25976 KB | Output is correct |
55 | Correct | 25 ms | 25720 KB | Output is correct |
56 | Correct | 95 ms | 31364 KB | Output is correct |
57 | Correct | 120 ms | 31152 KB | Output is correct |
58 | Correct | 32 ms | 25976 KB | Output is correct |
59 | Correct | 28 ms | 25720 KB | Output is correct |
60 | Correct | 30 ms | 25720 KB | Output is correct |
61 | Correct | 136 ms | 31204 KB | Output is correct |
62 | Correct | 26 ms | 25976 KB | Output is correct |
63 | Correct | 135 ms | 35448 KB | Output is correct |
64 | Correct | 114 ms | 31160 KB | Output is correct |
65 | Correct | 27 ms | 25976 KB | Output is correct |
66 | Correct | 130 ms | 31596 KB | Output is correct |
67 | Correct | 26 ms | 25976 KB | Output is correct |
68 | Correct | 26 ms | 25976 KB | Output is correct |
69 | Correct | 93 ms | 31340 KB | Output is correct |
70 | Correct | 26 ms | 26104 KB | Output is correct |
71 | Correct | 109 ms | 31468 KB | Output is correct |
72 | Correct | 123 ms | 32492 KB | Output is correct |
73 | Correct | 24 ms | 25720 KB | Output is correct |
74 | Correct | 131 ms | 35948 KB | Output is correct |
75 | Correct | 124 ms | 33256 KB | Output is correct |
76 | Correct | 26 ms | 25976 KB | Output is correct |
77 | Correct | 26 ms | 25976 KB | Output is correct |
78 | Correct | 95 ms | 31456 KB | Output is correct |
79 | Correct | 109 ms | 34436 KB | Output is correct |
80 | Correct | 27 ms | 25976 KB | Output is correct |
81 | Correct | 27 ms | 25976 KB | Output is correct |
82 | Correct | 25 ms | 25820 KB | Output is correct |
83 | Correct | 24 ms | 25768 KB | Output is correct |
84 | Correct | 27 ms | 25992 KB | Output is correct |
85 | Correct | 146 ms | 32572 KB | Output is correct |
86 | Correct | 27 ms | 25976 KB | Output is correct |
87 | Correct | 122 ms | 31536 KB | Output is correct |
88 | Correct | 25 ms | 25840 KB | Output is correct |
89 | Correct | 134 ms | 31512 KB | Output is correct |
90 | Correct | 144 ms | 32432 KB | Output is correct |
91 | Correct | 143 ms | 34112 KB | Output is correct |
92 | Correct | 100 ms | 31436 KB | Output is correct |
93 | Correct | 27 ms | 25976 KB | Output is correct |
94 | Correct | 24 ms | 25720 KB | Output is correct |
95 | Correct | 127 ms | 34024 KB | Output is correct |
96 | Correct | 143 ms | 36432 KB | Output is correct |
97 | Correct | 27 ms | 25956 KB | Output is correct |
98 | Correct | 24 ms | 25720 KB | Output is correct |
99 | Correct | 112 ms | 34524 KB | Output is correct |
100 | Correct | 27 ms | 25976 KB | Output is correct |
101 | Correct | 135 ms | 33356 KB | Output is correct |
102 | Correct | 147 ms | 37340 KB | Output is correct |
103 | Correct | 27 ms | 26212 KB | Output is correct |
104 | Correct | 27 ms | 26084 KB | Output is correct |
105 | Correct | 27 ms | 25964 KB | Output is correct |
106 | Correct | 26 ms | 25964 KB | Output is correct |
107 | Correct | 27 ms | 26076 KB | Output is correct |
108 | Correct | 629 ms | 69580 KB | Output is correct |
109 | Correct | 735 ms | 71696 KB | Output is correct |
110 | Correct | 752 ms | 78752 KB | Output is correct |
111 | Correct | 850 ms | 91068 KB | Output is correct |
112 | Correct | 887 ms | 85152 KB | Output is correct |
113 | Correct | 698 ms | 77312 KB | Output is correct |
114 | Correct | 634 ms | 60792 KB | Output is correct |
115 | Correct | 611 ms | 61356 KB | Output is correct |
116 | Correct | 782 ms | 65828 KB | Output is correct |
117 | Correct | 776 ms | 77304 KB | Output is correct |
118 | Correct | 690 ms | 62268 KB | Output is correct |
119 | Correct | 793 ms | 77364 KB | Output is correct |
120 | Correct | 818 ms | 85500 KB | Output is correct |
121 | Correct | 800 ms | 77296 KB | Output is correct |
122 | Correct | 833 ms | 71064 KB | Output is correct |
123 | Correct | 586 ms | 78832 KB | Output is correct |
124 | Correct | 836 ms | 76080 KB | Output is correct |