# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
254793 | model_code | Village (BOI20_village) | C++17 | 116 ms | 16956 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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");
}
컴파일 시 표준 에러 (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... |