# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
545054 |
2022-04-03T13:15:43 Z |
chonka |
Mergers (JOI19_mergers) |
C++ |
|
1287 ms |
139420 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 prv[ MAXN ] , rnk[ MAXN ] ;
int hh[ MAXN ] ;
int get ( int x ) {
if ( prv[ x ] == -1 ) { return x ; }
int y = get ( prv[ x ] ) ;
prv[ x ] = y ;
return y ;
}
void unite ( int x , int y ) {
int k1 = get ( x ) , k2 = get ( y ) ;
if ( k1 != k2 ) {
if ( rnk[ k1 ] >= rnk[ k2 ] ) {
rnk[ k1 ] += ( rnk[ k1 ] == rnk[ k2 ] ) ;
prv[ k2 ] = k1 ;
}
else {
prv[ k1 ] = k2 ;
}
}
}
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 ) ;
}
if ( vertex == 1 ) { return ret ; }
if ( ret < lvl[ vertex ] ) {
unite ( vertex , prv ) ;
}
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 ( ) ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
prv[ i ] = -1 ;
}
dfs ( 1 , -1 ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
for ( auto j : v[ i ] ) {
if ( get ( i ) != get ( j ) ) {
++ hh[ get ( i ) ] ;
}
}
}
int ans = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
if ( get ( i ) == i && hh[ i ] == 1 ) {
++ ans ;
}
}
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 |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23824 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23868 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23772 KB |
Output is correct |
8 |
Correct |
12 ms |
23812 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23876 KB |
Output is correct |
11 |
Correct |
12 ms |
23824 KB |
Output is correct |
12 |
Correct |
14 ms |
23856 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
16 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23824 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23868 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23772 KB |
Output is correct |
8 |
Correct |
12 ms |
23812 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23876 KB |
Output is correct |
11 |
Correct |
12 ms |
23824 KB |
Output is correct |
12 |
Correct |
14 ms |
23856 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
16 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23820 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
17 ms |
24276 KB |
Output is correct |
27 |
Correct |
14 ms |
24288 KB |
Output is correct |
28 |
Correct |
16 ms |
24404 KB |
Output is correct |
29 |
Correct |
15 ms |
24372 KB |
Output is correct |
30 |
Correct |
15 ms |
24288 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24480 KB |
Output is correct |
33 |
Correct |
13 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24184 KB |
Output is correct |
35 |
Correct |
14 ms |
24284 KB |
Output is correct |
36 |
Correct |
14 ms |
24284 KB |
Output is correct |
37 |
Correct |
14 ms |
24276 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
18 ms |
24252 KB |
Output is correct |
41 |
Correct |
15 ms |
24276 KB |
Output is correct |
42 |
Correct |
18 ms |
24332 KB |
Output is correct |
43 |
Correct |
17 ms |
24404 KB |
Output is correct |
44 |
Correct |
13 ms |
23892 KB |
Output is correct |
45 |
Correct |
14 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24204 KB |
Output is correct |
47 |
Correct |
12 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24276 KB |
Output is correct |
49 |
Correct |
14 ms |
24276 KB |
Output is correct |
50 |
Correct |
14 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24276 KB |
Output is correct |
52 |
Correct |
12 ms |
24208 KB |
Output is correct |
53 |
Correct |
13 ms |
24276 KB |
Output is correct |
54 |
Correct |
15 ms |
24292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23824 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23868 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23772 KB |
Output is correct |
8 |
Correct |
12 ms |
23812 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23876 KB |
Output is correct |
11 |
Correct |
12 ms |
23824 KB |
Output is correct |
12 |
Correct |
14 ms |
23856 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
16 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23820 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
85 ms |
37336 KB |
Output is correct |
27 |
Correct |
136 ms |
37404 KB |
Output is correct |
28 |
Correct |
14 ms |
24276 KB |
Output is correct |
29 |
Correct |
14 ms |
23820 KB |
Output is correct |
30 |
Correct |
15 ms |
23852 KB |
Output is correct |
31 |
Correct |
108 ms |
37456 KB |
Output is correct |
32 |
Correct |
13 ms |
24232 KB |
Output is correct |
33 |
Correct |
120 ms |
44256 KB |
Output is correct |
34 |
Correct |
106 ms |
37380 KB |
Output is correct |
35 |
Correct |
16 ms |
24404 KB |
Output is correct |
36 |
Correct |
133 ms |
38060 KB |
Output is correct |
37 |
Correct |
15 ms |
24220 KB |
Output is correct |
38 |
Correct |
14 ms |
24316 KB |
Output is correct |
39 |
Correct |
77 ms |
38668 KB |
Output is correct |
40 |
Correct |
18 ms |
24532 KB |
Output is correct |
41 |
Correct |
110 ms |
38696 KB |
Output is correct |
42 |
Correct |
149 ms |
40724 KB |
Output is correct |
43 |
Correct |
12 ms |
23764 KB |
Output is correct |
44 |
Correct |
124 ms |
45832 KB |
Output is correct |
45 |
Correct |
127 ms |
42500 KB |
Output is correct |
46 |
Correct |
14 ms |
24344 KB |
Output is correct |
47 |
Correct |
15 ms |
24296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
37368 KB |
Output is correct |
2 |
Correct |
112 ms |
40648 KB |
Output is correct |
3 |
Correct |
15 ms |
24276 KB |
Output is correct |
4 |
Correct |
14 ms |
24276 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
24192 KB |
Output is correct |
8 |
Correct |
120 ms |
39036 KB |
Output is correct |
9 |
Correct |
14 ms |
24276 KB |
Output is correct |
10 |
Correct |
114 ms |
37796 KB |
Output is correct |
11 |
Correct |
13 ms |
23884 KB |
Output is correct |
12 |
Correct |
128 ms |
37744 KB |
Output is correct |
13 |
Correct |
121 ms |
38900 KB |
Output is correct |
14 |
Correct |
141 ms |
40396 KB |
Output is correct |
15 |
Correct |
90 ms |
37232 KB |
Output is correct |
16 |
Correct |
18 ms |
24212 KB |
Output is correct |
17 |
Correct |
14 ms |
23764 KB |
Output is correct |
18 |
Correct |
116 ms |
40392 KB |
Output is correct |
19 |
Correct |
170 ms |
44324 KB |
Output is correct |
20 |
Correct |
13 ms |
24276 KB |
Output is correct |
21 |
Correct |
14 ms |
23844 KB |
Output is correct |
22 |
Correct |
122 ms |
38824 KB |
Output is correct |
23 |
Correct |
17 ms |
24312 KB |
Output is correct |
24 |
Correct |
126 ms |
38124 KB |
Output is correct |
25 |
Correct |
134 ms |
42936 KB |
Output is correct |
26 |
Correct |
14 ms |
24400 KB |
Output is correct |
27 |
Correct |
15 ms |
24404 KB |
Output is correct |
28 |
Correct |
14 ms |
24304 KB |
Output is correct |
29 |
Correct |
15 ms |
24276 KB |
Output is correct |
30 |
Correct |
15 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23824 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23868 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23772 KB |
Output is correct |
8 |
Correct |
12 ms |
23812 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23876 KB |
Output is correct |
11 |
Correct |
12 ms |
23824 KB |
Output is correct |
12 |
Correct |
14 ms |
23856 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
16 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23820 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
17 ms |
24276 KB |
Output is correct |
27 |
Correct |
14 ms |
24288 KB |
Output is correct |
28 |
Correct |
16 ms |
24404 KB |
Output is correct |
29 |
Correct |
15 ms |
24372 KB |
Output is correct |
30 |
Correct |
15 ms |
24288 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24480 KB |
Output is correct |
33 |
Correct |
13 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24184 KB |
Output is correct |
35 |
Correct |
14 ms |
24284 KB |
Output is correct |
36 |
Correct |
14 ms |
24284 KB |
Output is correct |
37 |
Correct |
14 ms |
24276 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
18 ms |
24252 KB |
Output is correct |
41 |
Correct |
15 ms |
24276 KB |
Output is correct |
42 |
Correct |
18 ms |
24332 KB |
Output is correct |
43 |
Correct |
17 ms |
24404 KB |
Output is correct |
44 |
Correct |
13 ms |
23892 KB |
Output is correct |
45 |
Correct |
14 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24204 KB |
Output is correct |
47 |
Correct |
12 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24276 KB |
Output is correct |
49 |
Correct |
14 ms |
24276 KB |
Output is correct |
50 |
Correct |
14 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24276 KB |
Output is correct |
52 |
Correct |
12 ms |
24208 KB |
Output is correct |
53 |
Correct |
13 ms |
24276 KB |
Output is correct |
54 |
Correct |
15 ms |
24292 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
85 ms |
37336 KB |
Output is correct |
57 |
Correct |
136 ms |
37404 KB |
Output is correct |
58 |
Correct |
14 ms |
24276 KB |
Output is correct |
59 |
Correct |
14 ms |
23820 KB |
Output is correct |
60 |
Correct |
15 ms |
23852 KB |
Output is correct |
61 |
Correct |
108 ms |
37456 KB |
Output is correct |
62 |
Correct |
13 ms |
24232 KB |
Output is correct |
63 |
Correct |
120 ms |
44256 KB |
Output is correct |
64 |
Correct |
106 ms |
37380 KB |
Output is correct |
65 |
Correct |
16 ms |
24404 KB |
Output is correct |
66 |
Correct |
133 ms |
38060 KB |
Output is correct |
67 |
Correct |
15 ms |
24220 KB |
Output is correct |
68 |
Correct |
14 ms |
24316 KB |
Output is correct |
69 |
Correct |
77 ms |
38668 KB |
Output is correct |
70 |
Correct |
18 ms |
24532 KB |
Output is correct |
71 |
Correct |
110 ms |
38696 KB |
Output is correct |
72 |
Correct |
149 ms |
40724 KB |
Output is correct |
73 |
Correct |
12 ms |
23764 KB |
Output is correct |
74 |
Correct |
124 ms |
45832 KB |
Output is correct |
75 |
Correct |
127 ms |
42500 KB |
Output is correct |
76 |
Correct |
14 ms |
24344 KB |
Output is correct |
77 |
Correct |
15 ms |
24296 KB |
Output is correct |
78 |
Correct |
82 ms |
37368 KB |
Output is correct |
79 |
Correct |
112 ms |
40648 KB |
Output is correct |
80 |
Correct |
15 ms |
24276 KB |
Output is correct |
81 |
Correct |
14 ms |
24276 KB |
Output is correct |
82 |
Correct |
13 ms |
23764 KB |
Output is correct |
83 |
Correct |
13 ms |
23756 KB |
Output is correct |
84 |
Correct |
14 ms |
24192 KB |
Output is correct |
85 |
Correct |
120 ms |
39036 KB |
Output is correct |
86 |
Correct |
14 ms |
24276 KB |
Output is correct |
87 |
Correct |
114 ms |
37796 KB |
Output is correct |
88 |
Correct |
13 ms |
23884 KB |
Output is correct |
89 |
Correct |
128 ms |
37744 KB |
Output is correct |
90 |
Correct |
121 ms |
38900 KB |
Output is correct |
91 |
Correct |
141 ms |
40396 KB |
Output is correct |
92 |
Correct |
90 ms |
37232 KB |
Output is correct |
93 |
Correct |
18 ms |
24212 KB |
Output is correct |
94 |
Correct |
14 ms |
23764 KB |
Output is correct |
95 |
Correct |
116 ms |
40392 KB |
Output is correct |
96 |
Correct |
170 ms |
44324 KB |
Output is correct |
97 |
Correct |
13 ms |
24276 KB |
Output is correct |
98 |
Correct |
14 ms |
23844 KB |
Output is correct |
99 |
Correct |
122 ms |
38824 KB |
Output is correct |
100 |
Correct |
17 ms |
24312 KB |
Output is correct |
101 |
Correct |
126 ms |
38124 KB |
Output is correct |
102 |
Correct |
134 ms |
42936 KB |
Output is correct |
103 |
Correct |
14 ms |
24400 KB |
Output is correct |
104 |
Correct |
15 ms |
24404 KB |
Output is correct |
105 |
Correct |
14 ms |
24304 KB |
Output is correct |
106 |
Correct |
15 ms |
24276 KB |
Output is correct |
107 |
Correct |
15 ms |
24276 KB |
Output is correct |
108 |
Correct |
606 ms |
107476 KB |
Output is correct |
109 |
Correct |
892 ms |
114608 KB |
Output is correct |
110 |
Correct |
940 ms |
125872 KB |
Output is correct |
111 |
Correct |
1136 ms |
139420 KB |
Output is correct |
112 |
Correct |
1114 ms |
129844 KB |
Output is correct |
113 |
Correct |
758 ms |
116344 KB |
Output is correct |
114 |
Correct |
744 ms |
98540 KB |
Output is correct |
115 |
Correct |
733 ms |
98568 KB |
Output is correct |
116 |
Correct |
787 ms |
105456 KB |
Output is correct |
117 |
Correct |
945 ms |
116584 KB |
Output is correct |
118 |
Correct |
681 ms |
100568 KB |
Output is correct |
119 |
Correct |
932 ms |
116460 KB |
Output is correct |
120 |
Correct |
1287 ms |
130396 KB |
Output is correct |
121 |
Correct |
968 ms |
117212 KB |
Output is correct |
122 |
Correct |
993 ms |
111464 KB |
Output is correct |
123 |
Correct |
830 ms |
117988 KB |
Output is correct |
124 |
Correct |
1234 ms |
116164 KB |
Output is correct |