Submission #1102050

# Submission time Handle Problem Language Result Execution time Memory
1102050 2024-10-17T11:10:21 Z crafticat Village (BOI20_village) C++17
0 / 100
2 ms 336 KB
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
template<typename T>
using V = vector<T>;
using vi = V<ll>;
using vvi = V<vi>;
using pi = pair<ll,ll>;
using vpi = V<pi>;

#define F0R(i, n) for(ll i = 0; i < n;i++)
#define FOR(i,a, n) for(ll i = a; i < n;i++)
#define pb push_back

vvi g;
vi to;
vi dist;
vi sum;
V<bool> vis;
vi org;
ll n;

void calcOrg(int x, int p, int orgP) {
    org[x] = orgP;
    for (auto y : g[x]) {
        if (y == p) continue;
        calcOrg(y,x,orgP == -1 ? x : orgP);
    }
}

void calc(ll x, ll p, ll d = 0) {
    dist[x] = d;
    for (auto y : g[x]) {
        if (y == p) continue;
        calc(y,x, d + 1);
    }
}

void sumDfs(int x, int p) {
    sum[x] = 1;
    for (auto y : g[x]) {
        if (y == p) continue;
        sumDfs(y,x);
        sum[x] += sum[y];
    }
}

int findCentroid(int x, int p) {
    for (auto v : g[x]) {
        if (v == p) continue;
        if (sum[v] * 2 > n)
            return findCentroid(v,x);
    }
    return x;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    cin >> n;
    g.resize(n + 1);
    to.resize(n + 1);
    vis.resize(n + 1);
    dist.resize(n + 1);
    org.resize(n + 1);
    sum.resize(n + 1);

    F0R(i, n - 1) {
        ll a, b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }

    FOR(i, 1, n + 1) sum[i]++;
    sumDfs(1,-1);
    int cen = findCentroid(1,-1);
    int root = cen;
    ll sum = 0;

    while (true) {
        dist.clear();
        dist.resize(n + 1, 1e9);

        calc(root, -1,0);
        int sel = -1;

        vis[root] = true;

        FOR(i, 1, n + 1) {
            if (vis[i]) continue;
            bool conecvis = false;
            for (auto y : g[i])
                if (vis[y]) conecvis = true;
            if ((sel == -1 || dist[i] > dist[sel]) && conecvis) sel = i; // Must be connected to a vis node
        }

        if (sel == -1)
            break;
        calc(root, -1,0);

        to[root] = sel;
        root = sel;
        sum += dist[sel];
    }

    calc(root, -1, 0);
    to[root] = cen;
    sum += dist[cen];

    cout << sum << " " << sum << "\n";
    FOR(i, 1, n + 1) {
        cout << to[i] << " ";
    }
    cout << "\n";
    FOR(i, 1, n + 1) {
        cout << to[i] << " ";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -