#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi (for minimum version)
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<ll> adj[N];
vector<ll> subsiz(N);
vector<ll> order;
vector<ll> par(N);
void dfs1(ll u, ll p){
subsiz[u] = 1;
par[u] = p;
trav(v,adj[u]){
if(v == p) conts;
dfs1(v,u);
subsiz[u] += subsiz[v];
}
order.pb(u);
}
ll dfs2(ll u, ll p){
trav(v,adj[u]){
if(v == p) conts;
if(subsiz[v] > subsiz[1]/2){
return dfs2(v,u);
}
}
return u;
}
vector<ll> here[N];
ll cost2 = 0;
void dfs3(ll u, ll p, ll r, ll d){
here[r].pb(u);
cost2 += d;
trav(v,adj[u]){
if(v == p) conts;
dfs3(v,u,r,d+1);
}
}
void solve(int test_case)
{
ll n; cin >> n;
rep1(i,n-1){
ll u,v; cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
}
dfs1(1,-1);
vector<ll> ans1(n+5);
rep1(i,n) ans1[i] = i;
ll cost1 = 0;
trav(u,order){
if(u == 1) conts;
if(ans1[u] != u) conts;
ll p = par[u];
swap(ans1[u],ans1[p]);
cost1 += 2;
}
if(ans1[1] == 1){
ll v = adj[1][0];
swap(ans1[1],ans1[v]);
cost1 += 2;
}
ll c = dfs2(1,-1);
trav(v,adj[c]){
dfs3(v,c,v,1);
}
cost2 *= 2;
priority_queue<pll> pq;
trav(v,adj[c]){
pq.push({sz(here[v]),v});
}
vector<ll> ans2(n+5,-1);
rep1(iter,(n-2)/2){
auto [s1,v1] = pq.top();
pq.pop();
auto [s2,v2] = pq.top();
pq.pop();
ll x = here[v1].back();
ll y = here[v2].back();
here[v1].pop_back();
here[v2].pop_back();
ans2[x] = y, ans2[y] = x;
pq.push({s1-1,v1});
pq.push({s2-1,v2});
}
vector<ll> remain;
rep1(i,n){
if(ans2[i] == -1){
remain.pb(i);
}
}
rep(i,sz(remain)){
ans2[remain[i]] = remain[(i+1)%sz(remain)];
}
cout << cost1 << " " << cost2 << endl;
rep1(i,n) cout << ans1[i] << " ";
cout << endl;
rep1(i,n) cout << ans2[i] << " ";
cout << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6704 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6556 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6900 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6744 KB |
Output is correct |
8 |
Correct |
4 ms |
6748 KB |
Output is correct |
9 |
Correct |
5 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
3 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6944 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6740 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
5 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6704 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6556 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6900 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6744 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6736 KB |
Output is correct |
24 |
Correct |
3 ms |
6744 KB |
Output is correct |
25 |
Correct |
4 ms |
6748 KB |
Output is correct |
26 |
Correct |
5 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
3 ms |
6748 KB |
Output is correct |
33 |
Correct |
2 ms |
6944 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
3 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
2 ms |
6740 KB |
Output is correct |
44 |
Correct |
2 ms |
6748 KB |
Output is correct |
45 |
Correct |
5 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
44 ms |
14544 KB |
Output is correct |
49 |
Correct |
49 ms |
15252 KB |
Output is correct |
50 |
Correct |
51 ms |
15300 KB |
Output is correct |
51 |
Correct |
38 ms |
13252 KB |
Output is correct |
52 |
Correct |
52 ms |
15032 KB |
Output is correct |
53 |
Correct |
57 ms |
14280 KB |
Output is correct |
54 |
Correct |
27 ms |
12752 KB |
Output is correct |
55 |
Correct |
56 ms |
18996 KB |
Output is correct |
56 |
Correct |
52 ms |
17352 KB |
Output is correct |
57 |
Correct |
52 ms |
16332 KB |
Output is correct |
58 |
Correct |
53 ms |
15916 KB |
Output is correct |
59 |
Correct |
52 ms |
15304 KB |
Output is correct |
60 |
Correct |
66 ms |
18624 KB |
Output is correct |
61 |
Correct |
51 ms |
16316 KB |
Output is correct |
62 |
Correct |
54 ms |
15560 KB |
Output is correct |
63 |
Correct |
44 ms |
15044 KB |
Output is correct |
64 |
Correct |
53 ms |
15304 KB |
Output is correct |
65 |
Correct |
43 ms |
15804 KB |
Output is correct |
66 |
Correct |
43 ms |
14788 KB |
Output is correct |
67 |
Correct |
35 ms |
13620 KB |
Output is correct |
68 |
Correct |
39 ms |
14024 KB |
Output is correct |
69 |
Correct |
45 ms |
15548 KB |
Output is correct |
70 |
Correct |
41 ms |
14964 KB |
Output is correct |
71 |
Correct |
32 ms |
12744 KB |
Output is correct |
72 |
Correct |
34 ms |
13516 KB |
Output is correct |
73 |
Correct |
46 ms |
15804 KB |
Output is correct |
74 |
Correct |
42 ms |
14792 KB |
Output is correct |
75 |
Correct |
48 ms |
15612 KB |
Output is correct |
76 |
Correct |
45 ms |
14796 KB |
Output is correct |
77 |
Correct |
47 ms |
15312 KB |
Output is correct |
78 |
Correct |
30 ms |
12496 KB |
Output is correct |
79 |
Correct |
34 ms |
13516 KB |
Output is correct |
80 |
Correct |
51 ms |
15448 KB |
Output is correct |
81 |
Correct |
43 ms |
15312 KB |
Output is correct |
82 |
Correct |
46 ms |
15048 KB |
Output is correct |