#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back //emplace_back
#define pp pop_back
#define pii pair<ll, ll> //pair<int, int>
#define all(x) (x).begin(),(x).end()
#define mp(a,b) make_pair(a , b)
#define sz(x) (ll)(x).size()
#define F first
#define S second
#define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
#define debug(x) cout << #x << " : " << x << endl << flush
#define endl '\n'
#define arr(x) array<ll , (x)>
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
ll Sum(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a += b;
return a % MOD;
}
ll Mul(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a *= b;
return a % MOD;
}
ll Pow(ll a , ll b , ll MOD)
{
ll res = 1;
while(b)
{
if((b & 1))res = Mul(res , a , MOD);
a = Mul(a , a , MOD);
b >>= 1;
}
return res;
}
ll Min(ll a , ll b)
{
if(a > b)return b;
return a;
}
ll Max(ll a , ll b)
{
if(a > b)return a;
return b;
}
ll Ceil(ll a , ll b)
{
return a + (b-1) / b;
}
/////////////////////
//VALS
ll n;
const ll maxn = 2e5;
vector<ll> adj[maxn];
ll mx[maxn];
ll mn[maxn];
ll mx_ans, mn_ans;
ll sz[maxn];
ll centroid;
vector<ll> e;
/////////////////////
//FUNCTIONS
void dfs_mx_ans(ll u = 0, ll par = 0)
{
sz[u] = 1;
for(auto v : adj[u])
{
if(v == par)continue;
dfs_mx_ans(v, u);
sz[u] += sz[v];
}
ll x = Min(sz[u], n-sz[u]);
mx_ans += x*2;
}
void dfs_get_centroid(ll u = 0, ll par = 0)
{
bool flag = (n - sz[u] <= (n >> 1));
for(auto v : adj[u])
{
if(v == par)continue;
dfs_get_centroid(v, u);
if(sz[v] > (n >> 1))flag = 0;
}
if(flag)centroid = u;
}
void dfs_get_euler(ll u = centroid, ll par = centroid)
{
e.pb(u);
for(auto v : adj[u])
{
if(v == par)continue;
dfs_get_euler(v, u);
}
}
void dfs_mn(ll u = 0, ll par = 0)
{
for(auto v : adj[u])
{
if(v == par)continue;
dfs_mn(v, u);
}
if(mn[u] == u and u != 0)//swap with par
{
mn_ans += 2;
// ll tmp = mn[u];
// mn[u] = mn[par];
// mn[par] = tmp;
swap(mn[u], mn[par]);
}
if(mn[u] == u and u == 0)
{
mn_ans += 2;
// ll tmp = mn[u];
// mn[u] = mn[adj[u][0]];
// mn[adj[u][0]] = tmp;
swap(mn[u], mn[adj[u][0]]);
}
}
/////////////////////
//SOLVE
void solve()
{
cin >> n;
For(i,n-1)
{
ll u, v;
cin >> u >> v;
u--;v--;
adj[u].pb(v);
adj[v].pb(u);
}
// if(n == 2)
// {
// cout << 2 << " " << 2 << endl;
// cout << 2 << " " << 1 << endl;
// cout << 2 << " " << 1 << endl;
// return;
// }
//Max
dfs_mx_ans();
// debug(":L");
dfs_get_centroid();
// debug(":L");
dfs_get_euler();
// debug(":L");
if(n & 1)
{
mx[e[0]] = e[n/2]+1;
mx[e[n/2]] = e[n-1]+1;
mx[e[n-1]] = e[0]+1;
}
For(i,(n+1) / 2)
{
if(mx[e[i]])continue;
mx[e[i]] = e[i+n/2]+1;
mx[e[i+n/2]] = e[i]+1;
}
// cout << mx_ans << endl;
// For(i,n)cout << mx[i] + 1 << " ";
// cout << endl;
//Min
For(i,n)
mn[i] = i;
dfs_mn();
//print
cout << mn_ans << " " << mx_ans << endl;
For(i,n)cout << mn[i] + 1 << " ";
cout << endl;
For(i,n)cout << mx[i]/* + 1 */<< " ";
cout << endl;
}
/////////////////////
//MAIN
int main()
{
FAST;
ll t = 1;
// cin >> t;
while(t--)
{
solve();
}
}
/////////////////////
/*
ZZZZZZZ A M M IIIIIII N N
Z A A M M M M I NN N
Z A A M M M M I N N N
Z AAAAAAA M M M I N N N
Z A A M M I N N N
Z A A M M I N NN
ZZZZZZZ A A M M IIIIIII N N TREE
*/
Compilation message
Village.cpp: In function 'void solve()':
Village.cpp:17:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
17 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
Village.cpp:151:2: note: in expansion of macro 'For'
151 | For(i,n-1)
| ^~~
Village.cpp:17:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
17 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
Village.cpp:184:2: note: in expansion of macro 'For'
184 | For(i,(n+1) / 2)
| ^~~
Village.cpp:17:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
17 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
Village.cpp:196:2: note: in expansion of macro 'For'
196 | For(i,n)
| ^~~
Village.cpp:17:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
17 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
Village.cpp:204:2: note: in expansion of macro 'For'
204 | For(i,n)cout << mn[i] + 1 << " ";
| ^~~
Village.cpp:17:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
17 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
Village.cpp:206:2: note: in expansion of macro 'For'
206 | For(i,n)cout << mx[i]/* + 1 */<< " ";
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
9560 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7628 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7520 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
9564 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9564 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
9564 KB |
Output is correct |
9 |
Correct |
3 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
7688 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7768 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7628 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
7768 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
7512 KB |
Output is correct |
24 |
Correct |
2 ms |
7516 KB |
Output is correct |
25 |
Correct |
2 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
7516 KB |
Output is correct |
29 |
Correct |
3 ms |
9572 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
9560 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7628 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7520 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7764 KB |
Output is correct |
18 |
Correct |
2 ms |
7512 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
7688 KB |
Output is correct |
28 |
Correct |
2 ms |
7512 KB |
Output is correct |
29 |
Correct |
2 ms |
7768 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
3 ms |
7516 KB |
Output is correct |
32 |
Correct |
3 ms |
7628 KB |
Output is correct |
33 |
Correct |
3 ms |
7512 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
3 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
7768 KB |
Output is correct |
37 |
Correct |
3 ms |
7512 KB |
Output is correct |
38 |
Correct |
3 ms |
7516 KB |
Output is correct |
39 |
Correct |
3 ms |
9564 KB |
Output is correct |
40 |
Correct |
3 ms |
7512 KB |
Output is correct |
41 |
Correct |
2 ms |
7516 KB |
Output is correct |
42 |
Correct |
2 ms |
7516 KB |
Output is correct |
43 |
Correct |
3 ms |
7516 KB |
Output is correct |
44 |
Correct |
3 ms |
9564 KB |
Output is correct |
45 |
Correct |
3 ms |
7516 KB |
Output is correct |
46 |
Correct |
3 ms |
9572 KB |
Output is correct |
47 |
Correct |
2 ms |
7516 KB |
Output is correct |
48 |
Correct |
42 ms |
16232 KB |
Output is correct |
49 |
Correct |
57 ms |
16840 KB |
Output is correct |
50 |
Correct |
49 ms |
15564 KB |
Output is correct |
51 |
Correct |
37 ms |
15240 KB |
Output is correct |
52 |
Correct |
47 ms |
16600 KB |
Output is correct |
53 |
Correct |
45 ms |
14792 KB |
Output is correct |
54 |
Correct |
28 ms |
13472 KB |
Output is correct |
55 |
Correct |
54 ms |
19512 KB |
Output is correct |
56 |
Correct |
52 ms |
17816 KB |
Output is correct |
57 |
Correct |
56 ms |
18068 KB |
Output is correct |
58 |
Correct |
54 ms |
16340 KB |
Output is correct |
59 |
Correct |
46 ms |
15820 KB |
Output is correct |
60 |
Correct |
38 ms |
15688 KB |
Output is correct |
61 |
Correct |
36 ms |
15808 KB |
Output is correct |
62 |
Correct |
41 ms |
16068 KB |
Output is correct |
63 |
Correct |
40 ms |
16588 KB |
Output is correct |
64 |
Correct |
43 ms |
16072 KB |
Output is correct |
65 |
Correct |
37 ms |
16072 KB |
Output is correct |
66 |
Correct |
36 ms |
16588 KB |
Output is correct |
67 |
Correct |
28 ms |
13776 KB |
Output is correct |
68 |
Correct |
33 ms |
16012 KB |
Output is correct |
69 |
Correct |
38 ms |
16072 KB |
Output is correct |
70 |
Correct |
35 ms |
15548 KB |
Output is correct |
71 |
Correct |
26 ms |
13512 KB |
Output is correct |
72 |
Correct |
37 ms |
15820 KB |
Output is correct |
73 |
Correct |
55 ms |
17356 KB |
Output is correct |
74 |
Correct |
37 ms |
16584 KB |
Output is correct |
75 |
Correct |
48 ms |
16076 KB |
Output is correct |
76 |
Correct |
48 ms |
15392 KB |
Output is correct |
77 |
Correct |
42 ms |
15856 KB |
Output is correct |
78 |
Correct |
31 ms |
13000 KB |
Output is correct |
79 |
Correct |
32 ms |
15304 KB |
Output is correct |
80 |
Correct |
51 ms |
17104 KB |
Output is correct |
81 |
Correct |
47 ms |
16080 KB |
Output is correct |
82 |
Correct |
45 ms |
16824 KB |
Output is correct |