답안 #1102031

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102031 2024-10-17T10:27:21 Z crafticat Village (BOI20_village) C++17
6 / 100
700 ms 512 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;

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);
    }
}

int solve(int start, ll n) {
    ll root = start;
    ll sum = 0;
    V<bool> vis(n + 1);

    while (true) {
        calc(root, -1, 0);
        ll sel = -1;

        FOR(i,1,n + 1) {
            if (vis[i]) continue;
            if (sel == -1 || dist[i] > dist[sel]) sel = i;
        }

        if (sel == -1) break;
        to[root] = sel;
        vis[root] = true;
        root = sel;
        sum += dist[sel];
    }

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

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

    ll n; cin >> n;
    g.resize(n + 1);
    to.resize(n + 1);
    dist.resize(n + 1);

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

    ll bestRoot = 1, bestSum = 0;
    FOR(i,1, n + 1) {
        int y = solve(i, n);
        if (y > bestSum) {
            bestSum = y;
            bestRoot = i;
        }

    }
    int sum = solve(bestRoot,n);

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

    return 0;
}
# 결과 실행 시간 메모리 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 1 ms 336 KB Output is correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 512 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 336 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
# 결과 실행 시간 메모리 Grader output
1 Partially correct 103 ms 336 KB Partially correct
2 Execution timed out 727 ms 336 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 ms 336 KB Output is correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 512 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 336 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 103 ms 336 KB Partially correct
19 Execution timed out 727 ms 336 KB Time limit exceeded
20 Halted 0 ms 0 KB -