#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int N, M, ans=0, t=0;
cin>>N>>M;
if(N<3) { cout<<M-1<<'\n'; return 0; }
vector<int> C(N), L(M), R(M);
vector<vector<int>> G(N);
for(int i=1; i<N; i++) {
int x, y;
cin>>x>>y; --x, --y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=0; i<N; i++) cin>>C[i], --C[i];
function<void(int, int)> f=[&](int i, int p) {
++t;
L[C[i]]=L[C[i]]?:t;
for(int j:G[i]) if(j!=p) f(j, i);
R[C[i]]=t;
};
f(0, 0);
t=0;
function<tuple<int, int, int>(int, int)> g=[&](int i, int p) {
++t;
int a=L[C[i]], b=R[C[i]], x=0, s=t;
for(int j:G[i]) if(j!=p) {
auto[c,d,y]=g(j, i);
a=min(a, c);
b=max(b, d);
x+=y;
}
if(s==a && b==t && i) ans+=!x, x=1;
return make_tuple(a, b, x);
};
int r=get<2>(g(0, 0))==1;
cout<<(ans+r+1)/2<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
3 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
768 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
876 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
2 ms |
620 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
23 ms |
364 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
620 KB |
Output is correct |
43 |
Correct |
4 ms |
876 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
620 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
2 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
620 KB |
Output is correct |
53 |
Correct |
2 ms |
620 KB |
Output is correct |
54 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
50 ms |
7404 KB |
Output is correct |
27 |
Correct |
73 ms |
7148 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
81 ms |
7148 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
115 ms |
18924 KB |
Output is correct |
34 |
Correct |
75 ms |
7404 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
80 ms |
8544 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
2 ms |
492 KB |
Output is correct |
39 |
Correct |
57 ms |
7908 KB |
Output is correct |
40 |
Correct |
2 ms |
876 KB |
Output is correct |
41 |
Correct |
107 ms |
7532 KB |
Output is correct |
42 |
Correct |
104 ms |
11372 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
98 ms |
19564 KB |
Output is correct |
45 |
Correct |
83 ms |
13548 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
7012 KB |
Output is correct |
2 |
Correct |
61 ms |
7908 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
81 ms |
7916 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
98 ms |
7276 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
107 ms |
7148 KB |
Output is correct |
13 |
Correct |
84 ms |
7532 KB |
Output is correct |
14 |
Correct |
87 ms |
8172 KB |
Output is correct |
15 |
Correct |
50 ms |
7524 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
59 ms |
8144 KB |
Output is correct |
19 |
Correct |
99 ms |
14316 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
54 ms |
7908 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
92 ms |
7276 KB |
Output is correct |
25 |
Correct |
96 ms |
12140 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
3 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
768 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
876 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
2 ms |
620 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
23 ms |
364 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
620 KB |
Output is correct |
43 |
Correct |
4 ms |
876 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
620 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
2 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
620 KB |
Output is correct |
53 |
Correct |
2 ms |
620 KB |
Output is correct |
54 |
Correct |
2 ms |
492 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
50 ms |
7404 KB |
Output is correct |
57 |
Correct |
73 ms |
7148 KB |
Output is correct |
58 |
Correct |
2 ms |
620 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
81 ms |
7148 KB |
Output is correct |
62 |
Correct |
2 ms |
620 KB |
Output is correct |
63 |
Correct |
115 ms |
18924 KB |
Output is correct |
64 |
Correct |
75 ms |
7404 KB |
Output is correct |
65 |
Correct |
2 ms |
492 KB |
Output is correct |
66 |
Correct |
80 ms |
8544 KB |
Output is correct |
67 |
Correct |
2 ms |
492 KB |
Output is correct |
68 |
Correct |
2 ms |
492 KB |
Output is correct |
69 |
Correct |
57 ms |
7908 KB |
Output is correct |
70 |
Correct |
2 ms |
876 KB |
Output is correct |
71 |
Correct |
107 ms |
7532 KB |
Output is correct |
72 |
Correct |
104 ms |
11372 KB |
Output is correct |
73 |
Correct |
1 ms |
364 KB |
Output is correct |
74 |
Correct |
98 ms |
19564 KB |
Output is correct |
75 |
Correct |
83 ms |
13548 KB |
Output is correct |
76 |
Correct |
2 ms |
620 KB |
Output is correct |
77 |
Correct |
2 ms |
492 KB |
Output is correct |
78 |
Correct |
49 ms |
7012 KB |
Output is correct |
79 |
Correct |
61 ms |
7908 KB |
Output is correct |
80 |
Correct |
2 ms |
620 KB |
Output is correct |
81 |
Correct |
2 ms |
620 KB |
Output is correct |
82 |
Correct |
1 ms |
364 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
2 ms |
620 KB |
Output is correct |
85 |
Correct |
81 ms |
7916 KB |
Output is correct |
86 |
Correct |
2 ms |
492 KB |
Output is correct |
87 |
Correct |
98 ms |
7276 KB |
Output is correct |
88 |
Correct |
1 ms |
364 KB |
Output is correct |
89 |
Correct |
107 ms |
7148 KB |
Output is correct |
90 |
Correct |
84 ms |
7532 KB |
Output is correct |
91 |
Correct |
87 ms |
8172 KB |
Output is correct |
92 |
Correct |
50 ms |
7524 KB |
Output is correct |
93 |
Correct |
2 ms |
620 KB |
Output is correct |
94 |
Correct |
1 ms |
364 KB |
Output is correct |
95 |
Correct |
59 ms |
8144 KB |
Output is correct |
96 |
Correct |
99 ms |
14316 KB |
Output is correct |
97 |
Correct |
2 ms |
620 KB |
Output is correct |
98 |
Correct |
1 ms |
364 KB |
Output is correct |
99 |
Correct |
54 ms |
7908 KB |
Output is correct |
100 |
Correct |
2 ms |
620 KB |
Output is correct |
101 |
Correct |
92 ms |
7276 KB |
Output is correct |
102 |
Correct |
96 ms |
12140 KB |
Output is correct |
103 |
Correct |
3 ms |
620 KB |
Output is correct |
104 |
Correct |
2 ms |
620 KB |
Output is correct |
105 |
Correct |
2 ms |
620 KB |
Output is correct |
106 |
Correct |
2 ms |
620 KB |
Output is correct |
107 |
Correct |
2 ms |
620 KB |
Output is correct |
108 |
Correct |
419 ms |
34780 KB |
Output is correct |
109 |
Correct |
755 ms |
60908 KB |
Output is correct |
110 |
Correct |
766 ms |
78956 KB |
Output is correct |
111 |
Correct |
799 ms |
72940 KB |
Output is correct |
112 |
Correct |
733 ms |
57324 KB |
Output is correct |
113 |
Correct |
453 ms |
36564 KB |
Output is correct |
114 |
Correct |
650 ms |
31724 KB |
Output is correct |
115 |
Correct |
629 ms |
31852 KB |
Output is correct |
116 |
Correct |
673 ms |
32784 KB |
Output is correct |
117 |
Correct |
685 ms |
35820 KB |
Output is correct |
118 |
Correct |
634 ms |
31340 KB |
Output is correct |
119 |
Correct |
716 ms |
35820 KB |
Output is correct |
120 |
Correct |
773 ms |
58604 KB |
Output is correct |
121 |
Correct |
696 ms |
35820 KB |
Output is correct |
122 |
Correct |
683 ms |
34284 KB |
Output is correct |
123 |
Correct |
449 ms |
37128 KB |
Output is correct |
124 |
Correct |
724 ms |
35192 KB |
Output is correct |