#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=500050;
const int L=19;
vector<int> my[N],E[N];
int s[N],par[N][L],dep[N],sum[N];
void DFS(int u, int p)
{
par[u][0]=p;
dep[u]=dep[p]+1;
for(int i=1;i<L;i++) par[u][i]=par[par[u][i-1]][i-1];
for(int v:E[u]) if(v!=p) DFS(v,u);
}
int LCA(int u, int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int i=L-1;~i;i--) if(dep[par[u][i]]>=dep[v]) u=par[u][i];
for(int i=L-1;~i;i--) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
return u==v?v:par[v][0];
}
void Solve(int u, int p)
{
for(int v:E[u]) if(v!=p) Solve(v,u),sum[u]+=sum[v];
}
int csz,deg[N];
void Compress(int u, int p, int c)
{
for(int v:E[u]) if(v!=p)
{
if(sum[v]==0)
{
csz++;
deg[c]++;
deg[csz]++;
Compress(v,u,csz);
}
else Compress(v,u,c);
}
}
int main()
{
int n,k,u,v;
scanf("%i %i",&n,&k);
for(int i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
for(int i=1;i<=n;i++) scanf("%i",&s[i]),my[s[i]].pb(i);
DFS(1,0);
for(int i=1;i<=k;i++)
{
for(int j=1;j<my[i].size();j++)
{
int lca=LCA(my[i][j-1],my[i][j]);
sum[my[i][j-1]]++;
sum[my[i][j]]++;
sum[lca]-=2;
}
}
Solve(1,0);
csz=1;
Compress(1,0,1);
int cnt=0;
for(int i=1;i<=csz;i++) if(deg[i]==1) cnt++;
int ans=cnt+1>>1;
printf("%i\n",ans);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:50:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=1;j<my[i].size();j++)
~^~~~~~~~~~~~~
mergers.cpp:63:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int ans=cnt+1>>1;
~~~^~
mergers.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&k);
~~~~~^~~~~~~~~~~~~~~
mergers.cpp:45:54: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
mergers.cpp:46:41: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%i",&s[i]),my[s[i]].pb(i);
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23872 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23864 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23804 KB |
Output is correct |
13 |
Correct |
23 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
23 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23872 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23864 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23804 KB |
Output is correct |
13 |
Correct |
23 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
23 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23832 KB |
Output is correct |
25 |
Correct |
23 ms |
24056 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
31 ms |
24440 KB |
Output is correct |
29 |
Correct |
32 ms |
24316 KB |
Output is correct |
30 |
Correct |
27 ms |
24312 KB |
Output is correct |
31 |
Correct |
23 ms |
23800 KB |
Output is correct |
32 |
Correct |
26 ms |
24440 KB |
Output is correct |
33 |
Correct |
23 ms |
23800 KB |
Output is correct |
34 |
Correct |
25 ms |
24312 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24184 KB |
Output is correct |
37 |
Correct |
27 ms |
24312 KB |
Output is correct |
38 |
Correct |
23 ms |
23800 KB |
Output is correct |
39 |
Correct |
26 ms |
24312 KB |
Output is correct |
40 |
Correct |
27 ms |
24312 KB |
Output is correct |
41 |
Correct |
26 ms |
24184 KB |
Output is correct |
42 |
Correct |
26 ms |
24316 KB |
Output is correct |
43 |
Correct |
27 ms |
24440 KB |
Output is correct |
44 |
Correct |
23 ms |
23800 KB |
Output is correct |
45 |
Correct |
26 ms |
24420 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
23 ms |
23788 KB |
Output is correct |
48 |
Correct |
26 ms |
24320 KB |
Output is correct |
49 |
Correct |
25 ms |
24312 KB |
Output is correct |
50 |
Correct |
25 ms |
24440 KB |
Output is correct |
51 |
Correct |
30 ms |
24312 KB |
Output is correct |
52 |
Correct |
26 ms |
24312 KB |
Output is correct |
53 |
Correct |
26 ms |
24312 KB |
Output is correct |
54 |
Correct |
26 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23872 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23864 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23804 KB |
Output is correct |
13 |
Correct |
23 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
23 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23832 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
140 ms |
37996 KB |
Output is correct |
27 |
Correct |
169 ms |
37596 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
23 ms |
23928 KB |
Output is correct |
30 |
Correct |
23 ms |
23928 KB |
Output is correct |
31 |
Correct |
172 ms |
37596 KB |
Output is correct |
32 |
Correct |
25 ms |
24312 KB |
Output is correct |
33 |
Correct |
270 ms |
43412 KB |
Output is correct |
34 |
Correct |
177 ms |
37724 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
206 ms |
38264 KB |
Output is correct |
37 |
Correct |
26 ms |
24312 KB |
Output is correct |
38 |
Correct |
26 ms |
24184 KB |
Output is correct |
39 |
Correct |
142 ms |
38064 KB |
Output is correct |
40 |
Correct |
27 ms |
24440 KB |
Output is correct |
41 |
Correct |
178 ms |
37592 KB |
Output is correct |
42 |
Correct |
295 ms |
39416 KB |
Output is correct |
43 |
Correct |
24 ms |
23804 KB |
Output is correct |
44 |
Correct |
283 ms |
43580 KB |
Output is correct |
45 |
Correct |
215 ms |
40568 KB |
Output is correct |
46 |
Correct |
28 ms |
24320 KB |
Output is correct |
47 |
Correct |
29 ms |
24316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
38000 KB |
Output is correct |
2 |
Correct |
120 ms |
40900 KB |
Output is correct |
3 |
Correct |
26 ms |
24312 KB |
Output is correct |
4 |
Correct |
27 ms |
24312 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
26 ms |
24312 KB |
Output is correct |
8 |
Correct |
181 ms |
39032 KB |
Output is correct |
9 |
Correct |
49 ms |
24184 KB |
Output is correct |
10 |
Correct |
203 ms |
37812 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
206 ms |
37852 KB |
Output is correct |
13 |
Correct |
188 ms |
39032 KB |
Output is correct |
14 |
Correct |
172 ms |
41052 KB |
Output is correct |
15 |
Correct |
141 ms |
37872 KB |
Output is correct |
16 |
Correct |
25 ms |
24312 KB |
Output is correct |
17 |
Correct |
29 ms |
23928 KB |
Output is correct |
18 |
Correct |
137 ms |
40844 KB |
Output is correct |
19 |
Correct |
182 ms |
44152 KB |
Output is correct |
20 |
Correct |
27 ms |
24312 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
134 ms |
39280 KB |
Output is correct |
23 |
Correct |
27 ms |
24312 KB |
Output is correct |
24 |
Correct |
191 ms |
38200 KB |
Output is correct |
25 |
Correct |
160 ms |
43000 KB |
Output is correct |
26 |
Correct |
25 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24440 KB |
Output is correct |
28 |
Correct |
27 ms |
24312 KB |
Output is correct |
29 |
Correct |
26 ms |
24312 KB |
Output is correct |
30 |
Correct |
25 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23872 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23864 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23804 KB |
Output is correct |
13 |
Correct |
23 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
23 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23832 KB |
Output is correct |
25 |
Correct |
23 ms |
24056 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
31 ms |
24440 KB |
Output is correct |
29 |
Correct |
32 ms |
24316 KB |
Output is correct |
30 |
Correct |
27 ms |
24312 KB |
Output is correct |
31 |
Correct |
23 ms |
23800 KB |
Output is correct |
32 |
Correct |
26 ms |
24440 KB |
Output is correct |
33 |
Correct |
23 ms |
23800 KB |
Output is correct |
34 |
Correct |
25 ms |
24312 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24184 KB |
Output is correct |
37 |
Correct |
27 ms |
24312 KB |
Output is correct |
38 |
Correct |
23 ms |
23800 KB |
Output is correct |
39 |
Correct |
26 ms |
24312 KB |
Output is correct |
40 |
Correct |
27 ms |
24312 KB |
Output is correct |
41 |
Correct |
26 ms |
24184 KB |
Output is correct |
42 |
Correct |
26 ms |
24316 KB |
Output is correct |
43 |
Correct |
27 ms |
24440 KB |
Output is correct |
44 |
Correct |
23 ms |
23800 KB |
Output is correct |
45 |
Correct |
26 ms |
24420 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
23 ms |
23788 KB |
Output is correct |
48 |
Correct |
26 ms |
24320 KB |
Output is correct |
49 |
Correct |
25 ms |
24312 KB |
Output is correct |
50 |
Correct |
25 ms |
24440 KB |
Output is correct |
51 |
Correct |
30 ms |
24312 KB |
Output is correct |
52 |
Correct |
26 ms |
24312 KB |
Output is correct |
53 |
Correct |
26 ms |
24312 KB |
Output is correct |
54 |
Correct |
26 ms |
24312 KB |
Output is correct |
55 |
Correct |
23 ms |
23800 KB |
Output is correct |
56 |
Correct |
140 ms |
37996 KB |
Output is correct |
57 |
Correct |
169 ms |
37596 KB |
Output is correct |
58 |
Correct |
26 ms |
24184 KB |
Output is correct |
59 |
Correct |
23 ms |
23928 KB |
Output is correct |
60 |
Correct |
23 ms |
23928 KB |
Output is correct |
61 |
Correct |
172 ms |
37596 KB |
Output is correct |
62 |
Correct |
25 ms |
24312 KB |
Output is correct |
63 |
Correct |
270 ms |
43412 KB |
Output is correct |
64 |
Correct |
177 ms |
37724 KB |
Output is correct |
65 |
Correct |
26 ms |
24312 KB |
Output is correct |
66 |
Correct |
206 ms |
38264 KB |
Output is correct |
67 |
Correct |
26 ms |
24312 KB |
Output is correct |
68 |
Correct |
26 ms |
24184 KB |
Output is correct |
69 |
Correct |
142 ms |
38064 KB |
Output is correct |
70 |
Correct |
27 ms |
24440 KB |
Output is correct |
71 |
Correct |
178 ms |
37592 KB |
Output is correct |
72 |
Correct |
295 ms |
39416 KB |
Output is correct |
73 |
Correct |
24 ms |
23804 KB |
Output is correct |
74 |
Correct |
283 ms |
43580 KB |
Output is correct |
75 |
Correct |
215 ms |
40568 KB |
Output is correct |
76 |
Correct |
28 ms |
24320 KB |
Output is correct |
77 |
Correct |
29 ms |
24316 KB |
Output is correct |
78 |
Correct |
165 ms |
38000 KB |
Output is correct |
79 |
Correct |
120 ms |
40900 KB |
Output is correct |
80 |
Correct |
26 ms |
24312 KB |
Output is correct |
81 |
Correct |
27 ms |
24312 KB |
Output is correct |
82 |
Correct |
23 ms |
23800 KB |
Output is correct |
83 |
Correct |
23 ms |
23928 KB |
Output is correct |
84 |
Correct |
26 ms |
24312 KB |
Output is correct |
85 |
Correct |
181 ms |
39032 KB |
Output is correct |
86 |
Correct |
49 ms |
24184 KB |
Output is correct |
87 |
Correct |
203 ms |
37812 KB |
Output is correct |
88 |
Correct |
24 ms |
23800 KB |
Output is correct |
89 |
Correct |
206 ms |
37852 KB |
Output is correct |
90 |
Correct |
188 ms |
39032 KB |
Output is correct |
91 |
Correct |
172 ms |
41052 KB |
Output is correct |
92 |
Correct |
141 ms |
37872 KB |
Output is correct |
93 |
Correct |
25 ms |
24312 KB |
Output is correct |
94 |
Correct |
29 ms |
23928 KB |
Output is correct |
95 |
Correct |
137 ms |
40844 KB |
Output is correct |
96 |
Correct |
182 ms |
44152 KB |
Output is correct |
97 |
Correct |
27 ms |
24312 KB |
Output is correct |
98 |
Correct |
23 ms |
23800 KB |
Output is correct |
99 |
Correct |
134 ms |
39280 KB |
Output is correct |
100 |
Correct |
27 ms |
24312 KB |
Output is correct |
101 |
Correct |
191 ms |
38200 KB |
Output is correct |
102 |
Correct |
160 ms |
43000 KB |
Output is correct |
103 |
Correct |
25 ms |
24312 KB |
Output is correct |
104 |
Correct |
26 ms |
24440 KB |
Output is correct |
105 |
Correct |
27 ms |
24312 KB |
Output is correct |
106 |
Correct |
26 ms |
24312 KB |
Output is correct |
107 |
Correct |
25 ms |
24312 KB |
Output is correct |
108 |
Correct |
821 ms |
101132 KB |
Output is correct |
109 |
Correct |
1969 ms |
107008 KB |
Output is correct |
110 |
Correct |
2306 ms |
116168 KB |
Output is correct |
111 |
Correct |
1378 ms |
128972 KB |
Output is correct |
112 |
Correct |
1149 ms |
120324 KB |
Output is correct |
113 |
Correct |
728 ms |
110168 KB |
Output is correct |
114 |
Correct |
1040 ms |
92900 KB |
Output is correct |
115 |
Correct |
1041 ms |
92872 KB |
Output is correct |
116 |
Correct |
1249 ms |
97528 KB |
Output is correct |
117 |
Correct |
1008 ms |
110824 KB |
Output is correct |
118 |
Correct |
1178 ms |
93304 KB |
Output is correct |
119 |
Correct |
1055 ms |
110764 KB |
Output is correct |
120 |
Correct |
1139 ms |
121780 KB |
Output is correct |
121 |
Correct |
1008 ms |
110712 KB |
Output is correct |
122 |
Correct |
1133 ms |
102728 KB |
Output is correct |
123 |
Correct |
725 ms |
110436 KB |
Output is correct |
124 |
Correct |
1088 ms |
108152 KB |
Output is correct |