제출 #1351335

#제출 시각아이디문제언어결과실행 시간메모리
1351335Joshua_AnderssonVillage (BOI20_village)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;

#define rep(i,n) for(ll i =0; i <(n); i++)

ll gather(int u, int p, int d, vvi& adj) {
    ll ret = d;

    for (int e : adj[u]) {
        if (e==p) continue;

        ret += gather(e,u,d+1,adj);
    }

    return ret;
}

ll subtree_size(int u, int p, vvi& adj) {
    int ret = 1;

    for (int e : adj[u]) {
        if (e==p) continue;

        ret += subtree_size(e,u,adj);
    }

    return ret;
}

int main() {
    int n;
    cin >> n;

    vvi adj(n);
    rep(i,n-1) {
        int a,b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    ll centroid = -1;
    ll best = LLONG_MAX;
    rep(i,n) {
        
        ll largest_subtree = 0;
        for (int e : adj[i]) {
            largest_subtree = max(largest_subtree, subtree_size(e,i,adj));
            // cout << "gather(" << e << "," << i << ")=" << gather(e,i,1,adj) << '\n';
        }

        if (largest_subtree < best) {
            best = largest_subtree;
            centroid = i;
        }
    }

    ll ans = 0;
    for (int e : adj[centroid]) {
        ans += 2*gather(e,centroid,1,adj);
    }

    cout << ans << " " << ans << '\n';
    rep(i,n) cout << "1 ";
    cout << '\n';

    rep(i,n) cout << "1 ";
    cout << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...