#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
#define fi first
#define se second
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MAXN = 5e5 + 7 ;
int n ;
vector < int > v[ MAXN ] , ord ;
int cnt[ MAXN ] , root , prv[ MAXN ] ;
int spec ;
void init ( int x , int lst ) {
bool leaf = true ;
cnt[ x ] = 0 ;
for ( auto y : v[ x ] ) {
if ( y == lst ) { continue ; }
leaf = false ;
prv[ y ] = x ;
init ( y , x ) ;
cnt[ x ] += cnt[ y ] ;
}
if ( leaf == true ) {
++ cnt[ x ] ;
}
}
int get_centroid ( int x , int lst , int targ ) {
for ( auto y : v[ x ] ) {
if ( y == lst ) { continue ; }
if ( cnt[ y ] >= targ ) {
return get_centroid ( y , x , targ ) ;
}
}
return x ;
}
void dfs ( int x , int lst ) {
bool leaf = true ;
for ( auto y : v[ x ] ) {
if ( y == lst ) { continue ; }
leaf = false ;
dfs ( y , x ) ;
}
if ( leaf == true && cnt[ x ] > 0 ) {
ord.push_back ( x ) ;
}
}
vector < pair < int , int > > ans ;
void solve ( ) {
cin >> n ;
for ( int i = 1 , x , y ; i < n ; ++ i ) {
cin >> x >> y ;
v[ x ].push_back ( y ) ;
v[ y ].push_back ( x ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
if ( ( int ) v[ i ].size ( ) >= 2 ) {
root = i ; break ;
}
}
init ( root , -1 ) ;
root = get_centroid ( root , -1 , cnt[ root ] / 2 + 1 ) ;
init ( root , -1 ) ;
dfs ( root , -1 ) ;
for ( int i = 0 ; i < cnt[ root ] / 2 ; ++ i ) {
ans.push_back ( { ord[ i ] , ord[ i + cnt[ root ] / 2 ] } ) ;
}
if ( ( cnt[ root ] % 2 ) == 1 ) {
ans.push_back ( { root , ord.back ( ) } ) ;
}
int sz = ans.size ( ) ;
cout << sz << "\n" ;
for ( auto [ x , y ] : ans ) {
cout << x << " " << y << "\n" ;
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
12076 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12076 KB |
Output is correct |
8 |
Correct |
6 ms |
12072 KB |
Output is correct |
9 |
Correct |
6 ms |
12076 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
12076 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
8 ms |
11996 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
12076 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12076 KB |
Output is correct |
8 |
Correct |
6 ms |
12072 KB |
Output is correct |
9 |
Correct |
6 ms |
12076 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
12076 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
8 ms |
11996 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
6 ms |
11988 KB |
Output is correct |
22 |
Correct |
8 ms |
12212 KB |
Output is correct |
23 |
Correct |
7 ms |
12128 KB |
Output is correct |
24 |
Correct |
7 ms |
12116 KB |
Output is correct |
25 |
Correct |
8 ms |
12092 KB |
Output is correct |
26 |
Correct |
7 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
12152 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12152 KB |
Output is correct |
30 |
Correct |
6 ms |
11988 KB |
Output is correct |
31 |
Correct |
9 ms |
12116 KB |
Output is correct |
32 |
Correct |
6 ms |
11988 KB |
Output is correct |
33 |
Correct |
6 ms |
12072 KB |
Output is correct |
34 |
Correct |
6 ms |
11988 KB |
Output is correct |
35 |
Correct |
6 ms |
11988 KB |
Output is correct |
36 |
Correct |
6 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12080 KB |
Output is correct |
38 |
Correct |
7 ms |
12116 KB |
Output is correct |
39 |
Correct |
6 ms |
12072 KB |
Output is correct |
40 |
Correct |
6 ms |
12072 KB |
Output is correct |
41 |
Correct |
6 ms |
11988 KB |
Output is correct |
42 |
Correct |
6 ms |
12116 KB |
Output is correct |
43 |
Correct |
7 ms |
12116 KB |
Output is correct |
44 |
Correct |
6 ms |
11988 KB |
Output is correct |
45 |
Correct |
6 ms |
11988 KB |
Output is correct |
46 |
Correct |
6 ms |
12136 KB |
Output is correct |
47 |
Correct |
7 ms |
11988 KB |
Output is correct |
48 |
Correct |
6 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
12116 KB |
Output is correct |
50 |
Correct |
6 ms |
11988 KB |
Output is correct |
51 |
Correct |
6 ms |
12076 KB |
Output is correct |
52 |
Correct |
6 ms |
11988 KB |
Output is correct |
53 |
Correct |
7 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12016 KB |
Output is correct |
55 |
Correct |
6 ms |
12072 KB |
Output is correct |
56 |
Correct |
8 ms |
12028 KB |
Output is correct |
57 |
Correct |
7 ms |
12072 KB |
Output is correct |
58 |
Correct |
8 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
12076 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12076 KB |
Output is correct |
8 |
Correct |
6 ms |
12072 KB |
Output is correct |
9 |
Correct |
6 ms |
12076 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
12076 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
8 ms |
11996 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
6 ms |
11988 KB |
Output is correct |
22 |
Correct |
8 ms |
12212 KB |
Output is correct |
23 |
Correct |
7 ms |
12128 KB |
Output is correct |
24 |
Correct |
7 ms |
12116 KB |
Output is correct |
25 |
Correct |
8 ms |
12092 KB |
Output is correct |
26 |
Correct |
7 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
12152 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12152 KB |
Output is correct |
30 |
Correct |
6 ms |
11988 KB |
Output is correct |
31 |
Correct |
9 ms |
12116 KB |
Output is correct |
32 |
Correct |
6 ms |
11988 KB |
Output is correct |
33 |
Correct |
6 ms |
12072 KB |
Output is correct |
34 |
Correct |
6 ms |
11988 KB |
Output is correct |
35 |
Correct |
6 ms |
11988 KB |
Output is correct |
36 |
Correct |
6 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12080 KB |
Output is correct |
38 |
Correct |
7 ms |
12116 KB |
Output is correct |
39 |
Correct |
6 ms |
12072 KB |
Output is correct |
40 |
Correct |
6 ms |
12072 KB |
Output is correct |
41 |
Correct |
6 ms |
11988 KB |
Output is correct |
42 |
Correct |
6 ms |
12116 KB |
Output is correct |
43 |
Correct |
7 ms |
12116 KB |
Output is correct |
44 |
Correct |
6 ms |
11988 KB |
Output is correct |
45 |
Correct |
6 ms |
11988 KB |
Output is correct |
46 |
Correct |
6 ms |
12136 KB |
Output is correct |
47 |
Correct |
7 ms |
11988 KB |
Output is correct |
48 |
Correct |
6 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
12116 KB |
Output is correct |
50 |
Correct |
6 ms |
11988 KB |
Output is correct |
51 |
Correct |
6 ms |
12076 KB |
Output is correct |
52 |
Correct |
6 ms |
11988 KB |
Output is correct |
53 |
Correct |
7 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12016 KB |
Output is correct |
55 |
Correct |
6 ms |
12072 KB |
Output is correct |
56 |
Correct |
8 ms |
12028 KB |
Output is correct |
57 |
Correct |
7 ms |
12072 KB |
Output is correct |
58 |
Correct |
8 ms |
12116 KB |
Output is correct |
59 |
Correct |
348 ms |
40764 KB |
Output is correct |
60 |
Correct |
432 ms |
44020 KB |
Output is correct |
61 |
Correct |
7 ms |
11988 KB |
Output is correct |
62 |
Correct |
7 ms |
12080 KB |
Output is correct |
63 |
Correct |
312 ms |
37840 KB |
Output is correct |
64 |
Correct |
8 ms |
12628 KB |
Output is correct |
65 |
Correct |
16 ms |
14676 KB |
Output is correct |
66 |
Correct |
217 ms |
34656 KB |
Output is correct |
67 |
Correct |
618 ms |
59340 KB |
Output is correct |
68 |
Correct |
628 ms |
60268 KB |
Output is correct |
69 |
Correct |
13 ms |
13140 KB |
Output is correct |
70 |
Correct |
79 ms |
23060 KB |
Output is correct |
71 |
Correct |
284 ms |
47708 KB |
Output is correct |
72 |
Correct |
301 ms |
46392 KB |
Output is correct |
73 |
Correct |
102 ms |
20452 KB |
Output is correct |
74 |
Correct |
463 ms |
42520 KB |
Output is correct |
75 |
Correct |
25 ms |
15820 KB |
Output is correct |
76 |
Correct |
290 ms |
47388 KB |
Output is correct |
77 |
Correct |
282 ms |
47472 KB |
Output is correct |
78 |
Correct |
31 ms |
16616 KB |
Output is correct |
79 |
Correct |
632 ms |
56392 KB |
Output is correct |
80 |
Correct |
8 ms |
12372 KB |
Output is correct |
81 |
Correct |
111 ms |
23088 KB |
Output is correct |
82 |
Correct |
388 ms |
44896 KB |
Output is correct |
83 |
Correct |
454 ms |
42540 KB |
Output is correct |
84 |
Correct |
460 ms |
42508 KB |
Output is correct |
85 |
Correct |
463 ms |
42504 KB |
Output is correct |
86 |
Correct |
426 ms |
43948 KB |
Output is correct |
87 |
Correct |
442 ms |
44016 KB |
Output is correct |