Submission #254863

#TimeUsernameProblemLanguageResultExecution timeMemory
254863dolphingarlicVillage (BOI20_village)C++14
100 / 100
140 ms16952 KiB
#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)

Village.cpp: In function 'int main()':
Village.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
Village.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...