#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define inf 2000000000000000
#define N 500005
#define MOD 998244353
#define KOK 700
using namespace std;
int n,k,root,cnt;
int sub[N],has[N],h[N],dad[N],u[N],s[N],rem[N];
vector<int> v[N],v2[N];
void dfs6(int node,int ata) {
for(int i:v2[node]) {
if(i==ata) continue ;
dfs6(i,node);
rem[node]+=rem[i];
}
if(sz(v2[node])==1) {
rem[node]=1;
return ;
}
cnt+=rem[node]/2;
rem[node]&=1;
}
int find(int x) {return dad[x]==x?x:(dad[x]=find(dad[x]));}
void merge(int x,int y) {
while((x=find(x))!=find(root)) {
dad[x]=find(u[x]);
x=dad[x];
}
while((y=find(y))!=find(root)) {
dad[y]=find(u[y]);
y=dad[y];
}
}
void add(int node) {
if(!has[s[node]]) has[s[node]]=node;
}
void ask(int node) {
if(!has[s[node]]) return ;
merge(has[s[node]],node);
}
void dfs5(int node,int ata) {
has[s[node]]=0;
for(int i:v[node]) {
if(i==ata) continue ;
dfs5(i,node);
}
}
void dfs4(int node,int ata) {
add(node);
for(int i:v[node]) {
if(i==ata) continue ;
dfs4(i,node);
}
}
void dfs3(int node,int ata) {
ask(node);
for(int i:v[node]) {
if(i==ata) continue ;
dfs3(i,node);
}
}
void dfs2(int node,int ata,bool up) {
for(int i:v[node]) {
if(i==ata || i==h[node]) continue ;
dfs2(i,node,0);
}
if(h[node]) dfs2(h[node],node,1);
ask(root=node);
add(root=node);
for(int i:v[node]) {
if(i==ata || i==h[node]) continue ;
dfs3(i,node);
dfs4(i,node);
}
if(!up) {
dfs5(node,ata);
}
}
void dfs(int node,int ata) {
sub[node]=1;
for(int i:v[node]) {
if(i==ata) continue ;
dfs(i,node);
sub[node]+=sub[i];
if(!h[node]) h[node]=i;
else if(sub[i]>sub[h[node]]) h[node]=i;
}
u[node]=ata;dad[node]=node;
}
int main() {
scanf("%d %d",&n,&k);
for(int i=1;i<n;i++) {
int x,y;
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
for(int i=1;i<=n;i++) {
scanf("%d",s+i);
}
dfs(1,0);
dfs2(1,0,0);
map<ii,int> pr;
for(int i=1;i<=n;i++) {
for(auto x:v[i]) {
if(find(i)!=find(x) && !pr.count({find(i),find(x)})) {
pr[{find(i),find(x)}]=pr[{find(x),find(i)}]=1;
v2[find(i)].pb(find(x));
v2[find(x)].pb(find(i));
}
}
}
dfs6(find(1),0);
printf("%d",cnt+rem[find(1)]);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:180:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&k);
~~~~~^~~~~~~~~~~~~~~
mergers.cpp:186:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
mergers.cpp:195:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",s+i);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
27 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23804 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
29 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23932 KB |
Output is correct |
20 |
Correct |
24 ms |
23932 KB |
Output is correct |
21 |
Correct |
23 ms |
23932 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
27 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23804 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
29 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23932 KB |
Output is correct |
20 |
Correct |
24 ms |
23932 KB |
Output is correct |
21 |
Correct |
23 ms |
23932 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
24 ms |
23928 KB |
Output is correct |
25 |
Correct |
24 ms |
23928 KB |
Output is correct |
26 |
Correct |
28 ms |
24440 KB |
Output is correct |
27 |
Correct |
26 ms |
24056 KB |
Output is correct |
28 |
Correct |
27 ms |
24440 KB |
Output is correct |
29 |
Correct |
30 ms |
24568 KB |
Output is correct |
30 |
Correct |
27 ms |
24056 KB |
Output is correct |
31 |
Correct |
24 ms |
23948 KB |
Output is correct |
32 |
Correct |
31 ms |
24312 KB |
Output is correct |
33 |
Correct |
23 ms |
23928 KB |
Output is correct |
34 |
Correct |
25 ms |
24056 KB |
Output is correct |
35 |
Correct |
29 ms |
24568 KB |
Output is correct |
36 |
Correct |
26 ms |
24056 KB |
Output is correct |
37 |
Correct |
27 ms |
24184 KB |
Output is correct |
38 |
Correct |
24 ms |
23928 KB |
Output is correct |
39 |
Correct |
27 ms |
24184 KB |
Output is correct |
40 |
Correct |
27 ms |
24056 KB |
Output is correct |
41 |
Correct |
26 ms |
24056 KB |
Output is correct |
42 |
Correct |
30 ms |
24104 KB |
Output is correct |
43 |
Correct |
31 ms |
24184 KB |
Output is correct |
44 |
Correct |
25 ms |
23800 KB |
Output is correct |
45 |
Correct |
26 ms |
24184 KB |
Output is correct |
46 |
Correct |
26 ms |
24188 KB |
Output is correct |
47 |
Correct |
24 ms |
23928 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
27 ms |
24568 KB |
Output is correct |
50 |
Correct |
29 ms |
24568 KB |
Output is correct |
51 |
Correct |
32 ms |
24184 KB |
Output is correct |
52 |
Correct |
30 ms |
24056 KB |
Output is correct |
53 |
Correct |
31 ms |
24184 KB |
Output is correct |
54 |
Correct |
26 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
27 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23804 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
29 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23932 KB |
Output is correct |
20 |
Correct |
24 ms |
23932 KB |
Output is correct |
21 |
Correct |
23 ms |
23932 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
24 ms |
23928 KB |
Output is correct |
25 |
Correct |
24 ms |
23928 KB |
Output is correct |
26 |
Correct |
99 ms |
30416 KB |
Output is correct |
27 |
Correct |
189 ms |
30432 KB |
Output is correct |
28 |
Correct |
26 ms |
24056 KB |
Output is correct |
29 |
Correct |
24 ms |
23928 KB |
Output is correct |
30 |
Correct |
24 ms |
23928 KB |
Output is correct |
31 |
Correct |
185 ms |
30724 KB |
Output is correct |
32 |
Correct |
31 ms |
24056 KB |
Output is correct |
33 |
Correct |
179 ms |
37852 KB |
Output is correct |
34 |
Correct |
190 ms |
30456 KB |
Output is correct |
35 |
Correct |
26 ms |
24056 KB |
Output is correct |
36 |
Correct |
184 ms |
31352 KB |
Output is correct |
37 |
Correct |
32 ms |
24056 KB |
Output is correct |
38 |
Correct |
28 ms |
24056 KB |
Output is correct |
39 |
Correct |
99 ms |
30320 KB |
Output is correct |
40 |
Correct |
26 ms |
24312 KB |
Output is correct |
41 |
Correct |
192 ms |
30428 KB |
Output is correct |
42 |
Correct |
164 ms |
32864 KB |
Output is correct |
43 |
Correct |
23 ms |
23800 KB |
Output is correct |
44 |
Correct |
177 ms |
38008 KB |
Output is correct |
45 |
Correct |
157 ms |
34172 KB |
Output is correct |
46 |
Correct |
26 ms |
24056 KB |
Output is correct |
47 |
Correct |
26 ms |
24060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
28912 KB |
Output is correct |
2 |
Correct |
345 ms |
45800 KB |
Output is correct |
3 |
Correct |
29 ms |
24312 KB |
Output is correct |
4 |
Correct |
32 ms |
24056 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
25 ms |
24056 KB |
Output is correct |
8 |
Correct |
233 ms |
33144 KB |
Output is correct |
9 |
Correct |
26 ms |
24184 KB |
Output is correct |
10 |
Correct |
180 ms |
31068 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
240 ms |
31068 KB |
Output is correct |
13 |
Correct |
237 ms |
34680 KB |
Output is correct |
14 |
Correct |
381 ms |
47352 KB |
Output is correct |
15 |
Correct |
99 ms |
30320 KB |
Output is correct |
16 |
Correct |
26 ms |
24228 KB |
Output is correct |
17 |
Correct |
29 ms |
23928 KB |
Output is correct |
18 |
Correct |
354 ms |
44388 KB |
Output is correct |
19 |
Correct |
439 ms |
51192 KB |
Output is correct |
20 |
Correct |
27 ms |
24056 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
142 ms |
34160 KB |
Output is correct |
23 |
Correct |
27 ms |
24056 KB |
Output is correct |
24 |
Correct |
235 ms |
32604 KB |
Output is correct |
25 |
Correct |
355 ms |
50040 KB |
Output is correct |
26 |
Correct |
28 ms |
24568 KB |
Output is correct |
27 |
Correct |
29 ms |
24568 KB |
Output is correct |
28 |
Correct |
27 ms |
24184 KB |
Output is correct |
29 |
Correct |
25 ms |
24056 KB |
Output is correct |
30 |
Correct |
26 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
27 ms |
23928 KB |
Output is correct |
14 |
Correct |
23 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23804 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
29 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23932 KB |
Output is correct |
20 |
Correct |
24 ms |
23932 KB |
Output is correct |
21 |
Correct |
23 ms |
23932 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
24 ms |
23928 KB |
Output is correct |
25 |
Correct |
24 ms |
23928 KB |
Output is correct |
26 |
Correct |
28 ms |
24440 KB |
Output is correct |
27 |
Correct |
26 ms |
24056 KB |
Output is correct |
28 |
Correct |
27 ms |
24440 KB |
Output is correct |
29 |
Correct |
30 ms |
24568 KB |
Output is correct |
30 |
Correct |
27 ms |
24056 KB |
Output is correct |
31 |
Correct |
24 ms |
23948 KB |
Output is correct |
32 |
Correct |
31 ms |
24312 KB |
Output is correct |
33 |
Correct |
23 ms |
23928 KB |
Output is correct |
34 |
Correct |
25 ms |
24056 KB |
Output is correct |
35 |
Correct |
29 ms |
24568 KB |
Output is correct |
36 |
Correct |
26 ms |
24056 KB |
Output is correct |
37 |
Correct |
27 ms |
24184 KB |
Output is correct |
38 |
Correct |
24 ms |
23928 KB |
Output is correct |
39 |
Correct |
27 ms |
24184 KB |
Output is correct |
40 |
Correct |
27 ms |
24056 KB |
Output is correct |
41 |
Correct |
26 ms |
24056 KB |
Output is correct |
42 |
Correct |
30 ms |
24104 KB |
Output is correct |
43 |
Correct |
31 ms |
24184 KB |
Output is correct |
44 |
Correct |
25 ms |
23800 KB |
Output is correct |
45 |
Correct |
26 ms |
24184 KB |
Output is correct |
46 |
Correct |
26 ms |
24188 KB |
Output is correct |
47 |
Correct |
24 ms |
23928 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
27 ms |
24568 KB |
Output is correct |
50 |
Correct |
29 ms |
24568 KB |
Output is correct |
51 |
Correct |
32 ms |
24184 KB |
Output is correct |
52 |
Correct |
30 ms |
24056 KB |
Output is correct |
53 |
Correct |
31 ms |
24184 KB |
Output is correct |
54 |
Correct |
26 ms |
24056 KB |
Output is correct |
55 |
Correct |
24 ms |
23928 KB |
Output is correct |
56 |
Correct |
99 ms |
30416 KB |
Output is correct |
57 |
Correct |
189 ms |
30432 KB |
Output is correct |
58 |
Correct |
26 ms |
24056 KB |
Output is correct |
59 |
Correct |
24 ms |
23928 KB |
Output is correct |
60 |
Correct |
24 ms |
23928 KB |
Output is correct |
61 |
Correct |
185 ms |
30724 KB |
Output is correct |
62 |
Correct |
31 ms |
24056 KB |
Output is correct |
63 |
Correct |
179 ms |
37852 KB |
Output is correct |
64 |
Correct |
190 ms |
30456 KB |
Output is correct |
65 |
Correct |
26 ms |
24056 KB |
Output is correct |
66 |
Correct |
184 ms |
31352 KB |
Output is correct |
67 |
Correct |
32 ms |
24056 KB |
Output is correct |
68 |
Correct |
28 ms |
24056 KB |
Output is correct |
69 |
Correct |
99 ms |
30320 KB |
Output is correct |
70 |
Correct |
26 ms |
24312 KB |
Output is correct |
71 |
Correct |
192 ms |
30428 KB |
Output is correct |
72 |
Correct |
164 ms |
32864 KB |
Output is correct |
73 |
Correct |
23 ms |
23800 KB |
Output is correct |
74 |
Correct |
177 ms |
38008 KB |
Output is correct |
75 |
Correct |
157 ms |
34172 KB |
Output is correct |
76 |
Correct |
26 ms |
24056 KB |
Output is correct |
77 |
Correct |
26 ms |
24060 KB |
Output is correct |
78 |
Correct |
100 ms |
28912 KB |
Output is correct |
79 |
Correct |
345 ms |
45800 KB |
Output is correct |
80 |
Correct |
29 ms |
24312 KB |
Output is correct |
81 |
Correct |
32 ms |
24056 KB |
Output is correct |
82 |
Correct |
24 ms |
23800 KB |
Output is correct |
83 |
Correct |
24 ms |
23800 KB |
Output is correct |
84 |
Correct |
25 ms |
24056 KB |
Output is correct |
85 |
Correct |
233 ms |
33144 KB |
Output is correct |
86 |
Correct |
26 ms |
24184 KB |
Output is correct |
87 |
Correct |
180 ms |
31068 KB |
Output is correct |
88 |
Correct |
24 ms |
23800 KB |
Output is correct |
89 |
Correct |
240 ms |
31068 KB |
Output is correct |
90 |
Correct |
237 ms |
34680 KB |
Output is correct |
91 |
Correct |
381 ms |
47352 KB |
Output is correct |
92 |
Correct |
99 ms |
30320 KB |
Output is correct |
93 |
Correct |
26 ms |
24228 KB |
Output is correct |
94 |
Correct |
29 ms |
23928 KB |
Output is correct |
95 |
Correct |
354 ms |
44388 KB |
Output is correct |
96 |
Correct |
439 ms |
51192 KB |
Output is correct |
97 |
Correct |
27 ms |
24056 KB |
Output is correct |
98 |
Correct |
24 ms |
23928 KB |
Output is correct |
99 |
Correct |
142 ms |
34160 KB |
Output is correct |
100 |
Correct |
27 ms |
24056 KB |
Output is correct |
101 |
Correct |
235 ms |
32604 KB |
Output is correct |
102 |
Correct |
355 ms |
50040 KB |
Output is correct |
103 |
Correct |
28 ms |
24568 KB |
Output is correct |
104 |
Correct |
29 ms |
24568 KB |
Output is correct |
105 |
Correct |
27 ms |
24184 KB |
Output is correct |
106 |
Correct |
25 ms |
24056 KB |
Output is correct |
107 |
Correct |
26 ms |
24184 KB |
Output is correct |
108 |
Correct |
725 ms |
69152 KB |
Output is correct |
109 |
Correct |
1261 ms |
75556 KB |
Output is correct |
110 |
Correct |
1140 ms |
86848 KB |
Output is correct |
111 |
Correct |
2687 ms |
158972 KB |
Output is correct |
112 |
Correct |
1931 ms |
119944 KB |
Output is correct |
113 |
Correct |
1677 ms |
127952 KB |
Output is correct |
114 |
Correct |
1484 ms |
57448 KB |
Output is correct |
115 |
Correct |
1470 ms |
57476 KB |
Output is correct |
116 |
Correct |
1433 ms |
61568 KB |
Output is correct |
117 |
Correct |
2797 ms |
142484 KB |
Output is correct |
118 |
Correct |
1287 ms |
59260 KB |
Output is correct |
119 |
Correct |
2729 ms |
142580 KB |
Output is correct |
120 |
Correct |
2525 ms |
155924 KB |
Output is correct |
121 |
Correct |
2735 ms |
142204 KB |
Output is correct |
122 |
Correct |
1684 ms |
76716 KB |
Output is correct |
123 |
Correct |
2122 ms |
143512 KB |
Output is correct |
124 |
Correct |
2588 ms |
112932 KB |
Output is correct |