#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
using namespace std;
const int N = 5e5 + 5;
vector <pii> v[N],vec[N];
int col[N],tin[N],timer,d[25][N],dep[N];
int edge[N],val[N],n,D,I,p[N],lz[N];
bool vis[N],del[N];
set <int> st[N];
void dfs(int x,int par){
d[0][x] = par;
dep[x] = dep[par] + 1;
tin[x] = ++timer;
for (auto [to, id]: v[x]){
if (to != par) {
edge[to] = id;
dfs(to,x);
}
}
}
int lca(int x,int y){
if (dep[x] < dep[y ]) swap(x,y);
for (int i = 20; i >= 0; i--)
if (dep[d[i][x]] >= dep[y]) x = d[i][x];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (d[i][x] != d[i][y]) x = d[i][x],y = d[i][y];
return d[0][x];
}
void getpath(int x,int y){
int c = lca(x,y);
lz[x] += 1;
lz[y] += 1;
lz[c] -= 2;
}
int P(int x){
if(x==p[x])return x;
return p[x]=P(p[x]);
}
void dsu(int x,int y){
int px = P(x),py = P(y);
if ((int)st[px].size() < (int)st[py].size()) swap(px,py);
p[py] = px;
del[py] = 1;
for (int v: st[py])
st[px].insert(v);
st[py].clear();
}
int dfs2(int x,int par){
int s = lz[x];
for (auto [to,id]: v[x]){
if (to == par) continue;
s += dfs2(to,x);
}
if (x > 1 && s > 0) {
val[edge[x]] = 0;
}
return s;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int k;
cin>>n>>k;
for (int i = 1; i < n; i++){
int a,b;
cin>>a>>b;
st[a].insert(b);
st[b].insert(a);
v[a].pb({b,i});
v[b].pb({a,i});
val[i] = 1;
}
dfs(1,1);
for (int j = 1; j <= 20 ; j++)
for (int i = 1; i <= n; i++)
d[j][i] = d[j - 1][d[j - 1][i]];
for (int i = 1; i <= n; i++)
cin >> col[i],vec[col[i]].pb({tin[i],i}),p[i] = i;
for (int i = 1; i <= k; i++){
sort(vec[i].begin(),vec[i].end());
for (int j = 1; j < vec[i].size(); j++){
getpath(vec[i][j].s,vec[i][j - 1].s);
}
}
dfs2(1,1);
for (int i = 2; i <= n; i++)
if (!val[edge[i]]) dsu(i,d[0][i]);
int ans = 0;
for (int i = 1; i <= n;i++){
if (del[i]) continue;
set <int> nw;
for (int v: st[i])
nw.insert(P(v));
if (nw.find(i) != nw.end()) nw.erase(nw.find(i));
ans += ((int)nw.size() == 1);
}
cout<<(ans + 1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:104:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int j = 1; j < vec[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
21 ms |
47452 KB |
Output is correct |
3 |
Correct |
20 ms |
47448 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
22 ms |
47452 KB |
Output is correct |
6 |
Correct |
20 ms |
47448 KB |
Output is correct |
7 |
Correct |
24 ms |
47452 KB |
Output is correct |
8 |
Correct |
19 ms |
47448 KB |
Output is correct |
9 |
Correct |
23 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47452 KB |
Output is correct |
11 |
Correct |
20 ms |
47536 KB |
Output is correct |
12 |
Correct |
19 ms |
47528 KB |
Output is correct |
13 |
Correct |
20 ms |
47584 KB |
Output is correct |
14 |
Correct |
25 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47452 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
20 ms |
47448 KB |
Output is correct |
18 |
Correct |
20 ms |
47592 KB |
Output is correct |
19 |
Correct |
21 ms |
47612 KB |
Output is correct |
20 |
Correct |
20 ms |
47448 KB |
Output is correct |
21 |
Correct |
24 ms |
47452 KB |
Output is correct |
22 |
Correct |
20 ms |
47680 KB |
Output is correct |
23 |
Correct |
20 ms |
47452 KB |
Output is correct |
24 |
Correct |
19 ms |
47584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
21 ms |
47452 KB |
Output is correct |
3 |
Correct |
20 ms |
47448 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
22 ms |
47452 KB |
Output is correct |
6 |
Correct |
20 ms |
47448 KB |
Output is correct |
7 |
Correct |
24 ms |
47452 KB |
Output is correct |
8 |
Correct |
19 ms |
47448 KB |
Output is correct |
9 |
Correct |
23 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47452 KB |
Output is correct |
11 |
Correct |
20 ms |
47536 KB |
Output is correct |
12 |
Correct |
19 ms |
47528 KB |
Output is correct |
13 |
Correct |
20 ms |
47584 KB |
Output is correct |
14 |
Correct |
25 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47452 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
20 ms |
47448 KB |
Output is correct |
18 |
Correct |
20 ms |
47592 KB |
Output is correct |
19 |
Correct |
21 ms |
47612 KB |
Output is correct |
20 |
Correct |
20 ms |
47448 KB |
Output is correct |
21 |
Correct |
24 ms |
47452 KB |
Output is correct |
22 |
Correct |
20 ms |
47680 KB |
Output is correct |
23 |
Correct |
20 ms |
47452 KB |
Output is correct |
24 |
Correct |
19 ms |
47584 KB |
Output is correct |
25 |
Correct |
19 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48312 KB |
Output is correct |
27 |
Correct |
23 ms |
48216 KB |
Output is correct |
28 |
Correct |
29 ms |
48556 KB |
Output is correct |
29 |
Correct |
22 ms |
48476 KB |
Output is correct |
30 |
Correct |
22 ms |
48220 KB |
Output is correct |
31 |
Correct |
20 ms |
47452 KB |
Output is correct |
32 |
Correct |
24 ms |
48476 KB |
Output is correct |
33 |
Correct |
20 ms |
47488 KB |
Output is correct |
34 |
Correct |
22 ms |
48212 KB |
Output is correct |
35 |
Correct |
21 ms |
48472 KB |
Output is correct |
36 |
Correct |
23 ms |
48200 KB |
Output is correct |
37 |
Correct |
23 ms |
48256 KB |
Output is correct |
38 |
Correct |
21 ms |
47452 KB |
Output is correct |
39 |
Correct |
23 ms |
48352 KB |
Output is correct |
40 |
Correct |
23 ms |
48220 KB |
Output is correct |
41 |
Correct |
24 ms |
48476 KB |
Output is correct |
42 |
Correct |
22 ms |
48472 KB |
Output is correct |
43 |
Correct |
24 ms |
48376 KB |
Output is correct |
44 |
Correct |
19 ms |
47452 KB |
Output is correct |
45 |
Correct |
23 ms |
48472 KB |
Output is correct |
46 |
Correct |
23 ms |
48320 KB |
Output is correct |
47 |
Correct |
20 ms |
47452 KB |
Output is correct |
48 |
Correct |
22 ms |
48324 KB |
Output is correct |
49 |
Correct |
21 ms |
48476 KB |
Output is correct |
50 |
Correct |
23 ms |
48304 KB |
Output is correct |
51 |
Correct |
23 ms |
48220 KB |
Output is correct |
52 |
Correct |
23 ms |
48476 KB |
Output is correct |
53 |
Correct |
22 ms |
48356 KB |
Output is correct |
54 |
Correct |
26 ms |
48216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
21 ms |
47452 KB |
Output is correct |
3 |
Correct |
20 ms |
47448 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
22 ms |
47452 KB |
Output is correct |
6 |
Correct |
20 ms |
47448 KB |
Output is correct |
7 |
Correct |
24 ms |
47452 KB |
Output is correct |
8 |
Correct |
19 ms |
47448 KB |
Output is correct |
9 |
Correct |
23 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47452 KB |
Output is correct |
11 |
Correct |
20 ms |
47536 KB |
Output is correct |
12 |
Correct |
19 ms |
47528 KB |
Output is correct |
13 |
Correct |
20 ms |
47584 KB |
Output is correct |
14 |
Correct |
25 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47452 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
20 ms |
47448 KB |
Output is correct |
18 |
Correct |
20 ms |
47592 KB |
Output is correct |
19 |
Correct |
21 ms |
47612 KB |
Output is correct |
20 |
Correct |
20 ms |
47448 KB |
Output is correct |
21 |
Correct |
24 ms |
47452 KB |
Output is correct |
22 |
Correct |
20 ms |
47680 KB |
Output is correct |
23 |
Correct |
20 ms |
47452 KB |
Output is correct |
24 |
Correct |
19 ms |
47584 KB |
Output is correct |
25 |
Correct |
21 ms |
47428 KB |
Output is correct |
26 |
Correct |
147 ms |
74428 KB |
Output is correct |
27 |
Correct |
244 ms |
74300 KB |
Output is correct |
28 |
Correct |
23 ms |
48220 KB |
Output is correct |
29 |
Correct |
20 ms |
47452 KB |
Output is correct |
30 |
Correct |
19 ms |
47448 KB |
Output is correct |
31 |
Correct |
172 ms |
74068 KB |
Output is correct |
32 |
Correct |
23 ms |
48216 KB |
Output is correct |
33 |
Correct |
263 ms |
79320 KB |
Output is correct |
34 |
Correct |
209 ms |
74064 KB |
Output is correct |
35 |
Correct |
23 ms |
48220 KB |
Output is correct |
36 |
Correct |
207 ms |
74052 KB |
Output is correct |
37 |
Correct |
23 ms |
48220 KB |
Output is correct |
38 |
Correct |
23 ms |
48472 KB |
Output is correct |
39 |
Correct |
160 ms |
76356 KB |
Output is correct |
40 |
Correct |
24 ms |
48476 KB |
Output is correct |
41 |
Correct |
213 ms |
73928 KB |
Output is correct |
42 |
Correct |
227 ms |
75600 KB |
Output is correct |
43 |
Correct |
20 ms |
47452 KB |
Output is correct |
44 |
Correct |
296 ms |
79312 KB |
Output is correct |
45 |
Correct |
253 ms |
78000 KB |
Output is correct |
46 |
Correct |
23 ms |
48472 KB |
Output is correct |
47 |
Correct |
23 ms |
48212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
74440 KB |
Output is correct |
2 |
Correct |
132 ms |
80960 KB |
Output is correct |
3 |
Correct |
23 ms |
48476 KB |
Output is correct |
4 |
Correct |
22 ms |
48212 KB |
Output is correct |
5 |
Correct |
21 ms |
47444 KB |
Output is correct |
6 |
Correct |
21 ms |
47448 KB |
Output is correct |
7 |
Correct |
22 ms |
48244 KB |
Output is correct |
8 |
Correct |
156 ms |
75180 KB |
Output is correct |
9 |
Correct |
23 ms |
48220 KB |
Output is correct |
10 |
Correct |
192 ms |
74328 KB |
Output is correct |
11 |
Correct |
20 ms |
47452 KB |
Output is correct |
12 |
Correct |
168 ms |
73812 KB |
Output is correct |
13 |
Correct |
161 ms |
75056 KB |
Output is correct |
14 |
Correct |
129 ms |
76040 KB |
Output is correct |
15 |
Correct |
151 ms |
76348 KB |
Output is correct |
16 |
Correct |
23 ms |
48456 KB |
Output is correct |
17 |
Correct |
21 ms |
47452 KB |
Output is correct |
18 |
Correct |
153 ms |
79344 KB |
Output is correct |
19 |
Correct |
144 ms |
78676 KB |
Output is correct |
20 |
Correct |
23 ms |
48220 KB |
Output is correct |
21 |
Correct |
22 ms |
47420 KB |
Output is correct |
22 |
Correct |
163 ms |
75436 KB |
Output is correct |
23 |
Correct |
23 ms |
48476 KB |
Output is correct |
24 |
Correct |
164 ms |
74580 KB |
Output is correct |
25 |
Correct |
139 ms |
78356 KB |
Output is correct |
26 |
Correct |
25 ms |
48464 KB |
Output is correct |
27 |
Correct |
22 ms |
48464 KB |
Output is correct |
28 |
Correct |
27 ms |
48216 KB |
Output is correct |
29 |
Correct |
23 ms |
48476 KB |
Output is correct |
30 |
Correct |
23 ms |
48296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
21 ms |
47452 KB |
Output is correct |
3 |
Correct |
20 ms |
47448 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
22 ms |
47452 KB |
Output is correct |
6 |
Correct |
20 ms |
47448 KB |
Output is correct |
7 |
Correct |
24 ms |
47452 KB |
Output is correct |
8 |
Correct |
19 ms |
47448 KB |
Output is correct |
9 |
Correct |
23 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47452 KB |
Output is correct |
11 |
Correct |
20 ms |
47536 KB |
Output is correct |
12 |
Correct |
19 ms |
47528 KB |
Output is correct |
13 |
Correct |
20 ms |
47584 KB |
Output is correct |
14 |
Correct |
25 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47452 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
20 ms |
47448 KB |
Output is correct |
18 |
Correct |
20 ms |
47592 KB |
Output is correct |
19 |
Correct |
21 ms |
47612 KB |
Output is correct |
20 |
Correct |
20 ms |
47448 KB |
Output is correct |
21 |
Correct |
24 ms |
47452 KB |
Output is correct |
22 |
Correct |
20 ms |
47680 KB |
Output is correct |
23 |
Correct |
20 ms |
47452 KB |
Output is correct |
24 |
Correct |
19 ms |
47584 KB |
Output is correct |
25 |
Correct |
19 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48312 KB |
Output is correct |
27 |
Correct |
23 ms |
48216 KB |
Output is correct |
28 |
Correct |
29 ms |
48556 KB |
Output is correct |
29 |
Correct |
22 ms |
48476 KB |
Output is correct |
30 |
Correct |
22 ms |
48220 KB |
Output is correct |
31 |
Correct |
20 ms |
47452 KB |
Output is correct |
32 |
Correct |
24 ms |
48476 KB |
Output is correct |
33 |
Correct |
20 ms |
47488 KB |
Output is correct |
34 |
Correct |
22 ms |
48212 KB |
Output is correct |
35 |
Correct |
21 ms |
48472 KB |
Output is correct |
36 |
Correct |
23 ms |
48200 KB |
Output is correct |
37 |
Correct |
23 ms |
48256 KB |
Output is correct |
38 |
Correct |
21 ms |
47452 KB |
Output is correct |
39 |
Correct |
23 ms |
48352 KB |
Output is correct |
40 |
Correct |
23 ms |
48220 KB |
Output is correct |
41 |
Correct |
24 ms |
48476 KB |
Output is correct |
42 |
Correct |
22 ms |
48472 KB |
Output is correct |
43 |
Correct |
24 ms |
48376 KB |
Output is correct |
44 |
Correct |
19 ms |
47452 KB |
Output is correct |
45 |
Correct |
23 ms |
48472 KB |
Output is correct |
46 |
Correct |
23 ms |
48320 KB |
Output is correct |
47 |
Correct |
20 ms |
47452 KB |
Output is correct |
48 |
Correct |
22 ms |
48324 KB |
Output is correct |
49 |
Correct |
21 ms |
48476 KB |
Output is correct |
50 |
Correct |
23 ms |
48304 KB |
Output is correct |
51 |
Correct |
23 ms |
48220 KB |
Output is correct |
52 |
Correct |
23 ms |
48476 KB |
Output is correct |
53 |
Correct |
22 ms |
48356 KB |
Output is correct |
54 |
Correct |
26 ms |
48216 KB |
Output is correct |
55 |
Correct |
21 ms |
47428 KB |
Output is correct |
56 |
Correct |
147 ms |
74428 KB |
Output is correct |
57 |
Correct |
244 ms |
74300 KB |
Output is correct |
58 |
Correct |
23 ms |
48220 KB |
Output is correct |
59 |
Correct |
20 ms |
47452 KB |
Output is correct |
60 |
Correct |
19 ms |
47448 KB |
Output is correct |
61 |
Correct |
172 ms |
74068 KB |
Output is correct |
62 |
Correct |
23 ms |
48216 KB |
Output is correct |
63 |
Correct |
263 ms |
79320 KB |
Output is correct |
64 |
Correct |
209 ms |
74064 KB |
Output is correct |
65 |
Correct |
23 ms |
48220 KB |
Output is correct |
66 |
Correct |
207 ms |
74052 KB |
Output is correct |
67 |
Correct |
23 ms |
48220 KB |
Output is correct |
68 |
Correct |
23 ms |
48472 KB |
Output is correct |
69 |
Correct |
160 ms |
76356 KB |
Output is correct |
70 |
Correct |
24 ms |
48476 KB |
Output is correct |
71 |
Correct |
213 ms |
73928 KB |
Output is correct |
72 |
Correct |
227 ms |
75600 KB |
Output is correct |
73 |
Correct |
20 ms |
47452 KB |
Output is correct |
74 |
Correct |
296 ms |
79312 KB |
Output is correct |
75 |
Correct |
253 ms |
78000 KB |
Output is correct |
76 |
Correct |
23 ms |
48472 KB |
Output is correct |
77 |
Correct |
23 ms |
48212 KB |
Output is correct |
78 |
Correct |
140 ms |
74440 KB |
Output is correct |
79 |
Correct |
132 ms |
80960 KB |
Output is correct |
80 |
Correct |
23 ms |
48476 KB |
Output is correct |
81 |
Correct |
22 ms |
48212 KB |
Output is correct |
82 |
Correct |
21 ms |
47444 KB |
Output is correct |
83 |
Correct |
21 ms |
47448 KB |
Output is correct |
84 |
Correct |
22 ms |
48244 KB |
Output is correct |
85 |
Correct |
156 ms |
75180 KB |
Output is correct |
86 |
Correct |
23 ms |
48220 KB |
Output is correct |
87 |
Correct |
192 ms |
74328 KB |
Output is correct |
88 |
Correct |
20 ms |
47452 KB |
Output is correct |
89 |
Correct |
168 ms |
73812 KB |
Output is correct |
90 |
Correct |
161 ms |
75056 KB |
Output is correct |
91 |
Correct |
129 ms |
76040 KB |
Output is correct |
92 |
Correct |
151 ms |
76348 KB |
Output is correct |
93 |
Correct |
23 ms |
48456 KB |
Output is correct |
94 |
Correct |
21 ms |
47452 KB |
Output is correct |
95 |
Correct |
153 ms |
79344 KB |
Output is correct |
96 |
Correct |
144 ms |
78676 KB |
Output is correct |
97 |
Correct |
23 ms |
48220 KB |
Output is correct |
98 |
Correct |
22 ms |
47420 KB |
Output is correct |
99 |
Correct |
163 ms |
75436 KB |
Output is correct |
100 |
Correct |
23 ms |
48476 KB |
Output is correct |
101 |
Correct |
164 ms |
74580 KB |
Output is correct |
102 |
Correct |
139 ms |
78356 KB |
Output is correct |
103 |
Correct |
25 ms |
48464 KB |
Output is correct |
104 |
Correct |
22 ms |
48464 KB |
Output is correct |
105 |
Correct |
27 ms |
48216 KB |
Output is correct |
106 |
Correct |
23 ms |
48476 KB |
Output is correct |
107 |
Correct |
23 ms |
48296 KB |
Output is correct |
108 |
Correct |
1078 ms |
187780 KB |
Output is correct |
109 |
Correct |
2279 ms |
191716 KB |
Output is correct |
110 |
Correct |
2330 ms |
200608 KB |
Output is correct |
111 |
Correct |
1088 ms |
207676 KB |
Output is correct |
112 |
Correct |
2138 ms |
204040 KB |
Output is correct |
113 |
Correct |
1106 ms |
207712 KB |
Output is correct |
114 |
Correct |
1780 ms |
180032 KB |
Output is correct |
115 |
Correct |
1796 ms |
179896 KB |
Output is correct |
116 |
Correct |
1749 ms |
185260 KB |
Output is correct |
117 |
Correct |
856 ms |
191312 KB |
Output is correct |
118 |
Correct |
1584 ms |
180696 KB |
Output is correct |
119 |
Correct |
895 ms |
191572 KB |
Output is correct |
120 |
Correct |
950 ms |
203920 KB |
Output is correct |
121 |
Correct |
863 ms |
192852 KB |
Output is correct |
122 |
Correct |
1862 ms |
188244 KB |
Output is correct |
123 |
Correct |
911 ms |
215896 KB |
Output is correct |
124 |
Correct |
1049 ms |
188408 KB |
Output is correct |