# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
545053 |
2022-04-03T13:05:06 Z |
chonka |
Mergers (JOI19_mergers) |
C++ |
|
157 ms |
46012 KB |
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define MAXN 500007
#define LOG 20
int n , k ;
vector < int > v[ MAXN ] ;
int a[ MAXN ] ;
int lvl[ MAXN ] ;
int LCA[ MAXN ][ LOG ] ;
int st[ MAXN ] , tp ;
vector < int > col[ MAXN ] ;
void init ( int vertex , int prv ) {
st[ vertex ] = ++ tp ;
for ( int i = 1 ; i < LOG ; ++ i ) {
LCA[ vertex ][ i ] = LCA[ LCA[ vertex ][ i - 1 ] ][ i - 1 ] ;
}
for ( auto x : v[ vertex ] ) {
if ( x == prv ) { continue ; }
lvl[ x ] = lvl[ vertex ] + 1 ;
LCA[ x ][ 0 ] = vertex ;
init ( x , vertex ) ;
}
}
int ori[ MAXN ] ;
int get_lca ( int x , int y ) {
for ( int i = LOG - 1 ; i >= 0 ; -- i ) {
if ( lvl[ x ] - ( 1 << i ) >= lvl[ y ] ) {
x = LCA[ x ][ i ] ;
}
if ( lvl[ y ] - ( 1 << i ) >= lvl[ x ] ) {
y = LCA[ y ][ i ] ;
}
}
for ( int i = LOG - 1 ; i >= 0 ; -- i ) {
if ( LCA[ x ][ i ] != LCA[ y ][ i ] ) {
x = LCA[ x ][ i ] ;
y = LCA[ y ][ i ] ;
}
}
if ( x != y ) { x = LCA[ x ][ 0 ] ; }
return x ;
}
int dp[ MAXN ] ;
int bad[ MAXN ] ;
int ans = 0 ;
int dfs ( int vertex , int prv ) {
int ret = lvl[ ori[ a[ vertex ] ] ] ;
for ( auto x : v[ vertex ] ) {
if ( x == prv ) { continue ; }
int aux = dfs ( x , vertex ) ;
ret = min ( ret , aux ) ;
dp[ vertex ] += max ( bad[ x ] , dp[ x ] ) ;
}
if ( ret >= lvl[ vertex ] ) {
bad[ vertex ] = 1 ;
if ( dp[ vertex ] == 0 && vertex != 1 ) {
++ ans ;
}
}
return ret ;
}
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 ( ) {
init ( 1 , -1 ) ;
auto cmp = [ & ] ( int x , int y ) {
return ( st[ x ] < st[ y ] ) ;
};
for ( int i = 1 ; i <= k ; ++ i ) {
sort ( col[ i ].begin ( ) , col[ i ].end ( ) , cmp ) ;
ori[ i ] = get_lca ( col[ i ].front ( ) , col[ i ].back ( ) ) ;
}
dfs ( 1 , -1 ) ;
for ( int i = 2 ; i <= n ; ++ i ) {
if ( dp[ i ] == dp[ 1 ] ) {
ans += bad[ i ] ;
}
}
cout << ( ans + 1 ) / 2 << "\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 |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23856 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23728 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23756 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23844 KB |
Output is correct |
14 |
Correct |
14 ms |
23776 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23820 KB |
Output is correct |
17 |
Correct |
12 ms |
23824 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23760 KB |
Output is correct |
20 |
Correct |
11 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23856 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23728 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23756 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23844 KB |
Output is correct |
14 |
Correct |
14 ms |
23776 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23820 KB |
Output is correct |
17 |
Correct |
12 ms |
23824 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23760 KB |
Output is correct |
20 |
Correct |
11 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24264 KB |
Output is correct |
27 |
Correct |
15 ms |
24276 KB |
Output is correct |
28 |
Correct |
16 ms |
24420 KB |
Output is correct |
29 |
Correct |
14 ms |
24296 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24480 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
16 ms |
24300 KB |
Output is correct |
35 |
Correct |
14 ms |
24344 KB |
Output is correct |
36 |
Correct |
13 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24220 KB |
Output is correct |
38 |
Correct |
12 ms |
23764 KB |
Output is correct |
39 |
Correct |
14 ms |
24344 KB |
Output is correct |
40 |
Correct |
13 ms |
24276 KB |
Output is correct |
41 |
Correct |
15 ms |
24288 KB |
Output is correct |
42 |
Correct |
14 ms |
24284 KB |
Output is correct |
43 |
Correct |
14 ms |
24480 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
15 ms |
24404 KB |
Output is correct |
46 |
Correct |
15 ms |
24276 KB |
Output is correct |
47 |
Correct |
12 ms |
23764 KB |
Output is correct |
48 |
Correct |
14 ms |
24216 KB |
Output is correct |
49 |
Correct |
14 ms |
24284 KB |
Output is correct |
50 |
Correct |
14 ms |
24404 KB |
Output is correct |
51 |
Correct |
14 ms |
24212 KB |
Output is correct |
52 |
Correct |
13 ms |
24304 KB |
Output is correct |
53 |
Correct |
14 ms |
24288 KB |
Output is correct |
54 |
Correct |
13 ms |
24224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23856 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23728 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23756 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23844 KB |
Output is correct |
14 |
Correct |
14 ms |
23776 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23820 KB |
Output is correct |
17 |
Correct |
12 ms |
23824 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23760 KB |
Output is correct |
20 |
Correct |
11 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
90 ms |
38388 KB |
Output is correct |
27 |
Correct |
104 ms |
38424 KB |
Output is correct |
28 |
Correct |
14 ms |
24420 KB |
Output is correct |
29 |
Correct |
12 ms |
23856 KB |
Output is correct |
30 |
Correct |
12 ms |
23820 KB |
Output is correct |
31 |
Correct |
90 ms |
38552 KB |
Output is correct |
32 |
Correct |
14 ms |
24292 KB |
Output is correct |
33 |
Correct |
135 ms |
45588 KB |
Output is correct |
34 |
Correct |
95 ms |
38472 KB |
Output is correct |
35 |
Correct |
13 ms |
24248 KB |
Output is correct |
36 |
Incorrect |
116 ms |
39368 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
36932 KB |
Output is correct |
2 |
Correct |
119 ms |
40264 KB |
Output is correct |
3 |
Correct |
14 ms |
24276 KB |
Output is correct |
4 |
Correct |
16 ms |
24224 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23788 KB |
Output is correct |
7 |
Correct |
14 ms |
24216 KB |
Output is correct |
8 |
Correct |
128 ms |
40304 KB |
Output is correct |
9 |
Correct |
14 ms |
24276 KB |
Output is correct |
10 |
Correct |
95 ms |
39056 KB |
Output is correct |
11 |
Correct |
13 ms |
23820 KB |
Output is correct |
12 |
Correct |
127 ms |
38844 KB |
Output is correct |
13 |
Correct |
132 ms |
40272 KB |
Output is correct |
14 |
Correct |
132 ms |
42152 KB |
Output is correct |
15 |
Correct |
73 ms |
38308 KB |
Output is correct |
16 |
Correct |
15 ms |
24316 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
109 ms |
41620 KB |
Output is correct |
19 |
Correct |
157 ms |
46012 KB |
Output is correct |
20 |
Correct |
15 ms |
24204 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
99 ms |
40064 KB |
Output is correct |
23 |
Correct |
17 ms |
24288 KB |
Output is correct |
24 |
Correct |
117 ms |
39344 KB |
Output is correct |
25 |
Correct |
154 ms |
44736 KB |
Output is correct |
26 |
Correct |
15 ms |
24292 KB |
Output is correct |
27 |
Correct |
17 ms |
24372 KB |
Output is correct |
28 |
Correct |
14 ms |
24224 KB |
Output is correct |
29 |
Correct |
13 ms |
24276 KB |
Output is correct |
30 |
Correct |
15 ms |
24388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23856 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23728 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23756 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23844 KB |
Output is correct |
14 |
Correct |
14 ms |
23776 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23820 KB |
Output is correct |
17 |
Correct |
12 ms |
23824 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23760 KB |
Output is correct |
20 |
Correct |
11 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24264 KB |
Output is correct |
27 |
Correct |
15 ms |
24276 KB |
Output is correct |
28 |
Correct |
16 ms |
24420 KB |
Output is correct |
29 |
Correct |
14 ms |
24296 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24480 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
16 ms |
24300 KB |
Output is correct |
35 |
Correct |
14 ms |
24344 KB |
Output is correct |
36 |
Correct |
13 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24220 KB |
Output is correct |
38 |
Correct |
12 ms |
23764 KB |
Output is correct |
39 |
Correct |
14 ms |
24344 KB |
Output is correct |
40 |
Correct |
13 ms |
24276 KB |
Output is correct |
41 |
Correct |
15 ms |
24288 KB |
Output is correct |
42 |
Correct |
14 ms |
24284 KB |
Output is correct |
43 |
Correct |
14 ms |
24480 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
15 ms |
24404 KB |
Output is correct |
46 |
Correct |
15 ms |
24276 KB |
Output is correct |
47 |
Correct |
12 ms |
23764 KB |
Output is correct |
48 |
Correct |
14 ms |
24216 KB |
Output is correct |
49 |
Correct |
14 ms |
24284 KB |
Output is correct |
50 |
Correct |
14 ms |
24404 KB |
Output is correct |
51 |
Correct |
14 ms |
24212 KB |
Output is correct |
52 |
Correct |
13 ms |
24304 KB |
Output is correct |
53 |
Correct |
14 ms |
24288 KB |
Output is correct |
54 |
Correct |
13 ms |
24224 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
90 ms |
38388 KB |
Output is correct |
57 |
Correct |
104 ms |
38424 KB |
Output is correct |
58 |
Correct |
14 ms |
24420 KB |
Output is correct |
59 |
Correct |
12 ms |
23856 KB |
Output is correct |
60 |
Correct |
12 ms |
23820 KB |
Output is correct |
61 |
Correct |
90 ms |
38552 KB |
Output is correct |
62 |
Correct |
14 ms |
24292 KB |
Output is correct |
63 |
Correct |
135 ms |
45588 KB |
Output is correct |
64 |
Correct |
95 ms |
38472 KB |
Output is correct |
65 |
Correct |
13 ms |
24248 KB |
Output is correct |
66 |
Incorrect |
116 ms |
39368 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |