답안 #567864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
567864 2022-05-24T09:38:17 Z stevancv Village (BOI20_village) C++14
0 / 100
6 ms 5204 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
using namespace std;
const int N = 1e5 + 2;
const ll mod = 1e9 + 7;
vector<int> adj[N];
int ans[N], sol;
int sz[N], dep[N], n;
vector<int> order;
void Dfs(int s, int e) {
    for (auto u : adj[s]) {
        if (u == e) continue;
        Dfs(u, s);
        if (ans[u] == u) {
            swap(ans[u], ans[s]);
            sol += 2;
        }
    }
}
int Dfsb(int s, int e) {
    sz[s] = 1;
    for (auto u : adj[s]) {
        if (u == e) continue;
        Dfsb(u, s);
        sz[s] += sz[u];
    }
}
int Dfs1b(int s, int e) {
    for (auto u : adj[s]) {
        if (u == e) continue;
        if (2 * sz[u] > n) return Dfs1b(u, s);
    }
    return s;
}
void Dfs2b(int s, int e) {
    order.push_back(s);
    for (auto u : adj[s]) {
        if (u == e) continue;
        dep[u] = dep[s] + 1;
        Dfs2b(u, s);
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) ans[i] = i;
    Dfs(1, 0);
    if (ans[1] == 1) {
        swap(ans[1], ans[adj[1][0]]);
        sol += 2;
    }
    ll solmn = sol;
    vector<int> ansmn;
    for (int i = 1; i <= n; i++) ansmn.push_back(ans[i]);
    Dfsb(1, 0);
    int cen = Dfs1b(1, 0);
    Dfs2b(cen, 0);
    ll sol = 0;
    for (int i = 0; i < n; i++) {
        int x = order[i];
        int y = order[(i + n / 2) % n];
        ans[x] = y;
        sol += dep[x] + dep[y];
    }
    cout << solmn << sp << sol << en;
    for (int i : ansmn) cout << i << sp;
    cout << en;
    for (int i = 1; i <= n; i++) cout << ans[i] << sp;
    cout << en;
    return 0;
}

Compilation message

Village.cpp: In function 'int Dfsb(int, int)':
Village.cpp:30:1: warning: no return statement in function returning non-void [-Wreturn-type]
   30 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -