# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
254793 | model_code | Village (BOI20_village) | C++17 | 116 ms | 16956 KiB |
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;
using ll = long long;
const int MAXN = 100000;
int n;
struct Vertex{
vector<int> to;
int sz;
}v[MAXN + 1];
ll smallest_distance, largest_distance;
int place_smallest[MAXN + 1];
int place_largest[MAXN + 1];
int group[MAXN + 1];
int groupsz[MAXN + 1];
int order[MAXN + 1];
int cnt[MAXN + 1];
void dfs(int it, int p = 0)
{
v[it].sz = 1;
for (auto t : v[it].to)
{
if (t == p)
continue;
dfs(t, it);
v[it].sz += v[t].sz;
}
if (place_smallest[it] == it)
{
smallest_distance += 2;
if (p != 0)
{
swap(place_smallest[it], place_smallest[p]);
}
else
{
swap(place_smallest[it], place_smallest[v[it].to.front()]);
}
}
}
int get_centroid()
{
int it = 1;
while (true)
{
int nxt = 0;
for (auto t : v[it].to)
{
if (v[t].sz > v[it].sz)
continue;
if (v[t].sz * 2 > n)
{
nxt = t;
break;
}
}
if (nxt == 0)
break;
it = nxt;
}
return it;
}
int nextGroup = 0;
void dfsGroup(int it)
{
group[it] = nextGroup;
for (auto t : v[it].to)
{
if (group[t] != 0)
continue;
dfsGroup(t);
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%d %d", &a, &b);
v[a].to.push_back(b);
v[b].to.push_back(a);
}
for (int i = 1; i <= n; i++)
place_smallest[i] = i;
dfs(1);
for (int i = 1; i <= n; i++)
{
largest_distance += 2 * min(v[i].sz, n - v[i].sz);
}
int centroid = get_centroid();
group[centroid] = ++nextGroup;
for (auto t : v[centroid].to)
{
++nextGroup;
dfsGroup(t);
}
for (int i = 1; i <= n; i++)
{
groupsz[group[i]]++;
}
for (int i = 1; i <= nextGroup; i++)
{
groupsz[i] += groupsz[i - 1];
}
for (int i = 1; i <= n; i++)
{
order[groupsz[group[i] - 1] + cnt[group[i]]++] = i;
}
for (int i = 0, j = n / 2; i < n; i++, j++)
{
if (j >= n)
j = 0;
place_largest[order[i]] = order[j];
}
printf("%lld %lld\n", smallest_distance, largest_distance);
for (int i = 1; i <= n; i++)
printf("%d ", place_smallest[i]);
printf("\n");
for (int i = 1; i <= n; i++)
printf("%d ", place_largest[i]);
printf("\n");
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |