#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
const int LOG = 19;
int n, k, cnt;
int s[MAXN], par[MAXN][LOG], dub[MAXN], e[MAXN], ima[MAXN], bio[MAXN], deg[MAXN];
vector <int> v[MAXN], comp[MAXN];
void dfs (int x, int rod) {
par[x][0] = rod;
dub[x] = 1 + dub[rod];
for (auto sus : v[x]) {
if (sus == rod) continue;
dfs(sus, x);
}
}
void precompute () {
dfs(1, 0);
for (int i = 1; i < LOG; i++) {
for (int j = 1; j <= n; j++) {
par[j][i] = par[par[j][i - 1]][i - 1];
}
}
}
int digni (int x, int len) {
int pot = 0;
while (len > 0) {
if (len & 1) x = par[x][pot];
len /= 2;
pot++;
}
return x;
}
int lca (int a, int b) {
if (dub[a] < dub[b]) swap(a, b);
a = digni(a, dub[a] - dub[b]);
if (a == b) return a;
for (int i = LOG - 1; i >= 0; i--) {
if (par[a][i] != par[b][i]) {
a = par[a][i];
b = par[b][i];
}
}
return par[a][0];
}
void obradi (int c) {
int piv = comp[c][0];
for (auto x : comp[c]) {
if (x == piv) continue;
int g = lca(piv, x);
e[piv]++; e[x]++;
e[g] -= 2;
}
}
void nadi (int x, int rod) {
for (auto sus : v[x]) {
if (sus == rod) continue;
nadi(sus, x);
e[x] += e[sus];
}
if (rod != 0 && e[x] > 0) ima[x] = 1;
}
void oboji (int x) {
if (bio[x]) return;
bio[x] = cnt;
for (auto sus : v[x]) {
if (par[x][0] == sus && ima[x] || par[x][0] != sus && ima[sus]) oboji(sus);
}
}
void build () {
for (int i = 1; i <= n; i++) {
if (bio[i] == 0) {
cnt++;
oboji(i);
}
}
}
int solve () {
if (cnt == 1) return 0;
for (int i = 2; i <= n; i++) {
if (ima[i] == 0) {
deg[bio[i]]++;
deg[bio[par[i][0]]]++;
}
}
int sol = 0;
for (int i = 1; i <= cnt; i++) {
if (deg[i] == 1) sol++;
}
return (sol + 1) / 2;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
cin >> s[i];
comp[s[i]].push_back(i);
}
precompute();
for (int i = 1; i <= k; i++) obradi(i);
nadi(1, 0);
build();
cout << solve();
return 0;
}
Compilation message
mergers.cpp: In function 'void oboji(int)':
mergers.cpp:76:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
76 | if (par[x][0] == sus && ima[x] || par[x][0] != sus && ima[sus]) oboji(sus);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23912 KB |
Output is correct |
2 |
Correct |
20 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23912 KB |
Output is correct |
7 |
Correct |
20 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23936 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
16 ms |
23936 KB |
Output is correct |
13 |
Correct |
20 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23808 KB |
Output is correct |
17 |
Correct |
16 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23936 KB |
Output is correct |
19 |
Correct |
21 ms |
23808 KB |
Output is correct |
20 |
Correct |
20 ms |
23808 KB |
Output is correct |
21 |
Correct |
18 ms |
23936 KB |
Output is correct |
22 |
Correct |
17 ms |
23936 KB |
Output is correct |
23 |
Correct |
16 ms |
23808 KB |
Output is correct |
24 |
Correct |
17 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23912 KB |
Output is correct |
2 |
Correct |
20 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23912 KB |
Output is correct |
7 |
Correct |
20 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23936 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
16 ms |
23936 KB |
Output is correct |
13 |
Correct |
20 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23808 KB |
Output is correct |
17 |
Correct |
16 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23936 KB |
Output is correct |
19 |
Correct |
21 ms |
23808 KB |
Output is correct |
20 |
Correct |
20 ms |
23808 KB |
Output is correct |
21 |
Correct |
18 ms |
23936 KB |
Output is correct |
22 |
Correct |
17 ms |
23936 KB |
Output is correct |
23 |
Correct |
16 ms |
23808 KB |
Output is correct |
24 |
Correct |
17 ms |
23936 KB |
Output is correct |
25 |
Correct |
16 ms |
23936 KB |
Output is correct |
26 |
Correct |
19 ms |
24320 KB |
Output is correct |
27 |
Correct |
22 ms |
24320 KB |
Output is correct |
28 |
Correct |
22 ms |
24448 KB |
Output is correct |
29 |
Correct |
18 ms |
24448 KB |
Output is correct |
30 |
Correct |
18 ms |
24320 KB |
Output is correct |
31 |
Correct |
16 ms |
23936 KB |
Output is correct |
32 |
Correct |
19 ms |
24576 KB |
Output is correct |
33 |
Correct |
16 ms |
23936 KB |
Output is correct |
34 |
Correct |
22 ms |
24320 KB |
Output is correct |
35 |
Correct |
22 ms |
24404 KB |
Output is correct |
36 |
Correct |
22 ms |
24320 KB |
Output is correct |
37 |
Correct |
19 ms |
24320 KB |
Output is correct |
38 |
Correct |
16 ms |
23936 KB |
Output is correct |
39 |
Correct |
19 ms |
24320 KB |
Output is correct |
40 |
Correct |
22 ms |
24320 KB |
Output is correct |
41 |
Correct |
23 ms |
24320 KB |
Output is correct |
42 |
Correct |
18 ms |
24320 KB |
Output is correct |
43 |
Correct |
19 ms |
24448 KB |
Output is correct |
44 |
Correct |
17 ms |
23936 KB |
Output is correct |
45 |
Correct |
19 ms |
24448 KB |
Output is correct |
46 |
Correct |
22 ms |
24320 KB |
Output is correct |
47 |
Correct |
17 ms |
23936 KB |
Output is correct |
48 |
Correct |
22 ms |
24320 KB |
Output is correct |
49 |
Correct |
18 ms |
24320 KB |
Output is correct |
50 |
Correct |
20 ms |
24448 KB |
Output is correct |
51 |
Correct |
22 ms |
24312 KB |
Output is correct |
52 |
Correct |
18 ms |
24320 KB |
Output is correct |
53 |
Correct |
22 ms |
24320 KB |
Output is correct |
54 |
Correct |
19 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23912 KB |
Output is correct |
2 |
Correct |
20 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23912 KB |
Output is correct |
7 |
Correct |
20 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23936 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
16 ms |
23936 KB |
Output is correct |
13 |
Correct |
20 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23808 KB |
Output is correct |
17 |
Correct |
16 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23936 KB |
Output is correct |
19 |
Correct |
21 ms |
23808 KB |
Output is correct |
20 |
Correct |
20 ms |
23808 KB |
Output is correct |
21 |
Correct |
18 ms |
23936 KB |
Output is correct |
22 |
Correct |
17 ms |
23936 KB |
Output is correct |
23 |
Correct |
16 ms |
23808 KB |
Output is correct |
24 |
Correct |
17 ms |
23936 KB |
Output is correct |
25 |
Correct |
18 ms |
24064 KB |
Output is correct |
26 |
Correct |
107 ms |
38764 KB |
Output is correct |
27 |
Correct |
127 ms |
38392 KB |
Output is correct |
28 |
Correct |
18 ms |
24320 KB |
Output is correct |
29 |
Correct |
18 ms |
23936 KB |
Output is correct |
30 |
Correct |
17 ms |
23936 KB |
Output is correct |
31 |
Correct |
130 ms |
38392 KB |
Output is correct |
32 |
Correct |
18 ms |
24320 KB |
Output is correct |
33 |
Correct |
184 ms |
44152 KB |
Output is correct |
34 |
Correct |
146 ms |
38520 KB |
Output is correct |
35 |
Correct |
19 ms |
24320 KB |
Output is correct |
36 |
Correct |
159 ms |
38904 KB |
Output is correct |
37 |
Correct |
18 ms |
24320 KB |
Output is correct |
38 |
Correct |
21 ms |
24320 KB |
Output is correct |
39 |
Correct |
103 ms |
38792 KB |
Output is correct |
40 |
Correct |
18 ms |
24448 KB |
Output is correct |
41 |
Correct |
131 ms |
38388 KB |
Output is correct |
42 |
Correct |
215 ms |
40312 KB |
Output is correct |
43 |
Correct |
17 ms |
23936 KB |
Output is correct |
44 |
Correct |
173 ms |
44272 KB |
Output is correct |
45 |
Correct |
149 ms |
41336 KB |
Output is correct |
46 |
Correct |
21 ms |
24296 KB |
Output is correct |
47 |
Correct |
18 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
38764 KB |
Output is correct |
2 |
Correct |
96 ms |
41328 KB |
Output is correct |
3 |
Correct |
18 ms |
24320 KB |
Output is correct |
4 |
Correct |
19 ms |
24320 KB |
Output is correct |
5 |
Correct |
16 ms |
23936 KB |
Output is correct |
6 |
Correct |
16 ms |
23936 KB |
Output is correct |
7 |
Correct |
18 ms |
24320 KB |
Output is correct |
8 |
Correct |
146 ms |
39800 KB |
Output is correct |
9 |
Correct |
20 ms |
24320 KB |
Output is correct |
10 |
Correct |
128 ms |
38648 KB |
Output is correct |
11 |
Correct |
16 ms |
23936 KB |
Output is correct |
12 |
Correct |
138 ms |
38520 KB |
Output is correct |
13 |
Correct |
138 ms |
39800 KB |
Output is correct |
14 |
Correct |
126 ms |
41336 KB |
Output is correct |
15 |
Correct |
100 ms |
38640 KB |
Output is correct |
16 |
Correct |
18 ms |
24312 KB |
Output is correct |
17 |
Correct |
16 ms |
23936 KB |
Output is correct |
18 |
Correct |
114 ms |
41840 KB |
Output is correct |
19 |
Correct |
183 ms |
44408 KB |
Output is correct |
20 |
Correct |
18 ms |
24312 KB |
Output is correct |
21 |
Correct |
20 ms |
23936 KB |
Output is correct |
22 |
Correct |
124 ms |
40048 KB |
Output is correct |
23 |
Correct |
19 ms |
24320 KB |
Output is correct |
24 |
Correct |
151 ms |
39032 KB |
Output is correct |
25 |
Correct |
160 ms |
43424 KB |
Output is correct |
26 |
Correct |
22 ms |
24448 KB |
Output is correct |
27 |
Correct |
19 ms |
24440 KB |
Output is correct |
28 |
Correct |
19 ms |
24320 KB |
Output is correct |
29 |
Correct |
18 ms |
24320 KB |
Output is correct |
30 |
Correct |
20 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23912 KB |
Output is correct |
2 |
Correct |
20 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23912 KB |
Output is correct |
7 |
Correct |
20 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23936 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
16 ms |
23936 KB |
Output is correct |
13 |
Correct |
20 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23808 KB |
Output is correct |
17 |
Correct |
16 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23936 KB |
Output is correct |
19 |
Correct |
21 ms |
23808 KB |
Output is correct |
20 |
Correct |
20 ms |
23808 KB |
Output is correct |
21 |
Correct |
18 ms |
23936 KB |
Output is correct |
22 |
Correct |
17 ms |
23936 KB |
Output is correct |
23 |
Correct |
16 ms |
23808 KB |
Output is correct |
24 |
Correct |
17 ms |
23936 KB |
Output is correct |
25 |
Correct |
16 ms |
23936 KB |
Output is correct |
26 |
Correct |
19 ms |
24320 KB |
Output is correct |
27 |
Correct |
22 ms |
24320 KB |
Output is correct |
28 |
Correct |
22 ms |
24448 KB |
Output is correct |
29 |
Correct |
18 ms |
24448 KB |
Output is correct |
30 |
Correct |
18 ms |
24320 KB |
Output is correct |
31 |
Correct |
16 ms |
23936 KB |
Output is correct |
32 |
Correct |
19 ms |
24576 KB |
Output is correct |
33 |
Correct |
16 ms |
23936 KB |
Output is correct |
34 |
Correct |
22 ms |
24320 KB |
Output is correct |
35 |
Correct |
22 ms |
24404 KB |
Output is correct |
36 |
Correct |
22 ms |
24320 KB |
Output is correct |
37 |
Correct |
19 ms |
24320 KB |
Output is correct |
38 |
Correct |
16 ms |
23936 KB |
Output is correct |
39 |
Correct |
19 ms |
24320 KB |
Output is correct |
40 |
Correct |
22 ms |
24320 KB |
Output is correct |
41 |
Correct |
23 ms |
24320 KB |
Output is correct |
42 |
Correct |
18 ms |
24320 KB |
Output is correct |
43 |
Correct |
19 ms |
24448 KB |
Output is correct |
44 |
Correct |
17 ms |
23936 KB |
Output is correct |
45 |
Correct |
19 ms |
24448 KB |
Output is correct |
46 |
Correct |
22 ms |
24320 KB |
Output is correct |
47 |
Correct |
17 ms |
23936 KB |
Output is correct |
48 |
Correct |
22 ms |
24320 KB |
Output is correct |
49 |
Correct |
18 ms |
24320 KB |
Output is correct |
50 |
Correct |
20 ms |
24448 KB |
Output is correct |
51 |
Correct |
22 ms |
24312 KB |
Output is correct |
52 |
Correct |
18 ms |
24320 KB |
Output is correct |
53 |
Correct |
22 ms |
24320 KB |
Output is correct |
54 |
Correct |
19 ms |
24320 KB |
Output is correct |
55 |
Correct |
18 ms |
24064 KB |
Output is correct |
56 |
Correct |
107 ms |
38764 KB |
Output is correct |
57 |
Correct |
127 ms |
38392 KB |
Output is correct |
58 |
Correct |
18 ms |
24320 KB |
Output is correct |
59 |
Correct |
18 ms |
23936 KB |
Output is correct |
60 |
Correct |
17 ms |
23936 KB |
Output is correct |
61 |
Correct |
130 ms |
38392 KB |
Output is correct |
62 |
Correct |
18 ms |
24320 KB |
Output is correct |
63 |
Correct |
184 ms |
44152 KB |
Output is correct |
64 |
Correct |
146 ms |
38520 KB |
Output is correct |
65 |
Correct |
19 ms |
24320 KB |
Output is correct |
66 |
Correct |
159 ms |
38904 KB |
Output is correct |
67 |
Correct |
18 ms |
24320 KB |
Output is correct |
68 |
Correct |
21 ms |
24320 KB |
Output is correct |
69 |
Correct |
103 ms |
38792 KB |
Output is correct |
70 |
Correct |
18 ms |
24448 KB |
Output is correct |
71 |
Correct |
131 ms |
38388 KB |
Output is correct |
72 |
Correct |
215 ms |
40312 KB |
Output is correct |
73 |
Correct |
17 ms |
23936 KB |
Output is correct |
74 |
Correct |
173 ms |
44272 KB |
Output is correct |
75 |
Correct |
149 ms |
41336 KB |
Output is correct |
76 |
Correct |
21 ms |
24296 KB |
Output is correct |
77 |
Correct |
18 ms |
24320 KB |
Output is correct |
78 |
Correct |
93 ms |
38764 KB |
Output is correct |
79 |
Correct |
96 ms |
41328 KB |
Output is correct |
80 |
Correct |
18 ms |
24320 KB |
Output is correct |
81 |
Correct |
19 ms |
24320 KB |
Output is correct |
82 |
Correct |
16 ms |
23936 KB |
Output is correct |
83 |
Correct |
16 ms |
23936 KB |
Output is correct |
84 |
Correct |
18 ms |
24320 KB |
Output is correct |
85 |
Correct |
146 ms |
39800 KB |
Output is correct |
86 |
Correct |
20 ms |
24320 KB |
Output is correct |
87 |
Correct |
128 ms |
38648 KB |
Output is correct |
88 |
Correct |
16 ms |
23936 KB |
Output is correct |
89 |
Correct |
138 ms |
38520 KB |
Output is correct |
90 |
Correct |
138 ms |
39800 KB |
Output is correct |
91 |
Correct |
126 ms |
41336 KB |
Output is correct |
92 |
Correct |
100 ms |
38640 KB |
Output is correct |
93 |
Correct |
18 ms |
24312 KB |
Output is correct |
94 |
Correct |
16 ms |
23936 KB |
Output is correct |
95 |
Correct |
114 ms |
41840 KB |
Output is correct |
96 |
Correct |
183 ms |
44408 KB |
Output is correct |
97 |
Correct |
18 ms |
24312 KB |
Output is correct |
98 |
Correct |
20 ms |
23936 KB |
Output is correct |
99 |
Correct |
124 ms |
40048 KB |
Output is correct |
100 |
Correct |
19 ms |
24320 KB |
Output is correct |
101 |
Correct |
151 ms |
39032 KB |
Output is correct |
102 |
Correct |
160 ms |
43424 KB |
Output is correct |
103 |
Correct |
22 ms |
24448 KB |
Output is correct |
104 |
Correct |
19 ms |
24440 KB |
Output is correct |
105 |
Correct |
19 ms |
24320 KB |
Output is correct |
106 |
Correct |
18 ms |
24320 KB |
Output is correct |
107 |
Correct |
20 ms |
24320 KB |
Output is correct |
108 |
Correct |
721 ms |
100348 KB |
Output is correct |
109 |
Correct |
1273 ms |
109288 KB |
Output is correct |
110 |
Correct |
1253 ms |
118372 KB |
Output is correct |
111 |
Correct |
1193 ms |
127928 KB |
Output is correct |
112 |
Correct |
1098 ms |
119496 KB |
Output is correct |
113 |
Correct |
775 ms |
109292 KB |
Output is correct |
114 |
Correct |
915 ms |
94572 KB |
Output is correct |
115 |
Correct |
928 ms |
94436 KB |
Output is correct |
116 |
Correct |
1123 ms |
96416 KB |
Output is correct |
117 |
Correct |
881 ms |
107432 KB |
Output is correct |
118 |
Correct |
925 ms |
94200 KB |
Output is correct |
119 |
Correct |
887 ms |
107408 KB |
Output is correct |
120 |
Correct |
1002 ms |
118520 KB |
Output is correct |
121 |
Correct |
926 ms |
109160 KB |
Output is correct |
122 |
Correct |
1047 ms |
101276 KB |
Output is correct |
123 |
Correct |
627 ms |
106212 KB |
Output is correct |
124 |
Correct |
1033 ms |
106364 KB |
Output is correct |