/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
typedef long long ll;
const int N=5e5+7,lg=21;
int n,k,st[N],fn[N],t,r,s[N],mark[N],sum[N],ans,h[N],mn[N],sp[N][lg],P[N];
vector<int>vec[N],ver[N],x;
int lca(){
int m=N,g;
for(auto u:x){
if(h[u]<m){
m=h[u];
g=u;
}
}
for(int i=lg-1;i>=0;i--){
for(int j=0;j<x.size();j++){
int u=x[j];
if(h[sp[u][i]]>=m){
x[j]=sp[u][i];
}
}
}
int f=1;
for(auto u:x){
if(u!=g)f=0;
}
if(f)return g;
for(int i=lg-1;i>=0;i--){
f=0;
for(int j=1;j<x.size();j++){
if(sp[x[j]][i]!=sp[x[0]][i])f=1;
}
if(f){
for(int j=0;j<x.size();j++){
x[j]=sp[x[j]][i];
}
}
}
return sp[x[0]][0];
}
void dfs1(int v=1,int p=0){
h[v]=h[p]+1;
sp[v][0]=p;
st[v]=t++;
for(auto u:vec[v]){
if(u!=p) dfs1(u,v);
}
fn[v]=t++;
}
void dfs2(int v=1,int p=0){
mn[v]=h[P[s[v]]];
for(auto u:vec[v]){
if(u==p)continue;
dfs2(u,v);
sum[v]+=sum[u];
mn[v]=min(mn[v],mn[u]);
}
if(v!=1 && h[v]<=mn[v])mark[v]=1;
sum[v]+=mark[v];
}
void dfs3(int v=1,int p=0){
if(mark[v] && (sum[v]==1 || sum[v]==sum[1]))ans++;
for(auto u:vec[v]){
if(u==p)continue;
dfs3(u,v);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
int u,v;
for(int i=0;i<n-1;i++){
cin>>u>>v;
vec[u].pb(v);
vec[v].pb(u);
}
for(int i=1;i<=n;i++){
cin>>s[i];
ver[s[i]].pb(i);
}
dfs1();
for(int j=1;j<lg;j++){
for(int i=1;i<=n;i++){
sp[i][j]=sp[sp[i][j-1]][j-1];
}
}
for(int i=1;i<=k;i++){
x.clear();
for(auto u:ver[i])x.pb(u);
P[i]=lca();
}
dfs2();
dfs3();
cout<<ans/2+(ans%2==1);
}
Compilation message
mergers.cpp: In function 'int lca()':
mergers.cpp:25:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j=0;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:39:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j=1;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:43:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int j=0;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:17:10: warning: 'g' may be used uninitialized in this function [-Wmaybe-uninitialized]
17 | int m=N,g;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23812 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
11 ms |
23832 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23860 KB |
Output is correct |
16 |
Correct |
12 ms |
23812 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23804 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23828 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23888 KB |
Output is correct |
23 |
Correct |
11 ms |
23784 KB |
Output is correct |
24 |
Correct |
11 ms |
23836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23812 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
11 ms |
23832 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23860 KB |
Output is correct |
16 |
Correct |
12 ms |
23812 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23804 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23828 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23888 KB |
Output is correct |
23 |
Correct |
11 ms |
23784 KB |
Output is correct |
24 |
Correct |
11 ms |
23836 KB |
Output is correct |
25 |
Correct |
11 ms |
23816 KB |
Output is correct |
26 |
Correct |
13 ms |
24332 KB |
Output is correct |
27 |
Correct |
12 ms |
24276 KB |
Output is correct |
28 |
Correct |
14 ms |
24524 KB |
Output is correct |
29 |
Correct |
16 ms |
24392 KB |
Output is correct |
30 |
Correct |
13 ms |
24292 KB |
Output is correct |
31 |
Correct |
12 ms |
23836 KB |
Output is correct |
32 |
Correct |
14 ms |
24500 KB |
Output is correct |
33 |
Correct |
11 ms |
23844 KB |
Output is correct |
34 |
Correct |
15 ms |
24340 KB |
Output is correct |
35 |
Correct |
12 ms |
24336 KB |
Output is correct |
36 |
Correct |
13 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
13 ms |
24340 KB |
Output is correct |
40 |
Correct |
13 ms |
24276 KB |
Output is correct |
41 |
Correct |
13 ms |
24380 KB |
Output is correct |
42 |
Correct |
13 ms |
24404 KB |
Output is correct |
43 |
Correct |
16 ms |
24536 KB |
Output is correct |
44 |
Correct |
14 ms |
23892 KB |
Output is correct |
45 |
Correct |
17 ms |
24404 KB |
Output is correct |
46 |
Correct |
13 ms |
24336 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24276 KB |
Output is correct |
49 |
Correct |
12 ms |
24404 KB |
Output is correct |
50 |
Correct |
13 ms |
24508 KB |
Output is correct |
51 |
Correct |
16 ms |
24336 KB |
Output is correct |
52 |
Correct |
13 ms |
24276 KB |
Output is correct |
53 |
Correct |
13 ms |
24332 KB |
Output is correct |
54 |
Correct |
13 ms |
24340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23812 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
11 ms |
23832 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23860 KB |
Output is correct |
16 |
Correct |
12 ms |
23812 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23804 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23828 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23888 KB |
Output is correct |
23 |
Correct |
11 ms |
23784 KB |
Output is correct |
24 |
Correct |
11 ms |
23836 KB |
Output is correct |
25 |
Correct |
15 ms |
23792 KB |
Output is correct |
26 |
Correct |
80 ms |
39324 KB |
Output is correct |
27 |
Correct |
87 ms |
39628 KB |
Output is correct |
28 |
Correct |
13 ms |
24276 KB |
Output is correct |
29 |
Correct |
12 ms |
23816 KB |
Output is correct |
30 |
Correct |
12 ms |
23760 KB |
Output is correct |
31 |
Correct |
80 ms |
39760 KB |
Output is correct |
32 |
Correct |
13 ms |
24336 KB |
Output is correct |
33 |
Correct |
95 ms |
46708 KB |
Output is correct |
34 |
Correct |
70 ms |
39628 KB |
Output is correct |
35 |
Correct |
14 ms |
24344 KB |
Output is correct |
36 |
Correct |
80 ms |
40332 KB |
Output is correct |
37 |
Correct |
14 ms |
24276 KB |
Output is correct |
38 |
Correct |
13 ms |
24276 KB |
Output is correct |
39 |
Correct |
68 ms |
39844 KB |
Output is correct |
40 |
Correct |
14 ms |
24588 KB |
Output is correct |
41 |
Correct |
71 ms |
39912 KB |
Output is correct |
42 |
Correct |
88 ms |
41860 KB |
Output is correct |
43 |
Correct |
12 ms |
23840 KB |
Output is correct |
44 |
Correct |
94 ms |
47316 KB |
Output is correct |
45 |
Correct |
87 ms |
43288 KB |
Output is correct |
46 |
Correct |
13 ms |
24276 KB |
Output is correct |
47 |
Correct |
14 ms |
24276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
39520 KB |
Output is correct |
2 |
Correct |
73 ms |
42692 KB |
Output is correct |
3 |
Correct |
14 ms |
24404 KB |
Output is correct |
4 |
Correct |
15 ms |
24464 KB |
Output is correct |
5 |
Correct |
12 ms |
23864 KB |
Output is correct |
6 |
Correct |
13 ms |
23860 KB |
Output is correct |
7 |
Correct |
13 ms |
24276 KB |
Output is correct |
8 |
Correct |
87 ms |
41536 KB |
Output is correct |
9 |
Correct |
14 ms |
24276 KB |
Output is correct |
10 |
Correct |
86 ms |
40100 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
77 ms |
40112 KB |
Output is correct |
13 |
Correct |
84 ms |
41384 KB |
Output is correct |
14 |
Correct |
86 ms |
43252 KB |
Output is correct |
15 |
Correct |
58 ms |
39828 KB |
Output is correct |
16 |
Correct |
13 ms |
24332 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
72 ms |
43248 KB |
Output is correct |
19 |
Correct |
116 ms |
47180 KB |
Output is correct |
20 |
Correct |
13 ms |
24380 KB |
Output is correct |
21 |
Correct |
12 ms |
23828 KB |
Output is correct |
22 |
Correct |
65 ms |
41720 KB |
Output is correct |
23 |
Correct |
13 ms |
24276 KB |
Output is correct |
24 |
Correct |
83 ms |
40512 KB |
Output is correct |
25 |
Correct |
93 ms |
45812 KB |
Output is correct |
26 |
Correct |
16 ms |
24332 KB |
Output is correct |
27 |
Correct |
14 ms |
24488 KB |
Output is correct |
28 |
Correct |
13 ms |
24348 KB |
Output is correct |
29 |
Correct |
13 ms |
24336 KB |
Output is correct |
30 |
Correct |
13 ms |
24332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23812 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
11 ms |
23832 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23860 KB |
Output is correct |
16 |
Correct |
12 ms |
23812 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23804 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23828 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23888 KB |
Output is correct |
23 |
Correct |
11 ms |
23784 KB |
Output is correct |
24 |
Correct |
11 ms |
23836 KB |
Output is correct |
25 |
Correct |
11 ms |
23816 KB |
Output is correct |
26 |
Correct |
13 ms |
24332 KB |
Output is correct |
27 |
Correct |
12 ms |
24276 KB |
Output is correct |
28 |
Correct |
14 ms |
24524 KB |
Output is correct |
29 |
Correct |
16 ms |
24392 KB |
Output is correct |
30 |
Correct |
13 ms |
24292 KB |
Output is correct |
31 |
Correct |
12 ms |
23836 KB |
Output is correct |
32 |
Correct |
14 ms |
24500 KB |
Output is correct |
33 |
Correct |
11 ms |
23844 KB |
Output is correct |
34 |
Correct |
15 ms |
24340 KB |
Output is correct |
35 |
Correct |
12 ms |
24336 KB |
Output is correct |
36 |
Correct |
13 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
13 ms |
24340 KB |
Output is correct |
40 |
Correct |
13 ms |
24276 KB |
Output is correct |
41 |
Correct |
13 ms |
24380 KB |
Output is correct |
42 |
Correct |
13 ms |
24404 KB |
Output is correct |
43 |
Correct |
16 ms |
24536 KB |
Output is correct |
44 |
Correct |
14 ms |
23892 KB |
Output is correct |
45 |
Correct |
17 ms |
24404 KB |
Output is correct |
46 |
Correct |
13 ms |
24336 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24276 KB |
Output is correct |
49 |
Correct |
12 ms |
24404 KB |
Output is correct |
50 |
Correct |
13 ms |
24508 KB |
Output is correct |
51 |
Correct |
16 ms |
24336 KB |
Output is correct |
52 |
Correct |
13 ms |
24276 KB |
Output is correct |
53 |
Correct |
13 ms |
24332 KB |
Output is correct |
54 |
Correct |
13 ms |
24340 KB |
Output is correct |
55 |
Correct |
15 ms |
23792 KB |
Output is correct |
56 |
Correct |
80 ms |
39324 KB |
Output is correct |
57 |
Correct |
87 ms |
39628 KB |
Output is correct |
58 |
Correct |
13 ms |
24276 KB |
Output is correct |
59 |
Correct |
12 ms |
23816 KB |
Output is correct |
60 |
Correct |
12 ms |
23760 KB |
Output is correct |
61 |
Correct |
80 ms |
39760 KB |
Output is correct |
62 |
Correct |
13 ms |
24336 KB |
Output is correct |
63 |
Correct |
95 ms |
46708 KB |
Output is correct |
64 |
Correct |
70 ms |
39628 KB |
Output is correct |
65 |
Correct |
14 ms |
24344 KB |
Output is correct |
66 |
Correct |
80 ms |
40332 KB |
Output is correct |
67 |
Correct |
14 ms |
24276 KB |
Output is correct |
68 |
Correct |
13 ms |
24276 KB |
Output is correct |
69 |
Correct |
68 ms |
39844 KB |
Output is correct |
70 |
Correct |
14 ms |
24588 KB |
Output is correct |
71 |
Correct |
71 ms |
39912 KB |
Output is correct |
72 |
Correct |
88 ms |
41860 KB |
Output is correct |
73 |
Correct |
12 ms |
23840 KB |
Output is correct |
74 |
Correct |
94 ms |
47316 KB |
Output is correct |
75 |
Correct |
87 ms |
43288 KB |
Output is correct |
76 |
Correct |
13 ms |
24276 KB |
Output is correct |
77 |
Correct |
14 ms |
24276 KB |
Output is correct |
78 |
Correct |
56 ms |
39520 KB |
Output is correct |
79 |
Correct |
73 ms |
42692 KB |
Output is correct |
80 |
Correct |
14 ms |
24404 KB |
Output is correct |
81 |
Correct |
15 ms |
24464 KB |
Output is correct |
82 |
Correct |
12 ms |
23864 KB |
Output is correct |
83 |
Correct |
13 ms |
23860 KB |
Output is correct |
84 |
Correct |
13 ms |
24276 KB |
Output is correct |
85 |
Correct |
87 ms |
41536 KB |
Output is correct |
86 |
Correct |
14 ms |
24276 KB |
Output is correct |
87 |
Correct |
86 ms |
40100 KB |
Output is correct |
88 |
Correct |
13 ms |
23892 KB |
Output is correct |
89 |
Correct |
77 ms |
40112 KB |
Output is correct |
90 |
Correct |
84 ms |
41384 KB |
Output is correct |
91 |
Correct |
86 ms |
43252 KB |
Output is correct |
92 |
Correct |
58 ms |
39828 KB |
Output is correct |
93 |
Correct |
13 ms |
24332 KB |
Output is correct |
94 |
Correct |
13 ms |
23892 KB |
Output is correct |
95 |
Correct |
72 ms |
43248 KB |
Output is correct |
96 |
Correct |
116 ms |
47180 KB |
Output is correct |
97 |
Correct |
13 ms |
24380 KB |
Output is correct |
98 |
Correct |
12 ms |
23828 KB |
Output is correct |
99 |
Correct |
65 ms |
41720 KB |
Output is correct |
100 |
Correct |
13 ms |
24276 KB |
Output is correct |
101 |
Correct |
83 ms |
40512 KB |
Output is correct |
102 |
Correct |
93 ms |
45812 KB |
Output is correct |
103 |
Correct |
16 ms |
24332 KB |
Output is correct |
104 |
Correct |
14 ms |
24488 KB |
Output is correct |
105 |
Correct |
13 ms |
24348 KB |
Output is correct |
106 |
Correct |
13 ms |
24336 KB |
Output is correct |
107 |
Correct |
13 ms |
24332 KB |
Output is correct |
108 |
Correct |
504 ms |
113464 KB |
Output is correct |
109 |
Correct |
898 ms |
121720 KB |
Output is correct |
110 |
Correct |
896 ms |
133692 KB |
Output is correct |
111 |
Correct |
1039 ms |
145512 KB |
Output is correct |
112 |
Correct |
988 ms |
135532 KB |
Output is correct |
113 |
Correct |
544 ms |
122124 KB |
Output is correct |
114 |
Correct |
611 ms |
103532 KB |
Output is correct |
115 |
Correct |
618 ms |
104404 KB |
Output is correct |
116 |
Correct |
685 ms |
109340 KB |
Output is correct |
117 |
Correct |
773 ms |
122356 KB |
Output is correct |
118 |
Correct |
647 ms |
104476 KB |
Output is correct |
119 |
Correct |
790 ms |
122368 KB |
Output is correct |
120 |
Correct |
951 ms |
136212 KB |
Output is correct |
121 |
Correct |
811 ms |
122348 KB |
Output is correct |
122 |
Correct |
725 ms |
115280 KB |
Output is correct |
123 |
Correct |
560 ms |
123832 KB |
Output is correct |
124 |
Correct |
948 ms |
120184 KB |
Output is correct |