#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 5e5 + 10;
vector<int> adj[MAXN];
vector<int> adj2[MAXN];
int s[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
vector<int> e;
int dep[MAXN];
int tin[MAXN], tout[MAXN];
int tim = 0;
void dfs1(int node, int prev, int cur) {
e.push_back(node);
dep[node] = cur;
tin[node] = ++tim;
for(int x: adj[node]) {
if(x != prev) {
dfs1(x, node, cur + 1);
e.push_back(node);
}
}
tout[node] = ++tim;
}
pair<int, int> st[20][2*MAXN];
int l[MAXN], r[MAXN];
int lca(int x, int y) {
int m1 = min(l[x], l[y]);
int m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
return min(st[k][m1], st[k][m2 - (1<<k) + 1]).second;
}
int delta[MAXN];
bool cmp(int a, int b) {
return tin[a] < tin[b];
}
map<pair<int, int>, bool> lit;
int dfs2(int node, int prev) {
int s = delta[node];
for(int x: adj[node]) {
if(x != prev) s += dfs2(x, node);
}
if(s == 0) lit[{node, prev}] = lit[{prev, node}] = 1;
return s;
}
int32_t main() {
int n,k;cin >> n >> k;
for(int i=1;i<n;i++) {
int a,b;cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1; i<=n; i++) cin >> s[i];
for(int i=0; i<=n; i++) dsu[i] = i;
dfs1(1, -1, 0);
for(int i=0; i<e.size(); i++) r[e[i]] = i;
for(int i=e.size()-1; i>=0; i--) l[e[i]] = i;
for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
for(int i=1; i<20; i++) {
for(int j=0; j<e.size(); j++) {
if(j + (1<<i) - 1 < e.size()) {
st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
}
}
}
vector<int> wow[k+1];
for(int i=1; i<=n; i++) {
wow[s[i]].push_back(i);
}
for(int i=1; i<=k; i++) {
if(wow[i].size() == 1) continue;
sort(wow[i].begin(), wow[i].end(), cmp);
int l = lca(wow[i][0], wow[i][1]);
for(int j=0; j<wow[i].size(); j++) {
l = lca(l, wow[i][j]);
delta[wow[i][j]]++;
}
delta[l] -= wow[i].size();
}
dfs2(1, -1);
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(!lit[{i, x}]) {
union_(i, x);
}
}
}
map<pair<int, int>, bool> mp;
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && mp[{set_of(i), set_of(x)}] == 0) {
mp[{set_of(i), set_of(x)}] = 1;
mp[{set_of(x), set_of(i)}] = 1;
adj2[set_of(i)].push_back(set_of(x));
adj2[set_of(x)].push_back(set_of(i));
}
}
}
int s = 0;
for(int i=1; i<=n; i++) {
s += (adj2[i].size() == 1);
}
cout << (s + 1) / 2 << "\n";
}
Compilation message
mergers.cpp: In function 'int32_t main()':
mergers.cpp:63:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=0; i<e.size(); i++) r[e[i]] = i;
| ~^~~~~~~~~
mergers.cpp:65:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
| ~^~~~~~~~~
mergers.cpp:67:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j=0; j<e.size(); j++) {
| ~^~~~~~~~~
mergers.cpp:68:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | if(j + (1<<i) - 1 < e.size()) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~
mergers.cpp:81:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int j=0; j<wow[i].size(); j++) {
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23908 KB |
Output is correct |
3 |
Correct |
12 ms |
23884 KB |
Output is correct |
4 |
Correct |
12 ms |
23936 KB |
Output is correct |
5 |
Correct |
12 ms |
23796 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
14 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23884 KB |
Output is correct |
10 |
Correct |
14 ms |
23784 KB |
Output is correct |
11 |
Correct |
12 ms |
23904 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23864 KB |
Output is correct |
15 |
Correct |
14 ms |
23876 KB |
Output is correct |
16 |
Correct |
12 ms |
23780 KB |
Output is correct |
17 |
Correct |
13 ms |
23788 KB |
Output is correct |
18 |
Correct |
12 ms |
23904 KB |
Output is correct |
19 |
Correct |
12 ms |
23884 KB |
Output is correct |
20 |
Correct |
12 ms |
23776 KB |
Output is correct |
21 |
Correct |
11 ms |
23876 KB |
Output is correct |
22 |
Correct |
12 ms |
23884 KB |
Output is correct |
23 |
Correct |
12 ms |
23808 KB |
Output is correct |
24 |
Correct |
11 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23908 KB |
Output is correct |
3 |
Correct |
12 ms |
23884 KB |
Output is correct |
4 |
Correct |
12 ms |
23936 KB |
Output is correct |
5 |
Correct |
12 ms |
23796 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
14 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23884 KB |
Output is correct |
10 |
Correct |
14 ms |
23784 KB |
Output is correct |
11 |
Correct |
12 ms |
23904 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23864 KB |
Output is correct |
15 |
Correct |
14 ms |
23876 KB |
Output is correct |
16 |
Correct |
12 ms |
23780 KB |
Output is correct |
17 |
Correct |
13 ms |
23788 KB |
Output is correct |
18 |
Correct |
12 ms |
23904 KB |
Output is correct |
19 |
Correct |
12 ms |
23884 KB |
Output is correct |
20 |
Correct |
12 ms |
23776 KB |
Output is correct |
21 |
Correct |
11 ms |
23876 KB |
Output is correct |
22 |
Correct |
12 ms |
23884 KB |
Output is correct |
23 |
Correct |
12 ms |
23808 KB |
Output is correct |
24 |
Correct |
11 ms |
23788 KB |
Output is correct |
25 |
Correct |
11 ms |
23924 KB |
Output is correct |
26 |
Correct |
20 ms |
26060 KB |
Output is correct |
27 |
Correct |
22 ms |
25848 KB |
Output is correct |
28 |
Correct |
20 ms |
26320 KB |
Output is correct |
29 |
Correct |
20 ms |
26444 KB |
Output is correct |
30 |
Correct |
18 ms |
25772 KB |
Output is correct |
31 |
Correct |
13 ms |
23884 KB |
Output is correct |
32 |
Correct |
24 ms |
26200 KB |
Output is correct |
33 |
Correct |
14 ms |
23924 KB |
Output is correct |
34 |
Correct |
17 ms |
25808 KB |
Output is correct |
35 |
Correct |
21 ms |
26356 KB |
Output is correct |
36 |
Correct |
17 ms |
25708 KB |
Output is correct |
37 |
Correct |
18 ms |
25924 KB |
Output is correct |
38 |
Correct |
13 ms |
23924 KB |
Output is correct |
39 |
Correct |
19 ms |
26060 KB |
Output is correct |
40 |
Correct |
16 ms |
25732 KB |
Output is correct |
41 |
Correct |
17 ms |
25796 KB |
Output is correct |
42 |
Correct |
18 ms |
25904 KB |
Output is correct |
43 |
Correct |
16 ms |
26080 KB |
Output is correct |
44 |
Correct |
12 ms |
23948 KB |
Output is correct |
45 |
Correct |
17 ms |
25988 KB |
Output is correct |
46 |
Correct |
18 ms |
25848 KB |
Output is correct |
47 |
Correct |
13 ms |
23884 KB |
Output is correct |
48 |
Correct |
20 ms |
25856 KB |
Output is correct |
49 |
Correct |
20 ms |
26372 KB |
Output is correct |
50 |
Correct |
21 ms |
26496 KB |
Output is correct |
51 |
Correct |
18 ms |
25900 KB |
Output is correct |
52 |
Correct |
17 ms |
25832 KB |
Output is correct |
53 |
Correct |
18 ms |
26000 KB |
Output is correct |
54 |
Correct |
18 ms |
25752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23908 KB |
Output is correct |
3 |
Correct |
12 ms |
23884 KB |
Output is correct |
4 |
Correct |
12 ms |
23936 KB |
Output is correct |
5 |
Correct |
12 ms |
23796 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
14 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23884 KB |
Output is correct |
10 |
Correct |
14 ms |
23784 KB |
Output is correct |
11 |
Correct |
12 ms |
23904 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23864 KB |
Output is correct |
15 |
Correct |
14 ms |
23876 KB |
Output is correct |
16 |
Correct |
12 ms |
23780 KB |
Output is correct |
17 |
Correct |
13 ms |
23788 KB |
Output is correct |
18 |
Correct |
12 ms |
23904 KB |
Output is correct |
19 |
Correct |
12 ms |
23884 KB |
Output is correct |
20 |
Correct |
12 ms |
23776 KB |
Output is correct |
21 |
Correct |
11 ms |
23876 KB |
Output is correct |
22 |
Correct |
12 ms |
23884 KB |
Output is correct |
23 |
Correct |
12 ms |
23808 KB |
Output is correct |
24 |
Correct |
11 ms |
23788 KB |
Output is correct |
25 |
Correct |
12 ms |
23884 KB |
Output is correct |
26 |
Correct |
239 ms |
102856 KB |
Output is correct |
27 |
Correct |
205 ms |
102604 KB |
Output is correct |
28 |
Correct |
18 ms |
25724 KB |
Output is correct |
29 |
Correct |
13 ms |
23884 KB |
Output is correct |
30 |
Correct |
13 ms |
23920 KB |
Output is correct |
31 |
Correct |
207 ms |
102488 KB |
Output is correct |
32 |
Correct |
16 ms |
25804 KB |
Output is correct |
33 |
Correct |
215 ms |
112132 KB |
Output is correct |
34 |
Correct |
207 ms |
102644 KB |
Output is correct |
35 |
Correct |
17 ms |
25804 KB |
Output is correct |
36 |
Correct |
220 ms |
103088 KB |
Output is correct |
37 |
Correct |
16 ms |
25724 KB |
Output is correct |
38 |
Correct |
17 ms |
25796 KB |
Output is correct |
39 |
Correct |
237 ms |
103100 KB |
Output is correct |
40 |
Correct |
16 ms |
26052 KB |
Output is correct |
41 |
Correct |
203 ms |
102408 KB |
Output is correct |
42 |
Correct |
235 ms |
105284 KB |
Output is correct |
43 |
Correct |
13 ms |
23920 KB |
Output is correct |
44 |
Correct |
220 ms |
112448 KB |
Output is correct |
45 |
Correct |
205 ms |
108496 KB |
Output is correct |
46 |
Correct |
17 ms |
25804 KB |
Output is correct |
47 |
Correct |
22 ms |
25708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
101344 KB |
Output is correct |
2 |
Correct |
416 ms |
121624 KB |
Output is correct |
3 |
Correct |
19 ms |
26052 KB |
Output is correct |
4 |
Correct |
17 ms |
25836 KB |
Output is correct |
5 |
Correct |
14 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
16 ms |
25808 KB |
Output is correct |
8 |
Correct |
254 ms |
106788 KB |
Output is correct |
9 |
Correct |
17 ms |
25732 KB |
Output is correct |
10 |
Correct |
223 ms |
102984 KB |
Output is correct |
11 |
Correct |
14 ms |
23860 KB |
Output is correct |
12 |
Correct |
209 ms |
102500 KB |
Output is correct |
13 |
Correct |
275 ms |
108180 KB |
Output is correct |
14 |
Correct |
415 ms |
122968 KB |
Output is correct |
15 |
Correct |
205 ms |
103124 KB |
Output is correct |
16 |
Correct |
19 ms |
25972 KB |
Output is correct |
17 |
Correct |
13 ms |
23888 KB |
Output is correct |
18 |
Correct |
382 ms |
120440 KB |
Output is correct |
19 |
Correct |
429 ms |
127348 KB |
Output is correct |
20 |
Correct |
18 ms |
25848 KB |
Output is correct |
21 |
Correct |
13 ms |
23888 KB |
Output is correct |
22 |
Correct |
265 ms |
108772 KB |
Output is correct |
23 |
Correct |
18 ms |
25808 KB |
Output is correct |
24 |
Correct |
235 ms |
104972 KB |
Output is correct |
25 |
Correct |
440 ms |
127416 KB |
Output is correct |
26 |
Correct |
19 ms |
26448 KB |
Output is correct |
27 |
Correct |
20 ms |
26484 KB |
Output is correct |
28 |
Correct |
17 ms |
25812 KB |
Output is correct |
29 |
Correct |
18 ms |
25820 KB |
Output is correct |
30 |
Correct |
18 ms |
25904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23908 KB |
Output is correct |
3 |
Correct |
12 ms |
23884 KB |
Output is correct |
4 |
Correct |
12 ms |
23936 KB |
Output is correct |
5 |
Correct |
12 ms |
23796 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
14 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23884 KB |
Output is correct |
10 |
Correct |
14 ms |
23784 KB |
Output is correct |
11 |
Correct |
12 ms |
23904 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23864 KB |
Output is correct |
15 |
Correct |
14 ms |
23876 KB |
Output is correct |
16 |
Correct |
12 ms |
23780 KB |
Output is correct |
17 |
Correct |
13 ms |
23788 KB |
Output is correct |
18 |
Correct |
12 ms |
23904 KB |
Output is correct |
19 |
Correct |
12 ms |
23884 KB |
Output is correct |
20 |
Correct |
12 ms |
23776 KB |
Output is correct |
21 |
Correct |
11 ms |
23876 KB |
Output is correct |
22 |
Correct |
12 ms |
23884 KB |
Output is correct |
23 |
Correct |
12 ms |
23808 KB |
Output is correct |
24 |
Correct |
11 ms |
23788 KB |
Output is correct |
25 |
Correct |
11 ms |
23924 KB |
Output is correct |
26 |
Correct |
20 ms |
26060 KB |
Output is correct |
27 |
Correct |
22 ms |
25848 KB |
Output is correct |
28 |
Correct |
20 ms |
26320 KB |
Output is correct |
29 |
Correct |
20 ms |
26444 KB |
Output is correct |
30 |
Correct |
18 ms |
25772 KB |
Output is correct |
31 |
Correct |
13 ms |
23884 KB |
Output is correct |
32 |
Correct |
24 ms |
26200 KB |
Output is correct |
33 |
Correct |
14 ms |
23924 KB |
Output is correct |
34 |
Correct |
17 ms |
25808 KB |
Output is correct |
35 |
Correct |
21 ms |
26356 KB |
Output is correct |
36 |
Correct |
17 ms |
25708 KB |
Output is correct |
37 |
Correct |
18 ms |
25924 KB |
Output is correct |
38 |
Correct |
13 ms |
23924 KB |
Output is correct |
39 |
Correct |
19 ms |
26060 KB |
Output is correct |
40 |
Correct |
16 ms |
25732 KB |
Output is correct |
41 |
Correct |
17 ms |
25796 KB |
Output is correct |
42 |
Correct |
18 ms |
25904 KB |
Output is correct |
43 |
Correct |
16 ms |
26080 KB |
Output is correct |
44 |
Correct |
12 ms |
23948 KB |
Output is correct |
45 |
Correct |
17 ms |
25988 KB |
Output is correct |
46 |
Correct |
18 ms |
25848 KB |
Output is correct |
47 |
Correct |
13 ms |
23884 KB |
Output is correct |
48 |
Correct |
20 ms |
25856 KB |
Output is correct |
49 |
Correct |
20 ms |
26372 KB |
Output is correct |
50 |
Correct |
21 ms |
26496 KB |
Output is correct |
51 |
Correct |
18 ms |
25900 KB |
Output is correct |
52 |
Correct |
17 ms |
25832 KB |
Output is correct |
53 |
Correct |
18 ms |
26000 KB |
Output is correct |
54 |
Correct |
18 ms |
25752 KB |
Output is correct |
55 |
Correct |
12 ms |
23884 KB |
Output is correct |
56 |
Correct |
239 ms |
102856 KB |
Output is correct |
57 |
Correct |
205 ms |
102604 KB |
Output is correct |
58 |
Correct |
18 ms |
25724 KB |
Output is correct |
59 |
Correct |
13 ms |
23884 KB |
Output is correct |
60 |
Correct |
13 ms |
23920 KB |
Output is correct |
61 |
Correct |
207 ms |
102488 KB |
Output is correct |
62 |
Correct |
16 ms |
25804 KB |
Output is correct |
63 |
Correct |
215 ms |
112132 KB |
Output is correct |
64 |
Correct |
207 ms |
102644 KB |
Output is correct |
65 |
Correct |
17 ms |
25804 KB |
Output is correct |
66 |
Correct |
220 ms |
103088 KB |
Output is correct |
67 |
Correct |
16 ms |
25724 KB |
Output is correct |
68 |
Correct |
17 ms |
25796 KB |
Output is correct |
69 |
Correct |
237 ms |
103100 KB |
Output is correct |
70 |
Correct |
16 ms |
26052 KB |
Output is correct |
71 |
Correct |
203 ms |
102408 KB |
Output is correct |
72 |
Correct |
235 ms |
105284 KB |
Output is correct |
73 |
Correct |
13 ms |
23920 KB |
Output is correct |
74 |
Correct |
220 ms |
112448 KB |
Output is correct |
75 |
Correct |
205 ms |
108496 KB |
Output is correct |
76 |
Correct |
17 ms |
25804 KB |
Output is correct |
77 |
Correct |
22 ms |
25708 KB |
Output is correct |
78 |
Correct |
212 ms |
101344 KB |
Output is correct |
79 |
Correct |
416 ms |
121624 KB |
Output is correct |
80 |
Correct |
19 ms |
26052 KB |
Output is correct |
81 |
Correct |
17 ms |
25836 KB |
Output is correct |
82 |
Correct |
14 ms |
23888 KB |
Output is correct |
83 |
Correct |
12 ms |
23888 KB |
Output is correct |
84 |
Correct |
16 ms |
25808 KB |
Output is correct |
85 |
Correct |
254 ms |
106788 KB |
Output is correct |
86 |
Correct |
17 ms |
25732 KB |
Output is correct |
87 |
Correct |
223 ms |
102984 KB |
Output is correct |
88 |
Correct |
14 ms |
23860 KB |
Output is correct |
89 |
Correct |
209 ms |
102500 KB |
Output is correct |
90 |
Correct |
275 ms |
108180 KB |
Output is correct |
91 |
Correct |
415 ms |
122968 KB |
Output is correct |
92 |
Correct |
205 ms |
103124 KB |
Output is correct |
93 |
Correct |
19 ms |
25972 KB |
Output is correct |
94 |
Correct |
13 ms |
23888 KB |
Output is correct |
95 |
Correct |
382 ms |
120440 KB |
Output is correct |
96 |
Correct |
429 ms |
127348 KB |
Output is correct |
97 |
Correct |
18 ms |
25848 KB |
Output is correct |
98 |
Correct |
13 ms |
23888 KB |
Output is correct |
99 |
Correct |
265 ms |
108772 KB |
Output is correct |
100 |
Correct |
18 ms |
25808 KB |
Output is correct |
101 |
Correct |
235 ms |
104972 KB |
Output is correct |
102 |
Correct |
440 ms |
127416 KB |
Output is correct |
103 |
Correct |
19 ms |
26448 KB |
Output is correct |
104 |
Correct |
20 ms |
26484 KB |
Output is correct |
105 |
Correct |
17 ms |
25812 KB |
Output is correct |
106 |
Correct |
18 ms |
25820 KB |
Output is correct |
107 |
Correct |
18 ms |
25904 KB |
Output is correct |
108 |
Runtime error |
638 ms |
262148 KB |
Execution killed with signal 9 |
109 |
Halted |
0 ms |
0 KB |
- |