#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 tin[N],timer,d[25][N],dep[N];
int edge[N],val[N],n,p[N],lz[N];
bool 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++){
int c;
cin>>c;
vec[c].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:108: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]
108 | for (int j = 1; j < vec[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47452 KB |
Output is correct |
2 |
Correct |
20 ms |
47468 KB |
Output is correct |
3 |
Correct |
21 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
23 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
23 ms |
47452 KB |
Output is correct |
8 |
Correct |
21 ms |
47428 KB |
Output is correct |
9 |
Correct |
22 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47456 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
21 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47480 KB |
Output is correct |
14 |
Correct |
23 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47428 KB |
Output is correct |
16 |
Correct |
21 ms |
47624 KB |
Output is correct |
17 |
Correct |
27 ms |
47452 KB |
Output is correct |
18 |
Correct |
22 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47452 KB |
Output is correct |
20 |
Correct |
22 ms |
47448 KB |
Output is correct |
21 |
Correct |
20 ms |
47452 KB |
Output is correct |
22 |
Correct |
21 ms |
47452 KB |
Output is correct |
23 |
Correct |
21 ms |
47612 KB |
Output is correct |
24 |
Correct |
21 ms |
47452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47452 KB |
Output is correct |
2 |
Correct |
20 ms |
47468 KB |
Output is correct |
3 |
Correct |
21 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
23 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
23 ms |
47452 KB |
Output is correct |
8 |
Correct |
21 ms |
47428 KB |
Output is correct |
9 |
Correct |
22 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47456 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
21 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47480 KB |
Output is correct |
14 |
Correct |
23 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47428 KB |
Output is correct |
16 |
Correct |
21 ms |
47624 KB |
Output is correct |
17 |
Correct |
27 ms |
47452 KB |
Output is correct |
18 |
Correct |
22 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47452 KB |
Output is correct |
20 |
Correct |
22 ms |
47448 KB |
Output is correct |
21 |
Correct |
20 ms |
47452 KB |
Output is correct |
22 |
Correct |
21 ms |
47452 KB |
Output is correct |
23 |
Correct |
21 ms |
47612 KB |
Output is correct |
24 |
Correct |
21 ms |
47452 KB |
Output is correct |
25 |
Correct |
20 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48220 KB |
Output is correct |
27 |
Correct |
23 ms |
48220 KB |
Output is correct |
28 |
Correct |
24 ms |
48448 KB |
Output is correct |
29 |
Correct |
23 ms |
48340 KB |
Output is correct |
30 |
Correct |
24 ms |
48220 KB |
Output is correct |
31 |
Correct |
22 ms |
47448 KB |
Output is correct |
32 |
Correct |
23 ms |
48476 KB |
Output is correct |
33 |
Correct |
26 ms |
47600 KB |
Output is correct |
34 |
Correct |
23 ms |
48220 KB |
Output is correct |
35 |
Correct |
26 ms |
48472 KB |
Output is correct |
36 |
Correct |
23 ms |
48220 KB |
Output is correct |
37 |
Correct |
23 ms |
48220 KB |
Output is correct |
38 |
Correct |
22 ms |
47424 KB |
Output is correct |
39 |
Correct |
32 ms |
48208 KB |
Output is correct |
40 |
Correct |
24 ms |
48412 KB |
Output is correct |
41 |
Correct |
24 ms |
48220 KB |
Output is correct |
42 |
Correct |
23 ms |
48452 KB |
Output is correct |
43 |
Correct |
27 ms |
48476 KB |
Output is correct |
44 |
Correct |
21 ms |
47452 KB |
Output is correct |
45 |
Correct |
24 ms |
48476 KB |
Output is correct |
46 |
Correct |
35 ms |
48448 KB |
Output is correct |
47 |
Correct |
21 ms |
47452 KB |
Output is correct |
48 |
Correct |
23 ms |
48444 KB |
Output is correct |
49 |
Correct |
23 ms |
48472 KB |
Output is correct |
50 |
Correct |
23 ms |
48476 KB |
Output is correct |
51 |
Correct |
25 ms |
48236 KB |
Output is correct |
52 |
Correct |
24 ms |
48396 KB |
Output is correct |
53 |
Correct |
24 ms |
48216 KB |
Output is correct |
54 |
Correct |
25 ms |
48216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47452 KB |
Output is correct |
2 |
Correct |
20 ms |
47468 KB |
Output is correct |
3 |
Correct |
21 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
23 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
23 ms |
47452 KB |
Output is correct |
8 |
Correct |
21 ms |
47428 KB |
Output is correct |
9 |
Correct |
22 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47456 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
21 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47480 KB |
Output is correct |
14 |
Correct |
23 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47428 KB |
Output is correct |
16 |
Correct |
21 ms |
47624 KB |
Output is correct |
17 |
Correct |
27 ms |
47452 KB |
Output is correct |
18 |
Correct |
22 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47452 KB |
Output is correct |
20 |
Correct |
22 ms |
47448 KB |
Output is correct |
21 |
Correct |
20 ms |
47452 KB |
Output is correct |
22 |
Correct |
21 ms |
47452 KB |
Output is correct |
23 |
Correct |
21 ms |
47612 KB |
Output is correct |
24 |
Correct |
21 ms |
47452 KB |
Output is correct |
25 |
Correct |
20 ms |
47448 KB |
Output is correct |
26 |
Correct |
151 ms |
73948 KB |
Output is correct |
27 |
Correct |
216 ms |
73712 KB |
Output is correct |
28 |
Correct |
32 ms |
48208 KB |
Output is correct |
29 |
Correct |
22 ms |
47448 KB |
Output is correct |
30 |
Correct |
21 ms |
47448 KB |
Output is correct |
31 |
Correct |
201 ms |
73736 KB |
Output is correct |
32 |
Correct |
24 ms |
48220 KB |
Output is correct |
33 |
Correct |
280 ms |
79044 KB |
Output is correct |
34 |
Correct |
204 ms |
73700 KB |
Output is correct |
35 |
Correct |
24 ms |
48220 KB |
Output is correct |
36 |
Correct |
221 ms |
73736 KB |
Output is correct |
37 |
Correct |
25 ms |
48208 KB |
Output is correct |
38 |
Correct |
24 ms |
48220 KB |
Output is correct |
39 |
Correct |
173 ms |
75836 KB |
Output is correct |
40 |
Correct |
28 ms |
48476 KB |
Output is correct |
41 |
Correct |
217 ms |
73444 KB |
Output is correct |
42 |
Correct |
243 ms |
75024 KB |
Output is correct |
43 |
Correct |
20 ms |
47452 KB |
Output is correct |
44 |
Correct |
262 ms |
79048 KB |
Output is correct |
45 |
Correct |
266 ms |
77544 KB |
Output is correct |
46 |
Correct |
25 ms |
48220 KB |
Output is correct |
47 |
Correct |
26 ms |
48176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
73924 KB |
Output is correct |
2 |
Correct |
136 ms |
80580 KB |
Output is correct |
3 |
Correct |
24 ms |
48220 KB |
Output is correct |
4 |
Correct |
24 ms |
48216 KB |
Output is correct |
5 |
Correct |
30 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
23 ms |
48272 KB |
Output is correct |
8 |
Correct |
177 ms |
74904 KB |
Output is correct |
9 |
Correct |
24 ms |
48216 KB |
Output is correct |
10 |
Correct |
191 ms |
74068 KB |
Output is correct |
11 |
Correct |
22 ms |
47448 KB |
Output is correct |
12 |
Correct |
181 ms |
73300 KB |
Output is correct |
13 |
Correct |
154 ms |
74620 KB |
Output is correct |
14 |
Correct |
122 ms |
75756 KB |
Output is correct |
15 |
Correct |
164 ms |
75840 KB |
Output is correct |
16 |
Correct |
25 ms |
48468 KB |
Output is correct |
17 |
Correct |
21 ms |
47452 KB |
Output is correct |
18 |
Correct |
160 ms |
78912 KB |
Output is correct |
19 |
Correct |
129 ms |
78168 KB |
Output is correct |
20 |
Correct |
23 ms |
48220 KB |
Output is correct |
21 |
Correct |
21 ms |
47452 KB |
Output is correct |
22 |
Correct |
155 ms |
74980 KB |
Output is correct |
23 |
Correct |
23 ms |
48216 KB |
Output is correct |
24 |
Correct |
159 ms |
74068 KB |
Output is correct |
25 |
Correct |
137 ms |
78024 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
22 ms |
48344 KB |
Output is correct |
28 |
Correct |
24 ms |
48372 KB |
Output is correct |
29 |
Correct |
23 ms |
48220 KB |
Output is correct |
30 |
Correct |
23 ms |
48220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47452 KB |
Output is correct |
2 |
Correct |
20 ms |
47468 KB |
Output is correct |
3 |
Correct |
21 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47452 KB |
Output is correct |
5 |
Correct |
23 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
23 ms |
47452 KB |
Output is correct |
8 |
Correct |
21 ms |
47428 KB |
Output is correct |
9 |
Correct |
22 ms |
47444 KB |
Output is correct |
10 |
Correct |
20 ms |
47456 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
21 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47480 KB |
Output is correct |
14 |
Correct |
23 ms |
47452 KB |
Output is correct |
15 |
Correct |
21 ms |
47428 KB |
Output is correct |
16 |
Correct |
21 ms |
47624 KB |
Output is correct |
17 |
Correct |
27 ms |
47452 KB |
Output is correct |
18 |
Correct |
22 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47452 KB |
Output is correct |
20 |
Correct |
22 ms |
47448 KB |
Output is correct |
21 |
Correct |
20 ms |
47452 KB |
Output is correct |
22 |
Correct |
21 ms |
47452 KB |
Output is correct |
23 |
Correct |
21 ms |
47612 KB |
Output is correct |
24 |
Correct |
21 ms |
47452 KB |
Output is correct |
25 |
Correct |
20 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48220 KB |
Output is correct |
27 |
Correct |
23 ms |
48220 KB |
Output is correct |
28 |
Correct |
24 ms |
48448 KB |
Output is correct |
29 |
Correct |
23 ms |
48340 KB |
Output is correct |
30 |
Correct |
24 ms |
48220 KB |
Output is correct |
31 |
Correct |
22 ms |
47448 KB |
Output is correct |
32 |
Correct |
23 ms |
48476 KB |
Output is correct |
33 |
Correct |
26 ms |
47600 KB |
Output is correct |
34 |
Correct |
23 ms |
48220 KB |
Output is correct |
35 |
Correct |
26 ms |
48472 KB |
Output is correct |
36 |
Correct |
23 ms |
48220 KB |
Output is correct |
37 |
Correct |
23 ms |
48220 KB |
Output is correct |
38 |
Correct |
22 ms |
47424 KB |
Output is correct |
39 |
Correct |
32 ms |
48208 KB |
Output is correct |
40 |
Correct |
24 ms |
48412 KB |
Output is correct |
41 |
Correct |
24 ms |
48220 KB |
Output is correct |
42 |
Correct |
23 ms |
48452 KB |
Output is correct |
43 |
Correct |
27 ms |
48476 KB |
Output is correct |
44 |
Correct |
21 ms |
47452 KB |
Output is correct |
45 |
Correct |
24 ms |
48476 KB |
Output is correct |
46 |
Correct |
35 ms |
48448 KB |
Output is correct |
47 |
Correct |
21 ms |
47452 KB |
Output is correct |
48 |
Correct |
23 ms |
48444 KB |
Output is correct |
49 |
Correct |
23 ms |
48472 KB |
Output is correct |
50 |
Correct |
23 ms |
48476 KB |
Output is correct |
51 |
Correct |
25 ms |
48236 KB |
Output is correct |
52 |
Correct |
24 ms |
48396 KB |
Output is correct |
53 |
Correct |
24 ms |
48216 KB |
Output is correct |
54 |
Correct |
25 ms |
48216 KB |
Output is correct |
55 |
Correct |
20 ms |
47448 KB |
Output is correct |
56 |
Correct |
151 ms |
73948 KB |
Output is correct |
57 |
Correct |
216 ms |
73712 KB |
Output is correct |
58 |
Correct |
32 ms |
48208 KB |
Output is correct |
59 |
Correct |
22 ms |
47448 KB |
Output is correct |
60 |
Correct |
21 ms |
47448 KB |
Output is correct |
61 |
Correct |
201 ms |
73736 KB |
Output is correct |
62 |
Correct |
24 ms |
48220 KB |
Output is correct |
63 |
Correct |
280 ms |
79044 KB |
Output is correct |
64 |
Correct |
204 ms |
73700 KB |
Output is correct |
65 |
Correct |
24 ms |
48220 KB |
Output is correct |
66 |
Correct |
221 ms |
73736 KB |
Output is correct |
67 |
Correct |
25 ms |
48208 KB |
Output is correct |
68 |
Correct |
24 ms |
48220 KB |
Output is correct |
69 |
Correct |
173 ms |
75836 KB |
Output is correct |
70 |
Correct |
28 ms |
48476 KB |
Output is correct |
71 |
Correct |
217 ms |
73444 KB |
Output is correct |
72 |
Correct |
243 ms |
75024 KB |
Output is correct |
73 |
Correct |
20 ms |
47452 KB |
Output is correct |
74 |
Correct |
262 ms |
79048 KB |
Output is correct |
75 |
Correct |
266 ms |
77544 KB |
Output is correct |
76 |
Correct |
25 ms |
48220 KB |
Output is correct |
77 |
Correct |
26 ms |
48176 KB |
Output is correct |
78 |
Correct |
147 ms |
73924 KB |
Output is correct |
79 |
Correct |
136 ms |
80580 KB |
Output is correct |
80 |
Correct |
24 ms |
48220 KB |
Output is correct |
81 |
Correct |
24 ms |
48216 KB |
Output is correct |
82 |
Correct |
30 ms |
47452 KB |
Output is correct |
83 |
Correct |
21 ms |
47452 KB |
Output is correct |
84 |
Correct |
23 ms |
48272 KB |
Output is correct |
85 |
Correct |
177 ms |
74904 KB |
Output is correct |
86 |
Correct |
24 ms |
48216 KB |
Output is correct |
87 |
Correct |
191 ms |
74068 KB |
Output is correct |
88 |
Correct |
22 ms |
47448 KB |
Output is correct |
89 |
Correct |
181 ms |
73300 KB |
Output is correct |
90 |
Correct |
154 ms |
74620 KB |
Output is correct |
91 |
Correct |
122 ms |
75756 KB |
Output is correct |
92 |
Correct |
164 ms |
75840 KB |
Output is correct |
93 |
Correct |
25 ms |
48468 KB |
Output is correct |
94 |
Correct |
21 ms |
47452 KB |
Output is correct |
95 |
Correct |
160 ms |
78912 KB |
Output is correct |
96 |
Correct |
129 ms |
78168 KB |
Output is correct |
97 |
Correct |
23 ms |
48220 KB |
Output is correct |
98 |
Correct |
21 ms |
47452 KB |
Output is correct |
99 |
Correct |
155 ms |
74980 KB |
Output is correct |
100 |
Correct |
23 ms |
48216 KB |
Output is correct |
101 |
Correct |
159 ms |
74068 KB |
Output is correct |
102 |
Correct |
137 ms |
78024 KB |
Output is correct |
103 |
Correct |
23 ms |
48476 KB |
Output is correct |
104 |
Correct |
22 ms |
48344 KB |
Output is correct |
105 |
Correct |
24 ms |
48372 KB |
Output is correct |
106 |
Correct |
23 ms |
48220 KB |
Output is correct |
107 |
Correct |
23 ms |
48220 KB |
Output is correct |
108 |
Correct |
1092 ms |
184700 KB |
Output is correct |
109 |
Correct |
2318 ms |
189280 KB |
Output is correct |
110 |
Correct |
2392 ms |
197192 KB |
Output is correct |
111 |
Correct |
1051 ms |
203452 KB |
Output is correct |
112 |
Correct |
2003 ms |
199868 KB |
Output is correct |
113 |
Correct |
1129 ms |
203564 KB |
Output is correct |
114 |
Correct |
1756 ms |
176620 KB |
Output is correct |
115 |
Correct |
1805 ms |
176568 KB |
Output is correct |
116 |
Correct |
1785 ms |
179220 KB |
Output is correct |
117 |
Correct |
881 ms |
184748 KB |
Output is correct |
118 |
Correct |
1606 ms |
174672 KB |
Output is correct |
119 |
Correct |
864 ms |
184660 KB |
Output is correct |
120 |
Correct |
1000 ms |
197172 KB |
Output is correct |
121 |
Correct |
842 ms |
185936 KB |
Output is correct |
122 |
Correct |
1871 ms |
181488 KB |
Output is correct |
123 |
Correct |
935 ms |
209180 KB |
Output is correct |
124 |
Correct |
1109 ms |
181704 KB |
Output is correct |