# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
545051 |
2022-04-03T12:48:06 Z |
chonka |
Mergers (JOI19_mergers) |
C++ |
|
103 ms |
41916 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 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 ;
}
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 ) ;
cout << ( dp[ 1 ] + 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 |
12 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23852 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23772 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
15 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23800 KB |
Output is correct |
13 |
Correct |
11 ms |
23756 KB |
Output is correct |
14 |
Correct |
14 ms |
23840 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23852 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23772 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
15 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23800 KB |
Output is correct |
13 |
Correct |
11 ms |
23756 KB |
Output is correct |
14 |
Correct |
14 ms |
23840 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23852 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23772 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
15 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23800 KB |
Output is correct |
13 |
Correct |
11 ms |
23756 KB |
Output is correct |
14 |
Correct |
14 ms |
23840 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
38396 KB |
Output is correct |
2 |
Incorrect |
103 ms |
41916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23852 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23772 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
15 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23800 KB |
Output is correct |
13 |
Correct |
11 ms |
23756 KB |
Output is correct |
14 |
Correct |
14 ms |
23840 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |