#include <bits/stdc++.h>
using namespace std;
int n,k,t,cnt=1,deg[500001],color[500001],ent[500001],out[500001],mn[500001],mx[500001],par[500001];
vector<int> segmn,segmx,graph[500001];
set<pair<int,int>> st;
void dfs(int curr , int p){
ent[curr] = cnt++;
for(auto i : graph[curr]){
if(i == p){
continue;
}
par[i] = curr;
dfs(i,curr);
}
out[curr] = cnt-1;
}
void dfs2(int curr , int p , int rt){
// cout << curr << " " << p << " " << rt << endl;
for(auto i : graph[curr]){
if(i == p){
continue;
}
if(st.count({curr,i})){
deg[rt] += 1 , deg[cnt+1] += 1;
dfs2(i,curr,++cnt);
}else{
dfs2(i,curr,rt);
}
}
}
int getmax(int a , int b , int curr , int l , int r){
if(b < l || a > r){
return 0;
}
if(a <= l && b >= r){
return segmx[curr];
}
int mid = (l+r)/2;
return max(getmax(a,b,2*curr,l,mid),getmax(a,b,2*curr+1,mid+1,r));
}
int getmin(int a , int b , int curr , int l , int r){
if(b < l || a > r){
return 1000000;
}
if(a <= l && b >= r){
return segmn[curr];
}
int mid = (l+r)/2;
return min(getmin(a,b,2*curr,l,mid),getmin(a,b,2*curr+1,mid+1,r));
}
void upd(int curr , int v1 , int v2){
while(curr){
segmn[curr] = min(segmn[curr],v1);
segmx[curr] = max(segmx[curr],v2);
curr /= 2;
}
}
int main(){
cin >> n >> k;
t = pow(2,ceil(log2(n)));
segmn.resize(2*t,1000000),segmx.resize(2*t,0);
for(int i = 0 ; i < n-1 ; i += 1){
int a,b;
cin >> a >> b;
graph[a].push_back(b),graph[b].push_back(a);
}
for(int i = 1 ; i <= k ; i += 1){
mn[i] = 1000000 , mx[i] = 0;
}
for(int i = 1 ; i <= n ; i += 1){
cin >> color[i];
}
dfs(1,1);
for(int i = 1 ; i <= n ; i += 1){
mn[color[i]] = min(mn[color[i]],ent[i]);
mx[color[i]] = max(mx[color[i]],ent[i]);
}
for(int i = 1 ; i <= n ; i += 1){
upd(ent[i]+t-1,mn[color[i]],mx[color[i]]);
}
for(int i = 2 ; i <= n ; i += 1){
if(getmin(ent[i],out[i],1,1,t) >= ent[i] && getmax(ent[i],out[i],1,1,t) <= out[i]){
st.insert({par[i],i});
}
}
cnt = 1;
dfs2(1,1,1);
int num = 0;
for(int i = 1 ; i <= cnt ; i += 1){
if(deg[i] == 1){
num += 1;
}
}
cout << (num+1)/2 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12108 KB |
Output is correct |
4 |
Correct |
8 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12040 KB |
Output is correct |
6 |
Correct |
9 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12048 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
11980 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
9 ms |
12016 KB |
Output is correct |
12 |
Correct |
7 ms |
12044 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12044 KB |
Output is correct |
16 |
Correct |
8 ms |
12080 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12040 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
9 ms |
11996 KB |
Output is correct |
21 |
Correct |
7 ms |
12036 KB |
Output is correct |
22 |
Correct |
9 ms |
12048 KB |
Output is correct |
23 |
Correct |
8 ms |
12028 KB |
Output is correct |
24 |
Correct |
8 ms |
12044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12108 KB |
Output is correct |
4 |
Correct |
8 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12040 KB |
Output is correct |
6 |
Correct |
9 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12048 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
11980 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
9 ms |
12016 KB |
Output is correct |
12 |
Correct |
7 ms |
12044 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12044 KB |
Output is correct |
16 |
Correct |
8 ms |
12080 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12040 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
9 ms |
11996 KB |
Output is correct |
21 |
Correct |
7 ms |
12036 KB |
Output is correct |
22 |
Correct |
9 ms |
12048 KB |
Output is correct |
23 |
Correct |
8 ms |
12028 KB |
Output is correct |
24 |
Correct |
8 ms |
12044 KB |
Output is correct |
25 |
Correct |
7 ms |
12044 KB |
Output is correct |
26 |
Correct |
14 ms |
12408 KB |
Output is correct |
27 |
Correct |
11 ms |
12312 KB |
Output is correct |
28 |
Correct |
13 ms |
12468 KB |
Output is correct |
29 |
Correct |
13 ms |
12492 KB |
Output is correct |
30 |
Correct |
11 ms |
12232 KB |
Output is correct |
31 |
Correct |
8 ms |
12108 KB |
Output is correct |
32 |
Correct |
15 ms |
12444 KB |
Output is correct |
33 |
Correct |
8 ms |
11980 KB |
Output is correct |
34 |
Correct |
13 ms |
12332 KB |
Output is correct |
35 |
Correct |
13 ms |
12484 KB |
Output is correct |
36 |
Correct |
11 ms |
12248 KB |
Output is correct |
37 |
Correct |
12 ms |
12312 KB |
Output is correct |
38 |
Correct |
8 ms |
12108 KB |
Output is correct |
39 |
Correct |
12 ms |
12316 KB |
Output is correct |
40 |
Correct |
11 ms |
12272 KB |
Output is correct |
41 |
Correct |
11 ms |
12236 KB |
Output is correct |
42 |
Correct |
12 ms |
12352 KB |
Output is correct |
43 |
Correct |
12 ms |
12448 KB |
Output is correct |
44 |
Correct |
8 ms |
12040 KB |
Output is correct |
45 |
Correct |
13 ms |
12364 KB |
Output is correct |
46 |
Correct |
12 ms |
12384 KB |
Output is correct |
47 |
Correct |
9 ms |
12012 KB |
Output is correct |
48 |
Correct |
12 ms |
12360 KB |
Output is correct |
49 |
Correct |
13 ms |
12492 KB |
Output is correct |
50 |
Correct |
13 ms |
12460 KB |
Output is correct |
51 |
Correct |
12 ms |
12312 KB |
Output is correct |
52 |
Correct |
11 ms |
12236 KB |
Output is correct |
53 |
Correct |
11 ms |
12364 KB |
Output is correct |
54 |
Correct |
11 ms |
12236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12108 KB |
Output is correct |
4 |
Correct |
8 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12040 KB |
Output is correct |
6 |
Correct |
9 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12048 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
11980 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
9 ms |
12016 KB |
Output is correct |
12 |
Correct |
7 ms |
12044 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12044 KB |
Output is correct |
16 |
Correct |
8 ms |
12080 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12040 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
9 ms |
11996 KB |
Output is correct |
21 |
Correct |
7 ms |
12036 KB |
Output is correct |
22 |
Correct |
9 ms |
12048 KB |
Output is correct |
23 |
Correct |
8 ms |
12028 KB |
Output is correct |
24 |
Correct |
8 ms |
12044 KB |
Output is correct |
25 |
Correct |
9 ms |
12108 KB |
Output is correct |
26 |
Correct |
146 ms |
20720 KB |
Output is correct |
27 |
Correct |
182 ms |
20360 KB |
Output is correct |
28 |
Correct |
12 ms |
12292 KB |
Output is correct |
29 |
Correct |
8 ms |
11980 KB |
Output is correct |
30 |
Correct |
7 ms |
12108 KB |
Output is correct |
31 |
Correct |
187 ms |
20332 KB |
Output is correct |
32 |
Correct |
12 ms |
12236 KB |
Output is correct |
33 |
Correct |
222 ms |
27472 KB |
Output is correct |
34 |
Correct |
178 ms |
20264 KB |
Output is correct |
35 |
Correct |
15 ms |
12376 KB |
Output is correct |
36 |
Correct |
191 ms |
21204 KB |
Output is correct |
37 |
Correct |
12 ms |
12236 KB |
Output is correct |
38 |
Correct |
12 ms |
12236 KB |
Output is correct |
39 |
Correct |
142 ms |
20580 KB |
Output is correct |
40 |
Correct |
13 ms |
12492 KB |
Output is correct |
41 |
Correct |
182 ms |
20256 KB |
Output is correct |
42 |
Correct |
195 ms |
22704 KB |
Output is correct |
43 |
Correct |
8 ms |
11980 KB |
Output is correct |
44 |
Correct |
181 ms |
27664 KB |
Output is correct |
45 |
Correct |
189 ms |
24008 KB |
Output is correct |
46 |
Correct |
11 ms |
12236 KB |
Output is correct |
47 |
Correct |
12 ms |
12308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
20708 KB |
Output is correct |
2 |
Correct |
240 ms |
26808 KB |
Output is correct |
3 |
Correct |
13 ms |
12364 KB |
Output is correct |
4 |
Correct |
13 ms |
12316 KB |
Output is correct |
5 |
Correct |
9 ms |
12044 KB |
Output is correct |
6 |
Correct |
10 ms |
12084 KB |
Output is correct |
7 |
Correct |
12 ms |
12236 KB |
Output is correct |
8 |
Correct |
255 ms |
21596 KB |
Output is correct |
9 |
Correct |
12 ms |
12236 KB |
Output is correct |
10 |
Correct |
184 ms |
20536 KB |
Output is correct |
11 |
Correct |
9 ms |
12108 KB |
Output is correct |
12 |
Correct |
194 ms |
20616 KB |
Output is correct |
13 |
Correct |
223 ms |
21924 KB |
Output is correct |
14 |
Correct |
297 ms |
26464 KB |
Output is correct |
15 |
Correct |
163 ms |
20596 KB |
Output is correct |
16 |
Correct |
12 ms |
12364 KB |
Output is correct |
17 |
Correct |
8 ms |
12108 KB |
Output is correct |
18 |
Correct |
234 ms |
25676 KB |
Output is correct |
19 |
Correct |
321 ms |
30404 KB |
Output is correct |
20 |
Correct |
13 ms |
12272 KB |
Output is correct |
21 |
Correct |
8 ms |
11980 KB |
Output is correct |
22 |
Correct |
184 ms |
22120 KB |
Output is correct |
23 |
Correct |
12 ms |
12236 KB |
Output is correct |
24 |
Correct |
225 ms |
20972 KB |
Output is correct |
25 |
Correct |
323 ms |
29016 KB |
Output is correct |
26 |
Correct |
14 ms |
12468 KB |
Output is correct |
27 |
Correct |
15 ms |
12632 KB |
Output is correct |
28 |
Correct |
13 ms |
12324 KB |
Output is correct |
29 |
Correct |
15 ms |
12232 KB |
Output is correct |
30 |
Correct |
13 ms |
12308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12108 KB |
Output is correct |
4 |
Correct |
8 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12040 KB |
Output is correct |
6 |
Correct |
9 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12048 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
11980 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
9 ms |
12016 KB |
Output is correct |
12 |
Correct |
7 ms |
12044 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12044 KB |
Output is correct |
16 |
Correct |
8 ms |
12080 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12040 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
9 ms |
11996 KB |
Output is correct |
21 |
Correct |
7 ms |
12036 KB |
Output is correct |
22 |
Correct |
9 ms |
12048 KB |
Output is correct |
23 |
Correct |
8 ms |
12028 KB |
Output is correct |
24 |
Correct |
8 ms |
12044 KB |
Output is correct |
25 |
Correct |
7 ms |
12044 KB |
Output is correct |
26 |
Correct |
14 ms |
12408 KB |
Output is correct |
27 |
Correct |
11 ms |
12312 KB |
Output is correct |
28 |
Correct |
13 ms |
12468 KB |
Output is correct |
29 |
Correct |
13 ms |
12492 KB |
Output is correct |
30 |
Correct |
11 ms |
12232 KB |
Output is correct |
31 |
Correct |
8 ms |
12108 KB |
Output is correct |
32 |
Correct |
15 ms |
12444 KB |
Output is correct |
33 |
Correct |
8 ms |
11980 KB |
Output is correct |
34 |
Correct |
13 ms |
12332 KB |
Output is correct |
35 |
Correct |
13 ms |
12484 KB |
Output is correct |
36 |
Correct |
11 ms |
12248 KB |
Output is correct |
37 |
Correct |
12 ms |
12312 KB |
Output is correct |
38 |
Correct |
8 ms |
12108 KB |
Output is correct |
39 |
Correct |
12 ms |
12316 KB |
Output is correct |
40 |
Correct |
11 ms |
12272 KB |
Output is correct |
41 |
Correct |
11 ms |
12236 KB |
Output is correct |
42 |
Correct |
12 ms |
12352 KB |
Output is correct |
43 |
Correct |
12 ms |
12448 KB |
Output is correct |
44 |
Correct |
8 ms |
12040 KB |
Output is correct |
45 |
Correct |
13 ms |
12364 KB |
Output is correct |
46 |
Correct |
12 ms |
12384 KB |
Output is correct |
47 |
Correct |
9 ms |
12012 KB |
Output is correct |
48 |
Correct |
12 ms |
12360 KB |
Output is correct |
49 |
Correct |
13 ms |
12492 KB |
Output is correct |
50 |
Correct |
13 ms |
12460 KB |
Output is correct |
51 |
Correct |
12 ms |
12312 KB |
Output is correct |
52 |
Correct |
11 ms |
12236 KB |
Output is correct |
53 |
Correct |
11 ms |
12364 KB |
Output is correct |
54 |
Correct |
11 ms |
12236 KB |
Output is correct |
55 |
Correct |
9 ms |
12108 KB |
Output is correct |
56 |
Correct |
146 ms |
20720 KB |
Output is correct |
57 |
Correct |
182 ms |
20360 KB |
Output is correct |
58 |
Correct |
12 ms |
12292 KB |
Output is correct |
59 |
Correct |
8 ms |
11980 KB |
Output is correct |
60 |
Correct |
7 ms |
12108 KB |
Output is correct |
61 |
Correct |
187 ms |
20332 KB |
Output is correct |
62 |
Correct |
12 ms |
12236 KB |
Output is correct |
63 |
Correct |
222 ms |
27472 KB |
Output is correct |
64 |
Correct |
178 ms |
20264 KB |
Output is correct |
65 |
Correct |
15 ms |
12376 KB |
Output is correct |
66 |
Correct |
191 ms |
21204 KB |
Output is correct |
67 |
Correct |
12 ms |
12236 KB |
Output is correct |
68 |
Correct |
12 ms |
12236 KB |
Output is correct |
69 |
Correct |
142 ms |
20580 KB |
Output is correct |
70 |
Correct |
13 ms |
12492 KB |
Output is correct |
71 |
Correct |
182 ms |
20256 KB |
Output is correct |
72 |
Correct |
195 ms |
22704 KB |
Output is correct |
73 |
Correct |
8 ms |
11980 KB |
Output is correct |
74 |
Correct |
181 ms |
27664 KB |
Output is correct |
75 |
Correct |
189 ms |
24008 KB |
Output is correct |
76 |
Correct |
11 ms |
12236 KB |
Output is correct |
77 |
Correct |
12 ms |
12308 KB |
Output is correct |
78 |
Correct |
156 ms |
20708 KB |
Output is correct |
79 |
Correct |
240 ms |
26808 KB |
Output is correct |
80 |
Correct |
13 ms |
12364 KB |
Output is correct |
81 |
Correct |
13 ms |
12316 KB |
Output is correct |
82 |
Correct |
9 ms |
12044 KB |
Output is correct |
83 |
Correct |
10 ms |
12084 KB |
Output is correct |
84 |
Correct |
12 ms |
12236 KB |
Output is correct |
85 |
Correct |
255 ms |
21596 KB |
Output is correct |
86 |
Correct |
12 ms |
12236 KB |
Output is correct |
87 |
Correct |
184 ms |
20536 KB |
Output is correct |
88 |
Correct |
9 ms |
12108 KB |
Output is correct |
89 |
Correct |
194 ms |
20616 KB |
Output is correct |
90 |
Correct |
223 ms |
21924 KB |
Output is correct |
91 |
Correct |
297 ms |
26464 KB |
Output is correct |
92 |
Correct |
163 ms |
20596 KB |
Output is correct |
93 |
Correct |
12 ms |
12364 KB |
Output is correct |
94 |
Correct |
8 ms |
12108 KB |
Output is correct |
95 |
Correct |
234 ms |
25676 KB |
Output is correct |
96 |
Correct |
321 ms |
30404 KB |
Output is correct |
97 |
Correct |
13 ms |
12272 KB |
Output is correct |
98 |
Correct |
8 ms |
11980 KB |
Output is correct |
99 |
Correct |
184 ms |
22120 KB |
Output is correct |
100 |
Correct |
12 ms |
12236 KB |
Output is correct |
101 |
Correct |
225 ms |
20972 KB |
Output is correct |
102 |
Correct |
323 ms |
29016 KB |
Output is correct |
103 |
Correct |
14 ms |
12468 KB |
Output is correct |
104 |
Correct |
15 ms |
12632 KB |
Output is correct |
105 |
Correct |
13 ms |
12324 KB |
Output is correct |
106 |
Correct |
15 ms |
12232 KB |
Output is correct |
107 |
Correct |
13 ms |
12308 KB |
Output is correct |
108 |
Correct |
1201 ms |
59324 KB |
Output is correct |
109 |
Correct |
1217 ms |
69828 KB |
Output is correct |
110 |
Correct |
1225 ms |
81040 KB |
Output is correct |
111 |
Correct |
2399 ms |
104576 KB |
Output is correct |
112 |
Correct |
1881 ms |
84900 KB |
Output is correct |
113 |
Correct |
1498 ms |
79740 KB |
Output is correct |
114 |
Correct |
1097 ms |
51804 KB |
Output is correct |
115 |
Correct |
1056 ms |
51800 KB |
Output is correct |
116 |
Correct |
1236 ms |
54548 KB |
Output is correct |
117 |
Correct |
2173 ms |
83472 KB |
Output is correct |
118 |
Correct |
1098 ms |
52220 KB |
Output is correct |
119 |
Correct |
2181 ms |
83588 KB |
Output is correct |
120 |
Correct |
2284 ms |
97492 KB |
Output is correct |
121 |
Correct |
2149 ms |
83424 KB |
Output is correct |
122 |
Correct |
1463 ms |
60916 KB |
Output is correct |
123 |
Correct |
1830 ms |
85068 KB |
Output is correct |
124 |
Correct |
2194 ms |
73520 KB |
Output is correct |