#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
int n , st[maxn] , fn[maxn] , dp[2][maxn] , par[2][maxn] , sz[maxn] , ar[maxn] , br[maxn];
vector <int> trav , adj[maxn] , child[maxn];
void dfs(int v , int p = -1)
{
st[v] = trav.size();
sz[v] = 1;
trav.push_back(v);
for(auto u : adj[v]) if(u != p)
{
dfs(u , v);
sz[v] += sz[u];
}
fn[v] = trav.size();
}
bool cmp(int a , int b)
{
return dp[1][a] - dp[0][a] < dp[1][b] - dp[0][b];
}
void solve(int v , int p = -1)
{
bool leaf = true;
int sum = 0;
for(auto u : adj[v]) if(u != p)
{
leaf = false;
solve(u , v);
child[v].push_back(u);
sum += dp[0][u];
}
if(leaf)
{
dp[1][v] = 0;
dp[0][v] = maxn << 2;
return;
}
sort(child[v].begin() , child[v].end() , cmp);
//cout << v << " " << p << " : " << endl;
//for(auto u : child[v])
// cout << u << " " << dp[0][u] << " " << dp[1][u] << endl;
//cout << endl;
int fsum = sum;
dp[1][v] = sum;
par[1][v] = 0;
fsum += (dp[1][child[v][0]] - dp[0][child[v][0]]);
for(int i = 1 ; i < child[v].size() ; i++)
{
int now = child[v][i];
fsum += (dp[1][now] - dp[0][now]);
if(fsum + 2 * (i + 1) < dp[1][v])
{
dp[1][v] = fsum + 2 * (i + 1);
par[1][v] = i + 1;
}
}
//cout << "SALAM" << endl;
//cout << v << " " << p << endl;
fsum = sum + dp[1][child[v][0]] - dp[0][child[v][0]];
dp[0][v] = fsum + 2;
par[0][v] = 1;
for(int i = 1 ; i < child[v].size() ; i++)
{
//cout << i << endl;
int now = child[v][i];
fsum += (dp[1][now] - dp[0][now]);
if(fsum + 2 * (i + 1) < dp[0][v])
{
dp[0][v] = fsum + 2 * (i + 1);
par[0][v] = i + 1;
}
}
//cout << v << " " << dp[0][v] << " " << dp[1][v] << endl;
}
void Build(int v , int ty)
{
//cout << v << " " << ty << " " << child[v].size() << endl;
if(child[v].size() == 0)
return;
for(int i = 0 ; i < par[ty][v] ; i++)
Build(child[v][i] , 1);
for(int i = par[ty][v] ; i < child[v].size() ; i++)
Build(child[v][i] , 0);
//cout << v << " " << ty << endl;
for(int i = 0 ; i + 1 < par[ty][v] ; i++)
ar[child[v][i]] = child[v][i + 1];
if(ty == 1)
{
if(par[ty][v] != 0)
ar[child[v][par[ty][v] - 1]] = child[v][0];
}
else
{
ar[v] = child[v][0];
ar[child[v][par[ty][v] - 1]] = v;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0 ; i < n - 1 ; i++)
{
int v , u; cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs(1);
//cout << "DFSED" << endl;
solve(1);
//cout << "SOLVED" << endl;
Build(1 , 0);
//cout << "BUILt" << endl;
int lar = 0 , sma = dp[0][1];
for(int i = 2 ; i <= n ; i++)
lar += min(sz[i] , n - sz[i]) * 2;
for(int i = 1 ; i <= n ; i++)
br[i] = trav[(st[i] + (n / 2)) % n];
cout << sma << " " << lar << endl;
for(int i = 1 ; i <= n ; i++)
cout << br[i] << " ";
cout << endl;
for(int i = 1 ; i <= n ; i++)
cout << br[i] << " ";
cout << endl;
return 0;
}
Compilation message
Village.cpp: In function 'void solve(long long int, long long int)':
Village.cpp:53:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 1 ; i < child[v].size() ; i++)
| ~~^~~~~~~~~~~~~~~~~
Village.cpp:68:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i = 1 ; i < child[v].size() ; i++)
| ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'void Build(long long int, long long int)':
Village.cpp:89:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = par[ty][v] ; i < child[v].size() ; i++)
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
2 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Partially correct |
2 ms |
5024 KB |
Partially correct |
5 |
Partially correct |
3 ms |
5024 KB |
Partially correct |
6 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
7 |
Correct |
2 ms |
4960 KB |
Output is correct |
8 |
Partially correct |
3 ms |
5024 KB |
Partially correct |
9 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
10 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
11 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
12 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
13 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
16 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
17 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
2 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
3 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
4 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
5 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
6 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
7 |
Partially correct |
2 ms |
5204 KB |
Partially correct |
8 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
9 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
10 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
11 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
14 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
15 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
16 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
17 |
Partially correct |
4 ms |
5076 KB |
Partially correct |
18 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
19 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
20 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
21 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
22 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
23 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
24 |
Partially correct |
3 ms |
5188 KB |
Partially correct |
25 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
26 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
27 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
28 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
29 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
30 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
2 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Partially correct |
2 ms |
5024 KB |
Partially correct |
5 |
Partially correct |
3 ms |
5024 KB |
Partially correct |
6 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
7 |
Correct |
2 ms |
4960 KB |
Output is correct |
8 |
Partially correct |
3 ms |
5024 KB |
Partially correct |
9 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
10 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
11 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
12 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
13 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Partially correct |
3 ms |
4948 KB |
Partially correct |
16 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
17 |
Partially correct |
2 ms |
4948 KB |
Partially correct |
18 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
19 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
20 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
21 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
22 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
23 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
24 |
Partially correct |
2 ms |
5204 KB |
Partially correct |
25 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
26 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
27 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
28 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
31 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
32 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
33 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
34 |
Partially correct |
4 ms |
5076 KB |
Partially correct |
35 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
36 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
37 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
38 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
39 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
40 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
41 |
Partially correct |
3 ms |
5188 KB |
Partially correct |
42 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
43 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
44 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
45 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
46 |
Partially correct |
3 ms |
5204 KB |
Partially correct |
47 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
48 |
Partially correct |
61 ms |
18700 KB |
Partially correct |
49 |
Partially correct |
87 ms |
20140 KB |
Partially correct |
50 |
Partially correct |
81 ms |
20024 KB |
Partially correct |
51 |
Partially correct |
46 ms |
16772 KB |
Partially correct |
52 |
Partially correct |
66 ms |
19940 KB |
Partially correct |
53 |
Partially correct |
58 ms |
18460 KB |
Partially correct |
54 |
Partially correct |
34 ms |
17224 KB |
Partially correct |
55 |
Partially correct |
92 ms |
29768 KB |
Partially correct |
56 |
Partially correct |
75 ms |
24584 KB |
Partially correct |
57 |
Partially correct |
72 ms |
22972 KB |
Partially correct |
58 |
Partially correct |
90 ms |
21648 KB |
Partially correct |
59 |
Partially correct |
61 ms |
20012 KB |
Partially correct |
60 |
Correct |
48 ms |
17668 KB |
Output is correct |
61 |
Partially correct |
45 ms |
18024 KB |
Partially correct |
62 |
Partially correct |
47 ms |
18444 KB |
Partially correct |
63 |
Partially correct |
48 ms |
17812 KB |
Partially correct |
64 |
Partially correct |
52 ms |
19868 KB |
Partially correct |
65 |
Partially correct |
53 ms |
19852 KB |
Partially correct |
66 |
Partially correct |
44 ms |
18020 KB |
Partially correct |
67 |
Partially correct |
42 ms |
14912 KB |
Partially correct |
68 |
Partially correct |
42 ms |
17616 KB |
Partially correct |
69 |
Partially correct |
52 ms |
19576 KB |
Partially correct |
70 |
Partially correct |
44 ms |
18752 KB |
Partially correct |
71 |
Partially correct |
32 ms |
15188 KB |
Partially correct |
72 |
Partially correct |
35 ms |
16576 KB |
Partially correct |
73 |
Partially correct |
48 ms |
19588 KB |
Partially correct |
74 |
Partially correct |
51 ms |
18492 KB |
Partially correct |
75 |
Partially correct |
68 ms |
19920 KB |
Partially correct |
76 |
Partially correct |
74 ms |
19636 KB |
Partially correct |
77 |
Partially correct |
65 ms |
19864 KB |
Partially correct |
78 |
Partially correct |
34 ms |
15020 KB |
Partially correct |
79 |
Partially correct |
51 ms |
16316 KB |
Partially correct |
80 |
Partially correct |
76 ms |
19980 KB |
Partially correct |
81 |
Partially correct |
51 ms |
19876 KB |
Partially correct |
82 |
Partially correct |
46 ms |
19144 KB |
Partially correct |