#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define MAXN 200007
int n , k ;
int a[ MAXN ] ;
vector < int > v[ MAXN ] ;
vector < int > col[ MAXN ] ;
int tot ;
int used[ MAXN ] , subtree[ MAXN ] , prv[ MAXN ] , mrk[ MAXN ] ;
bool added[ MAXN ] ;
vector < int > clist ;
int ans = MAXN ;
void dfs ( int vertex , int lst , int ori ) {
mrk[ vertex ] = ori ;
++ tot ;
subtree[ vertex ] = 1 ;
for ( auto x : v[ vertex ] ) {
if ( x == lst || used[ x ] == 1 ) { continue ; }
prv[ x ] = vertex ;
dfs ( x , vertex , ori ) ;
subtree[ vertex ] += subtree[ x ] ;
}
}
int get_centroid ( int vertex , int lst ) {
for ( auto x : v[ vertex ] ) {
if ( x == lst || used[ x ] == 1 ) { continue ; }
if ( 2 * subtree[ x ] > tot ) {
return get_centroid ( x , vertex ) ;
}
}
return vertex ;
}
queue < int > q ;
void decompose ( int vertex ) {
tot = 0 , prv[ vertex ] = 0 ;
dfs ( vertex , -1 , vertex ) ;
vertex = get_centroid ( vertex , -1 ) ;
tot = 0 , prv[ vertex ] = 0 ;
dfs ( vertex , -1 , vertex ) ;
bool ok = true ;
for ( auto x : col[ a[ vertex ] ] ) {
if ( mrk[ x ] != vertex ) {
ok = false ;
break ;
}
q.push ( x ) ;
}
clist.push_back ( a[ vertex ] ) ;
added[ a[ vertex ] ] = true ;
while ( q.empty ( ) == false ) {
if ( ok == false ) { break ; }
int x = q.front ( ) ;
q.pop ( ) ;
if ( prv[ x ] >= 1 ) {
if ( added[ a[ prv[ x ] ] ] == false ) {
for ( auto x : col[ a[ prv[ x ] ] ] ) {
if ( mrk[ x ] != vertex ) {
ok = false ;
break ;
}
q.push ( x ) ;
}
added[ a[ prv[ x ] ] ] = true ;
clist.push_back ( a[ prv[ x ] ] ) ;
}
}
}
if ( ok == true ) {
ans = min ( ans , (int)clist.size ( ) - 1 ) ;
}
for ( auto x : clist ) {
added[ x ] = false ;
}
clist.clear ( ) ;
while ( q.empty ( ) == false ) { q.pop ( ) ; }
used[ vertex ] = 1 ;
for ( auto x : v[ vertex ] ) {
if ( used[ x ] == 0 ) {
decompose ( x ) ;
}
}
}
void input ( ) {
cin >> n >> k ;
for ( int i = 1 ; i < n ; ++ i ) {
int x , y ; cin >> x >> y ;
v[ x ].push_back ( y ) ;
v[ y ].push_back ( x ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
col[ a[ i ] ].push_back ( i ) ;
}
}
void solve ( ) {
decompose ( 1 ) ;
cout << ans << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ;
// cin >> t ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9680 KB |
Output is correct |
9 |
Correct |
6 ms |
9740 KB |
Output is correct |
10 |
Correct |
6 ms |
9744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9680 KB |
Output is correct |
9 |
Correct |
6 ms |
9740 KB |
Output is correct |
10 |
Correct |
6 ms |
9744 KB |
Output is correct |
11 |
Correct |
7 ms |
9776 KB |
Output is correct |
12 |
Correct |
10 ms |
9956 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
7 ms |
9820 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
7 ms |
9940 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9900 KB |
Output is correct |
22 |
Correct |
7 ms |
10068 KB |
Output is correct |
23 |
Correct |
8 ms |
9872 KB |
Output is correct |
24 |
Correct |
7 ms |
9856 KB |
Output is correct |
25 |
Correct |
8 ms |
10000 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9876 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
624 ms |
38188 KB |
Output is correct |
2 |
Correct |
163 ms |
38744 KB |
Output is correct |
3 |
Correct |
588 ms |
37852 KB |
Output is correct |
4 |
Correct |
172 ms |
38672 KB |
Output is correct |
5 |
Correct |
591 ms |
35016 KB |
Output is correct |
6 |
Correct |
176 ms |
38780 KB |
Output is correct |
7 |
Correct |
667 ms |
35508 KB |
Output is correct |
8 |
Correct |
174 ms |
38516 KB |
Output is correct |
9 |
Correct |
656 ms |
33804 KB |
Output is correct |
10 |
Correct |
651 ms |
31444 KB |
Output is correct |
11 |
Correct |
708 ms |
34268 KB |
Output is correct |
12 |
Correct |
689 ms |
36900 KB |
Output is correct |
13 |
Correct |
649 ms |
30964 KB |
Output is correct |
14 |
Correct |
674 ms |
40884 KB |
Output is correct |
15 |
Correct |
696 ms |
40616 KB |
Output is correct |
16 |
Correct |
688 ms |
35552 KB |
Output is correct |
17 |
Correct |
652 ms |
36072 KB |
Output is correct |
18 |
Correct |
700 ms |
36224 KB |
Output is correct |
19 |
Correct |
704 ms |
39404 KB |
Output is correct |
20 |
Correct |
655 ms |
41820 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9680 KB |
Output is correct |
9 |
Correct |
6 ms |
9740 KB |
Output is correct |
10 |
Correct |
6 ms |
9744 KB |
Output is correct |
11 |
Correct |
7 ms |
9776 KB |
Output is correct |
12 |
Correct |
10 ms |
9956 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
7 ms |
9820 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
7 ms |
9940 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9900 KB |
Output is correct |
22 |
Correct |
7 ms |
10068 KB |
Output is correct |
23 |
Correct |
8 ms |
9872 KB |
Output is correct |
24 |
Correct |
7 ms |
9856 KB |
Output is correct |
25 |
Correct |
8 ms |
10000 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9876 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
624 ms |
38188 KB |
Output is correct |
32 |
Correct |
163 ms |
38744 KB |
Output is correct |
33 |
Correct |
588 ms |
37852 KB |
Output is correct |
34 |
Correct |
172 ms |
38672 KB |
Output is correct |
35 |
Correct |
591 ms |
35016 KB |
Output is correct |
36 |
Correct |
176 ms |
38780 KB |
Output is correct |
37 |
Correct |
667 ms |
35508 KB |
Output is correct |
38 |
Correct |
174 ms |
38516 KB |
Output is correct |
39 |
Correct |
656 ms |
33804 KB |
Output is correct |
40 |
Correct |
651 ms |
31444 KB |
Output is correct |
41 |
Correct |
708 ms |
34268 KB |
Output is correct |
42 |
Correct |
689 ms |
36900 KB |
Output is correct |
43 |
Correct |
649 ms |
30964 KB |
Output is correct |
44 |
Correct |
674 ms |
40884 KB |
Output is correct |
45 |
Correct |
696 ms |
40616 KB |
Output is correct |
46 |
Correct |
688 ms |
35552 KB |
Output is correct |
47 |
Correct |
652 ms |
36072 KB |
Output is correct |
48 |
Correct |
700 ms |
36224 KB |
Output is correct |
49 |
Correct |
704 ms |
39404 KB |
Output is correct |
50 |
Correct |
655 ms |
41820 KB |
Output is correct |
51 |
Correct |
7 ms |
9728 KB |
Output is correct |
52 |
Correct |
415 ms |
25852 KB |
Output is correct |
53 |
Correct |
501 ms |
25844 KB |
Output is correct |
54 |
Correct |
458 ms |
25804 KB |
Output is correct |
55 |
Correct |
553 ms |
25756 KB |
Output is correct |
56 |
Correct |
534 ms |
25852 KB |
Output is correct |
57 |
Correct |
461 ms |
25876 KB |
Output is correct |
58 |
Correct |
494 ms |
29020 KB |
Output is correct |
59 |
Correct |
453 ms |
29264 KB |
Output is correct |
60 |
Correct |
521 ms |
28636 KB |
Output is correct |
61 |
Correct |
572 ms |
28224 KB |
Output is correct |
62 |
Correct |
187 ms |
42368 KB |
Output is correct |
63 |
Correct |
186 ms |
42344 KB |
Output is correct |
64 |
Correct |
169 ms |
42280 KB |
Output is correct |
65 |
Correct |
182 ms |
42308 KB |
Output is correct |
66 |
Correct |
368 ms |
34268 KB |
Output is correct |
67 |
Correct |
404 ms |
34136 KB |
Output is correct |
68 |
Correct |
377 ms |
34132 KB |
Output is correct |
69 |
Correct |
350 ms |
34276 KB |
Output is correct |
70 |
Correct |
375 ms |
34072 KB |
Output is correct |
71 |
Correct |
371 ms |
34172 KB |
Output is correct |
72 |
Correct |
376 ms |
34072 KB |
Output is correct |
73 |
Correct |
358 ms |
33372 KB |
Output is correct |
74 |
Correct |
362 ms |
34272 KB |
Output is correct |
75 |
Correct |
358 ms |
34228 KB |
Output is correct |
76 |
Correct |
567 ms |
32604 KB |
Output is correct |
77 |
Correct |
553 ms |
30840 KB |
Output is correct |
78 |
Correct |
714 ms |
35996 KB |
Output is correct |
79 |
Correct |
690 ms |
34040 KB |
Output is correct |
80 |
Correct |
692 ms |
41264 KB |
Output is correct |
81 |
Correct |
686 ms |
37632 KB |
Output is correct |
82 |
Correct |
691 ms |
37736 KB |
Output is correct |
83 |
Correct |
674 ms |
34300 KB |
Output is correct |
84 |
Correct |
620 ms |
40272 KB |
Output is correct |
85 |
Correct |
636 ms |
38620 KB |
Output is correct |
86 |
Correct |
672 ms |
34064 KB |
Output is correct |
87 |
Correct |
674 ms |
35808 KB |
Output is correct |
88 |
Correct |
595 ms |
36764 KB |
Output is correct |
89 |
Correct |
521 ms |
32808 KB |
Output is correct |
90 |
Correct |
563 ms |
32744 KB |
Output is correct |
91 |
Correct |
526 ms |
35072 KB |
Output is correct |
92 |
Correct |
535 ms |
33532 KB |
Output is correct |
93 |
Correct |
575 ms |
33392 KB |
Output is correct |
94 |
Correct |
525 ms |
32848 KB |
Output is correct |
95 |
Correct |
538 ms |
34312 KB |
Output is correct |
96 |
Correct |
561 ms |
32832 KB |
Output is correct |
97 |
Correct |
566 ms |
34928 KB |
Output is correct |