#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
#define int ll
using namespace std;
const int N = 5e5 + 5,inf = 1e18;
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;
}
void DFS(int x,int par,int dd){
if (dd >= D) D = dd,I = x;
for (auto [to,id]: v[x]){
if (to != par) DFS(to,x,dd + val[id]);
}
}
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 (st[px].size() < 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});
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=1;i<=n;i++)
p[i] = i;
for (int i = 2; i <= n; i++)
if (!val[edge[i]]) dsu(i,d[0][i]);
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));
st[i] = nw;
}
int ans=0;
for (int i = 1; i <= n; i++){
if (del[i]) continue;
ans += ((int)st[i].size() == 1);
}
cout<<(ans + 1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:114:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int j = 1; j < vec[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47452 KB |
Output is correct |
2 |
Correct |
27 ms |
47660 KB |
Output is correct |
3 |
Correct |
20 ms |
47452 KB |
Output is correct |
4 |
Correct |
20 ms |
47636 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47696 KB |
Output is correct |
7 |
Correct |
20 ms |
47448 KB |
Output is correct |
8 |
Correct |
22 ms |
47452 KB |
Output is correct |
9 |
Correct |
22 ms |
47448 KB |
Output is correct |
10 |
Correct |
21 ms |
47452 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
23 ms |
47452 KB |
Output is correct |
13 |
Correct |
21 ms |
47652 KB |
Output is correct |
14 |
Correct |
20 ms |
47660 KB |
Output is correct |
15 |
Correct |
19 ms |
47616 KB |
Output is correct |
16 |
Correct |
19 ms |
47664 KB |
Output is correct |
17 |
Correct |
25 ms |
47452 KB |
Output is correct |
18 |
Correct |
21 ms |
47452 KB |
Output is correct |
19 |
Correct |
24 ms |
47452 KB |
Output is correct |
20 |
Correct |
20 ms |
47660 KB |
Output is correct |
21 |
Correct |
22 ms |
47452 KB |
Output is correct |
22 |
Correct |
22 ms |
47692 KB |
Output is correct |
23 |
Correct |
20 ms |
47448 KB |
Output is correct |
24 |
Correct |
19 ms |
47452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47452 KB |
Output is correct |
2 |
Correct |
27 ms |
47660 KB |
Output is correct |
3 |
Correct |
20 ms |
47452 KB |
Output is correct |
4 |
Correct |
20 ms |
47636 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47696 KB |
Output is correct |
7 |
Correct |
20 ms |
47448 KB |
Output is correct |
8 |
Correct |
22 ms |
47452 KB |
Output is correct |
9 |
Correct |
22 ms |
47448 KB |
Output is correct |
10 |
Correct |
21 ms |
47452 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
23 ms |
47452 KB |
Output is correct |
13 |
Correct |
21 ms |
47652 KB |
Output is correct |
14 |
Correct |
20 ms |
47660 KB |
Output is correct |
15 |
Correct |
19 ms |
47616 KB |
Output is correct |
16 |
Correct |
19 ms |
47664 KB |
Output is correct |
17 |
Correct |
25 ms |
47452 KB |
Output is correct |
18 |
Correct |
21 ms |
47452 KB |
Output is correct |
19 |
Correct |
24 ms |
47452 KB |
Output is correct |
20 |
Correct |
20 ms |
47660 KB |
Output is correct |
21 |
Correct |
22 ms |
47452 KB |
Output is correct |
22 |
Correct |
22 ms |
47692 KB |
Output is correct |
23 |
Correct |
20 ms |
47448 KB |
Output is correct |
24 |
Correct |
19 ms |
47452 KB |
Output is correct |
25 |
Correct |
21 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48732 KB |
Output is correct |
27 |
Correct |
24 ms |
48732 KB |
Output is correct |
28 |
Correct |
22 ms |
48732 KB |
Output is correct |
29 |
Correct |
23 ms |
48732 KB |
Output is correct |
30 |
Correct |
25 ms |
48780 KB |
Output is correct |
31 |
Correct |
28 ms |
47704 KB |
Output is correct |
32 |
Correct |
26 ms |
48696 KB |
Output is correct |
33 |
Correct |
21 ms |
47448 KB |
Output is correct |
34 |
Correct |
21 ms |
48732 KB |
Output is correct |
35 |
Correct |
22 ms |
48836 KB |
Output is correct |
36 |
Correct |
23 ms |
48728 KB |
Output is correct |
37 |
Correct |
23 ms |
48728 KB |
Output is correct |
38 |
Correct |
22 ms |
47452 KB |
Output is correct |
39 |
Correct |
26 ms |
48876 KB |
Output is correct |
40 |
Correct |
24 ms |
48732 KB |
Output is correct |
41 |
Correct |
27 ms |
48724 KB |
Output is correct |
42 |
Correct |
23 ms |
48732 KB |
Output is correct |
43 |
Correct |
25 ms |
48988 KB |
Output is correct |
44 |
Correct |
21 ms |
47448 KB |
Output is correct |
45 |
Correct |
23 ms |
48732 KB |
Output is correct |
46 |
Correct |
25 ms |
48820 KB |
Output is correct |
47 |
Correct |
23 ms |
47520 KB |
Output is correct |
48 |
Correct |
26 ms |
48732 KB |
Output is correct |
49 |
Correct |
23 ms |
48988 KB |
Output is correct |
50 |
Correct |
23 ms |
48724 KB |
Output is correct |
51 |
Correct |
30 ms |
48900 KB |
Output is correct |
52 |
Correct |
25 ms |
48732 KB |
Output is correct |
53 |
Correct |
28 ms |
48732 KB |
Output is correct |
54 |
Correct |
27 ms |
48724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47452 KB |
Output is correct |
2 |
Correct |
27 ms |
47660 KB |
Output is correct |
3 |
Correct |
20 ms |
47452 KB |
Output is correct |
4 |
Correct |
20 ms |
47636 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47696 KB |
Output is correct |
7 |
Correct |
20 ms |
47448 KB |
Output is correct |
8 |
Correct |
22 ms |
47452 KB |
Output is correct |
9 |
Correct |
22 ms |
47448 KB |
Output is correct |
10 |
Correct |
21 ms |
47452 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
23 ms |
47452 KB |
Output is correct |
13 |
Correct |
21 ms |
47652 KB |
Output is correct |
14 |
Correct |
20 ms |
47660 KB |
Output is correct |
15 |
Correct |
19 ms |
47616 KB |
Output is correct |
16 |
Correct |
19 ms |
47664 KB |
Output is correct |
17 |
Correct |
25 ms |
47452 KB |
Output is correct |
18 |
Correct |
21 ms |
47452 KB |
Output is correct |
19 |
Correct |
24 ms |
47452 KB |
Output is correct |
20 |
Correct |
20 ms |
47660 KB |
Output is correct |
21 |
Correct |
22 ms |
47452 KB |
Output is correct |
22 |
Correct |
22 ms |
47692 KB |
Output is correct |
23 |
Correct |
20 ms |
47448 KB |
Output is correct |
24 |
Correct |
19 ms |
47452 KB |
Output is correct |
25 |
Correct |
21 ms |
47448 KB |
Output is correct |
26 |
Correct |
168 ms |
87128 KB |
Output is correct |
27 |
Correct |
242 ms |
87512 KB |
Output is correct |
28 |
Correct |
25 ms |
48732 KB |
Output is correct |
29 |
Correct |
21 ms |
47448 KB |
Output is correct |
30 |
Correct |
20 ms |
47640 KB |
Output is correct |
31 |
Correct |
208 ms |
87380 KB |
Output is correct |
32 |
Correct |
23 ms |
48732 KB |
Output is correct |
33 |
Correct |
281 ms |
91984 KB |
Output is correct |
34 |
Correct |
231 ms |
87636 KB |
Output is correct |
35 |
Correct |
24 ms |
48732 KB |
Output is correct |
36 |
Correct |
263 ms |
88268 KB |
Output is correct |
37 |
Correct |
25 ms |
48732 KB |
Output is correct |
38 |
Correct |
23 ms |
48732 KB |
Output is correct |
39 |
Correct |
180 ms |
88884 KB |
Output is correct |
40 |
Correct |
26 ms |
48984 KB |
Output is correct |
41 |
Correct |
229 ms |
86996 KB |
Output is correct |
42 |
Correct |
265 ms |
89032 KB |
Output is correct |
43 |
Correct |
21 ms |
47452 KB |
Output is correct |
44 |
Correct |
324 ms |
91868 KB |
Output is correct |
45 |
Correct |
278 ms |
90196 KB |
Output is correct |
46 |
Correct |
22 ms |
48732 KB |
Output is correct |
47 |
Correct |
25 ms |
48692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
87484 KB |
Output is correct |
2 |
Correct |
152 ms |
92260 KB |
Output is correct |
3 |
Correct |
25 ms |
48732 KB |
Output is correct |
4 |
Correct |
24 ms |
48728 KB |
Output is correct |
5 |
Correct |
20 ms |
47452 KB |
Output is correct |
6 |
Correct |
21 ms |
47452 KB |
Output is correct |
7 |
Correct |
25 ms |
48732 KB |
Output is correct |
8 |
Correct |
191 ms |
88408 KB |
Output is correct |
9 |
Correct |
28 ms |
48732 KB |
Output is correct |
10 |
Correct |
219 ms |
87924 KB |
Output is correct |
11 |
Correct |
23 ms |
47452 KB |
Output is correct |
12 |
Correct |
225 ms |
88160 KB |
Output is correct |
13 |
Correct |
184 ms |
88384 KB |
Output is correct |
14 |
Correct |
129 ms |
88064 KB |
Output is correct |
15 |
Correct |
188 ms |
88868 KB |
Output is correct |
16 |
Correct |
25 ms |
48724 KB |
Output is correct |
17 |
Correct |
26 ms |
47444 KB |
Output is correct |
18 |
Correct |
190 ms |
91296 KB |
Output is correct |
19 |
Correct |
155 ms |
90452 KB |
Output is correct |
20 |
Correct |
32 ms |
48728 KB |
Output is correct |
21 |
Correct |
30 ms |
47444 KB |
Output is correct |
22 |
Correct |
184 ms |
88008 KB |
Output is correct |
23 |
Correct |
25 ms |
48728 KB |
Output is correct |
24 |
Correct |
187 ms |
88916 KB |
Output is correct |
25 |
Correct |
144 ms |
89940 KB |
Output is correct |
26 |
Correct |
24 ms |
48736 KB |
Output is correct |
27 |
Correct |
23 ms |
48732 KB |
Output is correct |
28 |
Correct |
25 ms |
48724 KB |
Output is correct |
29 |
Correct |
24 ms |
48732 KB |
Output is correct |
30 |
Correct |
23 ms |
48720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47452 KB |
Output is correct |
2 |
Correct |
27 ms |
47660 KB |
Output is correct |
3 |
Correct |
20 ms |
47452 KB |
Output is correct |
4 |
Correct |
20 ms |
47636 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47696 KB |
Output is correct |
7 |
Correct |
20 ms |
47448 KB |
Output is correct |
8 |
Correct |
22 ms |
47452 KB |
Output is correct |
9 |
Correct |
22 ms |
47448 KB |
Output is correct |
10 |
Correct |
21 ms |
47452 KB |
Output is correct |
11 |
Correct |
21 ms |
47448 KB |
Output is correct |
12 |
Correct |
23 ms |
47452 KB |
Output is correct |
13 |
Correct |
21 ms |
47652 KB |
Output is correct |
14 |
Correct |
20 ms |
47660 KB |
Output is correct |
15 |
Correct |
19 ms |
47616 KB |
Output is correct |
16 |
Correct |
19 ms |
47664 KB |
Output is correct |
17 |
Correct |
25 ms |
47452 KB |
Output is correct |
18 |
Correct |
21 ms |
47452 KB |
Output is correct |
19 |
Correct |
24 ms |
47452 KB |
Output is correct |
20 |
Correct |
20 ms |
47660 KB |
Output is correct |
21 |
Correct |
22 ms |
47452 KB |
Output is correct |
22 |
Correct |
22 ms |
47692 KB |
Output is correct |
23 |
Correct |
20 ms |
47448 KB |
Output is correct |
24 |
Correct |
19 ms |
47452 KB |
Output is correct |
25 |
Correct |
21 ms |
47452 KB |
Output is correct |
26 |
Correct |
22 ms |
48732 KB |
Output is correct |
27 |
Correct |
24 ms |
48732 KB |
Output is correct |
28 |
Correct |
22 ms |
48732 KB |
Output is correct |
29 |
Correct |
23 ms |
48732 KB |
Output is correct |
30 |
Correct |
25 ms |
48780 KB |
Output is correct |
31 |
Correct |
28 ms |
47704 KB |
Output is correct |
32 |
Correct |
26 ms |
48696 KB |
Output is correct |
33 |
Correct |
21 ms |
47448 KB |
Output is correct |
34 |
Correct |
21 ms |
48732 KB |
Output is correct |
35 |
Correct |
22 ms |
48836 KB |
Output is correct |
36 |
Correct |
23 ms |
48728 KB |
Output is correct |
37 |
Correct |
23 ms |
48728 KB |
Output is correct |
38 |
Correct |
22 ms |
47452 KB |
Output is correct |
39 |
Correct |
26 ms |
48876 KB |
Output is correct |
40 |
Correct |
24 ms |
48732 KB |
Output is correct |
41 |
Correct |
27 ms |
48724 KB |
Output is correct |
42 |
Correct |
23 ms |
48732 KB |
Output is correct |
43 |
Correct |
25 ms |
48988 KB |
Output is correct |
44 |
Correct |
21 ms |
47448 KB |
Output is correct |
45 |
Correct |
23 ms |
48732 KB |
Output is correct |
46 |
Correct |
25 ms |
48820 KB |
Output is correct |
47 |
Correct |
23 ms |
47520 KB |
Output is correct |
48 |
Correct |
26 ms |
48732 KB |
Output is correct |
49 |
Correct |
23 ms |
48988 KB |
Output is correct |
50 |
Correct |
23 ms |
48724 KB |
Output is correct |
51 |
Correct |
30 ms |
48900 KB |
Output is correct |
52 |
Correct |
25 ms |
48732 KB |
Output is correct |
53 |
Correct |
28 ms |
48732 KB |
Output is correct |
54 |
Correct |
27 ms |
48724 KB |
Output is correct |
55 |
Correct |
21 ms |
47448 KB |
Output is correct |
56 |
Correct |
168 ms |
87128 KB |
Output is correct |
57 |
Correct |
242 ms |
87512 KB |
Output is correct |
58 |
Correct |
25 ms |
48732 KB |
Output is correct |
59 |
Correct |
21 ms |
47448 KB |
Output is correct |
60 |
Correct |
20 ms |
47640 KB |
Output is correct |
61 |
Correct |
208 ms |
87380 KB |
Output is correct |
62 |
Correct |
23 ms |
48732 KB |
Output is correct |
63 |
Correct |
281 ms |
91984 KB |
Output is correct |
64 |
Correct |
231 ms |
87636 KB |
Output is correct |
65 |
Correct |
24 ms |
48732 KB |
Output is correct |
66 |
Correct |
263 ms |
88268 KB |
Output is correct |
67 |
Correct |
25 ms |
48732 KB |
Output is correct |
68 |
Correct |
23 ms |
48732 KB |
Output is correct |
69 |
Correct |
180 ms |
88884 KB |
Output is correct |
70 |
Correct |
26 ms |
48984 KB |
Output is correct |
71 |
Correct |
229 ms |
86996 KB |
Output is correct |
72 |
Correct |
265 ms |
89032 KB |
Output is correct |
73 |
Correct |
21 ms |
47452 KB |
Output is correct |
74 |
Correct |
324 ms |
91868 KB |
Output is correct |
75 |
Correct |
278 ms |
90196 KB |
Output is correct |
76 |
Correct |
22 ms |
48732 KB |
Output is correct |
77 |
Correct |
25 ms |
48692 KB |
Output is correct |
78 |
Correct |
167 ms |
87484 KB |
Output is correct |
79 |
Correct |
152 ms |
92260 KB |
Output is correct |
80 |
Correct |
25 ms |
48732 KB |
Output is correct |
81 |
Correct |
24 ms |
48728 KB |
Output is correct |
82 |
Correct |
20 ms |
47452 KB |
Output is correct |
83 |
Correct |
21 ms |
47452 KB |
Output is correct |
84 |
Correct |
25 ms |
48732 KB |
Output is correct |
85 |
Correct |
191 ms |
88408 KB |
Output is correct |
86 |
Correct |
28 ms |
48732 KB |
Output is correct |
87 |
Correct |
219 ms |
87924 KB |
Output is correct |
88 |
Correct |
23 ms |
47452 KB |
Output is correct |
89 |
Correct |
225 ms |
88160 KB |
Output is correct |
90 |
Correct |
184 ms |
88384 KB |
Output is correct |
91 |
Correct |
129 ms |
88064 KB |
Output is correct |
92 |
Correct |
188 ms |
88868 KB |
Output is correct |
93 |
Correct |
25 ms |
48724 KB |
Output is correct |
94 |
Correct |
26 ms |
47444 KB |
Output is correct |
95 |
Correct |
190 ms |
91296 KB |
Output is correct |
96 |
Correct |
155 ms |
90452 KB |
Output is correct |
97 |
Correct |
32 ms |
48728 KB |
Output is correct |
98 |
Correct |
30 ms |
47444 KB |
Output is correct |
99 |
Correct |
184 ms |
88008 KB |
Output is correct |
100 |
Correct |
25 ms |
48728 KB |
Output is correct |
101 |
Correct |
187 ms |
88916 KB |
Output is correct |
102 |
Correct |
144 ms |
89940 KB |
Output is correct |
103 |
Correct |
24 ms |
48736 KB |
Output is correct |
104 |
Correct |
23 ms |
48732 KB |
Output is correct |
105 |
Correct |
25 ms |
48724 KB |
Output is correct |
106 |
Correct |
24 ms |
48732 KB |
Output is correct |
107 |
Correct |
23 ms |
48720 KB |
Output is correct |
108 |
Correct |
1366 ms |
249936 KB |
Output is correct |
109 |
Correct |
2482 ms |
257180 KB |
Output is correct |
110 |
Correct |
2572 ms |
262144 KB |
Output is correct |
111 |
Correct |
1186 ms |
262144 KB |
Output is correct |
112 |
Correct |
2047 ms |
261636 KB |
Output is correct |
113 |
Correct |
1227 ms |
262144 KB |
Output is correct |
114 |
Correct |
2166 ms |
245752 KB |
Output is correct |
115 |
Correct |
2250 ms |
245564 KB |
Output is correct |
116 |
Correct |
2256 ms |
252624 KB |
Output is correct |
117 |
Correct |
958 ms |
251324 KB |
Output is correct |
118 |
Correct |
1660 ms |
246868 KB |
Output is correct |
119 |
Correct |
956 ms |
251576 KB |
Output is correct |
120 |
Correct |
941 ms |
261740 KB |
Output is correct |
121 |
Correct |
912 ms |
252808 KB |
Output is correct |
122 |
Correct |
2048 ms |
253288 KB |
Output is correct |
123 |
Runtime error |
799 ms |
262144 KB |
Execution killed with signal 9 |
124 |
Halted |
0 ms |
0 KB |
- |