Submission #853309

#TimeUsernameProblemLanguageResultExecution timeMemory
853309overwatch9Village (BOI20_village)C++17
0 / 100
1 ms2908 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 1;
vector <int> adj[maxn];
vector <int> min_path_nodes;
pair <int, bool> dfs(int s, int p, int d) {
    pair <int, bool> ans = {0, false};
    for (auto i : adj[s]) {
        if (i == p)
            continue;
        auto res = dfs(i, s, d+1);
        if (!res.second) {
            ans.second = true;
            ans.first += 2;
            swap(min_path_nodes[s], min_path_nodes[i]);
        }
        ans.first += res.first;
    }
    return ans;
}
int sz[maxn];
vector <int> ord;
void dfs2(int s, int p) {
    ord.push_back(s);
    sz[s] = 1;
    for (auto i : adj[s]) {
        if (i == p)
            continue;
        dfs2(i, s);
        sz[s] += sz[i];
    }
}
int main() {
    int n;
    cin >> n;
    vector <pair <int, int>> edges;
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        edges.push_back({a, b});
    }
    vector <int> nums(n);
    for (int i = 0; i < n; i++)
        nums[i] = i;
    min_path_nodes = nums;
    auto res = dfs(0, 0, 0);
    int min_path_len = res.first;
    if (res.second == false) {
        min_path_len += 2;
        swap(min_path_nodes[0], min_path_nodes[adj[0][0]]);
    }
    dfs2(0, 0);
    vector <int> max_path_nodes = nums;
    ll max_path_len = 0;   
    for (int i = 0; i < n-1; i++) {
        int a = edges[i].first, b = edges[i].second;
        if (sz[a] > sz[b])
            max_path_len += min(sz[0] - sz[b], sz[b]) * 2;
        else
            max_path_len += min(sz[0] - sz[a], sz[a]) * 2;
    }
    for (int i = 0; i < n; i++) {
        int u = ord[i], v = ord[(i + n / 2) % n];
        max_path_nodes[u] = v;
    }
    cout << max_path_len << '\n';
    for (int i = 0; i < n; i++)
        cout << max_path_nodes[i] + 1 << ' ';
    cout << '\n';

    cout << min_path_len << '\n';
    for (auto i : min_path_nodes)
        cout << i+1 << ' ';
    cout << '\n';

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...