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>
using namespace std;
#define int long long
const int nx=1e5+5;
int n, u, v, sz[nx], rt, res, mn[nx], mx[nx], x, lst, l, r, st, cur;
vector<int> d[nx], g[nx];
int dfssz(int u, int p)
{
sz[u]=1;
for (auto v:d[u]) if (v!=p) sz[u]+=dfssz(v, u);
return sz[u];
}
int findcentroid(int u, int p, int rtsz)
{
for (auto v:d[u]) if (v!=p&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
return u;
}
void dfsadd(int u, int p, int t, int lvl)
{
g[t].push_back(u);
res+=2*(lvl+1);
for (auto v:d[u]) if (v!=p) dfsadd(v, u, t, lvl+1);
}
int32_t main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
rt=findcentroid(1, 1, dfssz(1, 1));
dfssz(rt, rt);
vector<pair<int, int>> s;
for (auto v:d[rt]) s.push_back({-sz[v], v});
sort(s.begin(), s.end());
for (int i=0; i<s.size(); i++) dfsadd(s[i].second, rt, i, 0);
x=s.size();
g[x].push_back(rt);
l=0, r=1, lst=1, st=cur=g[0].back(), g[0].pop_back();
//cout<<"x "<<x<<'\n';
//for (int i=0; i<=x; i++) cout<<i<<' '<<g[i].size()<<'\n';
while (1)
{
//cout<<"debug "<<l<<' '<<r<<' '<<cur<<'\n';
if (g[r].empty())
{
if (lst==x)
{
mx[cur]=st;
break;
}
else r=++lst;
}
//cout<<"debugg "<<l<<' '<<r<<' '<<cur<<'\n';
mx[cur]=g[r].back();
cur=g[r].back();
g[r].pop_back();
swap(l, r);
}
cout<<0<<' '<<res<<'\n';
for (int i=1; i<=n; i++) cout<<1<<' ';
cout<<'\n';
for (int i=1; i<=n; i++) cout<<mx[i]<<' ';
}
Compilation message (stderr)
Village.cpp: In function 'int32_t main()':
Village.cpp:42:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i=0; i<s.size(); i++) dfsadd(s[i].second, rt, i, 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... |