This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, sz[maxn];
vector <ll> g[maxn];
vector <ll> ord;
ll up[maxn][22];
ll tin[maxn];
ll tout[maxn];
ll d[maxn], timer;
ll mx_par[maxn];
ll mn_par[maxn];
ll cnt[maxn];
void dfs_calc (ll v, ll pa)
{
sz[v] = 1;
for (ll to : g[v])
{
if (to != pa)
{
dfs_calc(to, v);
sz[v] += sz[to];
}
}
}
ll find_c (ll v, ll pa)
{
for (ll to : g[v])
{
if (to != pa)
{
if (sz[to] + sz[to] > n)
{
return find_c(to, v);
}
}
}
return v;
}
void dfs_ord (ll v, ll pa)
{
ord.pb(v);
d[v] = d[pa] + 1;
tin[v] = ++timer;
up[v][0] = pa;
for (ll j = 1; j <= 19; ++j)
{
up[v][j] = up[up[v][j - 1]][j - 1];
}
for (ll to : g[v])
{
if (to != pa)
{
dfs_ord(to, v);
}
}
tout[v] = timer;
}
bool is_anc (ll u, ll v)
{
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
ll lca (ll u, ll v)
{
if (is_anc(u, v)) return u;
if (is_anc(v, u)) return v;
for (ll j = 19; j >= 0; --j)
{
if (!is_anc(up[u][j], v))
{
u = up[u][j];
}
}
return up[u][0];
}
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1, u, v; i < n; ++i)
{
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
cnt[u]++, cnt[v]++;
}
ll ans_mn = 0, ans_mx = 0;
queue <int> q;
for (ll i = 1; i <= n; ++i)
{
if (cnt[i] == 1)
{
q.push(i);
}
mn_par[i] = i;
}
while (!q.empty())
{
ll v = q.front();
q.pop();
if (mn_par[v] == v)
{
ll qwe = g[v][0];
for (ll to : g[v])
{
if (cnt[to])
{
qwe = to;
break;
}
}
swap(mn_par[v], mn_par[qwe]);
ans_mn += 2;
}
for (ll to : g[v])
{
if (cnt[to])
{
cnt[v]--, cnt[to]--;
if (cnt[to] <= 1)
{
q.push(to);
}
}
}
}
dfs_calc (1, 1);
ll centroid = find_c(1, 1);
dfs_ord(centroid, centroid);
for (ll i = 0; i < sz(ord); ++i)
{
ll u = ord[i], v = ord[(i + (n / 2)) % n];
ans_mx += d[u] + d[v] - d[lca(u, v)] * 2;
mx_par[u] = v;
}
cout << ans_mn << ' ' << ans_mx << nl;
for (ll i = 1; i <= n; ++i)
{
cout << mn_par[i] << ' ';
}
cout << nl;
for (ll i = 1; i <= n; ++i)
{
cout << mx_par[i] << ' ';
}
cout << nl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |