#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
int sizee[MAX_N + 1], depth[MAX_N + 1], pairMinn[MAX_N + 1], pairMaxx[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int n;
int c;
void findCentroid( int u, int p ) {
int maxSizee = 0;
sizee[u] = 1;
for ( int v: adj[u] ) {
if ( v == p )
continue;
findCentroid( v, u );
sizee[u] += sizee[v];
maxSizee = max( maxSizee, sizee[v] );
}
maxSizee = max( maxSizee, n - sizee[u] );
if ( maxSizee <= n / 2 )
c = u;
}
void calcSizes( int u, int p ) {
sizee[u] = 1;
for ( int v: adj[u] ) {
if ( v == p )
continue;
calcSizes( v, u );
sizee[u] += sizee[v];
}
}
long long maxSumDist;
vector<int> vert;
void calcDepths( int u, int p ) {
depth[u] = depth[p] + 1;
maxSumDist += 2 * depth[u];
for ( int v: adj[u] ) {
if ( v == p )
continue;
calcDepths( v, u );
}
vert.push_back( u );
}
void getMaxSumDist() {
depth[0] = -1;
calcDepths( c, 0 );
for ( int i = 0; i < n / 2; i++ ) {
pairMaxx[vert[i]] = vert[n / 2 + i];
pairMaxx[vert[n / 2 + i]] = vert[i];
}
if ( pairMaxx[c] == 0 ) {
int a = adj[c][0], b = pairMaxx[a];
pairMaxx[a] = b;
pairMaxx[b] = c;
pairMaxx[c] = a;
}
}
long long minSumDist;
void dfs( int u, int p ) {
vector<int> unpaired;
sizee[u] = 1;
for ( int v: adj[u] ) {
if ( v == p )
continue;
dfs( v, u );
sizee[u] += sizee[v];
if ( pairMinn[v] == 0 )
unpaired.push_back( v );
}
if ( unpaired.size() >= 1 ) {
unpaired.push_back( u );
for ( int i = 0; i < (int)unpaired.size(); i++ )
pairMinn[unpaired[i]] = unpaired[(i + 1) % unpaired.size()];
minSumDist += 2 * (unpaired.size() - 1);
}
}
void getMinSumDist() {
minSumDist = 0;
dfs( c, 0 );
if ( pairMinn[c] == 0 ) {
int a = adj[c][0];
for ( int v: adj[c] ) {
if ( sizee[v] % 2 == 1 )
a = v;
}
int b = pairMinn[a];
pairMinn[a] = b;
pairMinn[b] = c;
pairMinn[c] = a;
if ( sizee[a] % 2 == 0 )
minSumDist += 2;
}
}
int main() {
cin >> n;
for ( int i = 0; i < n - 1; i++ ) {
int u, v;
cin >> u >> v;
adj[u].push_back( v );
adj[v].push_back( u );
}
findCentroid( 1, 0 );
getMinSumDist();
getMaxSumDist();
cout << minSumDist << " " << maxSumDist << "\n";
for ( int v = 1; v <= n; v++ )
cout << pairMinn[v] << " ";
cout << "\n";
for ( int v = 1; v <= n; v++ )
cout << pairMaxx[v] << " ";
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
25 |
Correct |
1 ms |
2904 KB |
Output is correct |
26 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2664 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
1 ms |
2652 KB |
Output is correct |
40 |
Correct |
1 ms |
2648 KB |
Output is correct |
41 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
42 |
Correct |
1 ms |
2904 KB |
Output is correct |
43 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
2 ms |
2652 KB |
Output is correct |
47 |
Correct |
2 ms |
2652 KB |
Output is correct |
48 |
Correct |
67 ms |
8652 KB |
Output is correct |
49 |
Correct |
74 ms |
9264 KB |
Output is correct |
50 |
Correct |
72 ms |
9168 KB |
Output is correct |
51 |
Correct |
63 ms |
7884 KB |
Output is correct |
52 |
Correct |
83 ms |
9168 KB |
Output is correct |
53 |
Correct |
64 ms |
8480 KB |
Output is correct |
54 |
Correct |
45 ms |
9464 KB |
Output is correct |
55 |
Correct |
96 ms |
16668 KB |
Output is correct |
56 |
Correct |
79 ms |
12824 KB |
Output is correct |
57 |
Correct |
82 ms |
11540 KB |
Output is correct |
58 |
Correct |
97 ms |
10440 KB |
Output is correct |
59 |
Correct |
79 ms |
9212 KB |
Output is correct |
60 |
Correct |
70 ms |
9920 KB |
Output is correct |
61 |
Correct |
73 ms |
9620 KB |
Output is correct |
62 |
Correct |
85 ms |
9520 KB |
Output is correct |
63 |
Correct |
78 ms |
9244 KB |
Output is correct |
64 |
Correct |
70 ms |
9592 KB |
Output is correct |
65 |
Correct |
68 ms |
9676 KB |
Output is correct |
66 |
Correct |
58 ms |
9236 KB |
Output is correct |
67 |
Correct |
46 ms |
7740 KB |
Output is correct |
68 |
Correct |
56 ms |
8628 KB |
Output is correct |
69 |
Correct |
73 ms |
9556 KB |
Output is correct |
70 |
Correct |
58 ms |
9108 KB |
Output is correct |
71 |
Correct |
56 ms |
7636 KB |
Output is correct |
72 |
Correct |
51 ms |
8164 KB |
Output is correct |
73 |
Correct |
63 ms |
9560 KB |
Output is correct |
74 |
Correct |
57 ms |
8948 KB |
Output is correct |
75 |
Correct |
71 ms |
8984 KB |
Output is correct |
76 |
Partially correct |
69 ms |
9168 KB |
Partially correct |
77 |
Correct |
77 ms |
9420 KB |
Output is correct |
78 |
Partially correct |
49 ms |
7332 KB |
Partially correct |
79 |
Correct |
56 ms |
7868 KB |
Output is correct |
80 |
Correct |
72 ms |
9168 KB |
Output is correct |
81 |
Partially correct |
62 ms |
9416 KB |
Partially correct |
82 |
Correct |
70 ms |
9424 KB |
Output is correct |