Submission #1102067

# Submission time Handle Problem Language Result Execution time Memory
1102067 2024-10-17T11:30:14 Z crafticat Village (BOI20_village) C++17
25 / 100
700 ms 8784 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);
    sumDfs(cen,-1);
    int root = cen;
    ll sumV = 0;
    int last = -1;

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

        int meow = -1;
        for (auto x : g[cen]) {
            if (x == last) continue;
            if (meow == -1 || (sum[x] > sum[meow])) meow = x;
        }
        if (meow == -1) meow = last;
        last = meow;
        sum[last]--;

        calc(meow, cen,0);
        int sel = -1;

        vis[root] = true;

        FOR(i, 1, n + 1) {
            if (vis[i]) continue;
            if ((sel == -1 || dist[i] < dist[sel])) sel = i; // Must be connected to a vis node
        }

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

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

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

    cout << sumV << " " << sumV << "\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 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 1 ms 336 KB Partially correct
3 Correct 1 ms 336 KB Output is correct
4 Partially correct 1 ms 336 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 336 KB Partially correct
7 Correct 0 ms 336 KB Output is correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 0 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 336 KB Partially correct
12 Partially correct 1 ms 336 KB Partially correct
13 Partially correct 1 ms 336 KB Partially correct
14 Correct 1 ms 500 KB Output is correct
15 Partially correct 1 ms 336 KB Partially correct
16 Partially correct 1 ms 336 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 3 ms 504 KB Partially correct
3 Partially correct 3 ms 504 KB Partially correct
4 Partially correct 10 ms 336 KB Partially correct
5 Partially correct 13 ms 336 KB Partially correct
6 Partially correct 9 ms 336 KB Partially correct
7 Partially correct 16 ms 592 KB Partially correct
8 Partially correct 12 ms 568 KB Partially correct
9 Partially correct 9 ms 508 KB Partially correct
10 Partially correct 11 ms 572 KB Partially correct
11 Partially correct 11 ms 504 KB Partially correct
12 Correct 6 ms 552 KB Output is correct
13 Partially correct 6 ms 500 KB Partially correct
14 Partially correct 7 ms 336 KB Partially correct
15 Partially correct 6 ms 336 KB Partially correct
16 Partially correct 8 ms 336 KB Partially correct
17 Partially correct 7 ms 568 KB Partially correct
18 Partially correct 6 ms 508 KB Partially correct
19 Partially correct 6 ms 504 KB Partially correct
20 Partially correct 6 ms 564 KB Partially correct
21 Partially correct 6 ms 504 KB Partially correct
22 Partially correct 6 ms 336 KB Partially correct
23 Partially correct 7 ms 336 KB Partially correct
24 Partially correct 8 ms 336 KB Partially correct
25 Partially correct 4 ms 512 KB Partially correct
26 Partially correct 10 ms 556 KB Partially correct
27 Partially correct 3 ms 336 KB Partially correct
28 Partially correct 8 ms 564 KB Partially correct
29 Partially correct 6 ms 504 KB Partially correct
30 Partially correct 6 ms 336 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 1 ms 336 KB Partially correct
3 Correct 1 ms 336 KB Output is correct
4 Partially correct 1 ms 336 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 336 KB Partially correct
7 Correct 0 ms 336 KB Output is correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 0 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 336 KB Partially correct
12 Partially correct 1 ms 336 KB Partially correct
13 Partially correct 1 ms 336 KB Partially correct
14 Correct 1 ms 500 KB Output is correct
15 Partially correct 1 ms 336 KB Partially correct
16 Partially correct 1 ms 336 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
18 Partially correct 1 ms 336 KB Partially correct
19 Partially correct 3 ms 504 KB Partially correct
20 Partially correct 3 ms 504 KB Partially correct
21 Partially correct 10 ms 336 KB Partially correct
22 Partially correct 13 ms 336 KB Partially correct
23 Partially correct 9 ms 336 KB Partially correct
24 Partially correct 16 ms 592 KB Partially correct
25 Partially correct 12 ms 568 KB Partially correct
26 Partially correct 9 ms 508 KB Partially correct
27 Partially correct 11 ms 572 KB Partially correct
28 Partially correct 11 ms 504 KB Partially correct
29 Correct 6 ms 552 KB Output is correct
30 Partially correct 6 ms 500 KB Partially correct
31 Partially correct 7 ms 336 KB Partially correct
32 Partially correct 6 ms 336 KB Partially correct
33 Partially correct 8 ms 336 KB Partially correct
34 Partially correct 7 ms 568 KB Partially correct
35 Partially correct 6 ms 508 KB Partially correct
36 Partially correct 6 ms 504 KB Partially correct
37 Partially correct 6 ms 564 KB Partially correct
38 Partially correct 6 ms 504 KB Partially correct
39 Partially correct 6 ms 336 KB Partially correct
40 Partially correct 7 ms 336 KB Partially correct
41 Partially correct 8 ms 336 KB Partially correct
42 Partially correct 4 ms 512 KB Partially correct
43 Partially correct 10 ms 556 KB Partially correct
44 Partially correct 3 ms 336 KB Partially correct
45 Partially correct 8 ms 564 KB Partially correct
46 Partially correct 6 ms 504 KB Partially correct
47 Partially correct 6 ms 336 KB Partially correct
48 Execution timed out 1050 ms 8784 KB Time limit exceeded
49 Halted 0 ms 0 KB -