답안 #567882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
567882 2022-05-24T09:51:07 Z stevancv Village (BOI20_village) C++14
100 / 100
112 ms 16548 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];
int sz[N], dep[N], n;
vector<int> order;
ll sol;
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;
        }
    }
}
void 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]);
        ans[i] = 0;
    }
    Dfsb(1, 0);
    int cen = Dfs1b(1, 0);
    Dfs2b(cen, 0);
    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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2580 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2596 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2744 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 3 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2580 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 3 ms 2596 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2744 KB Output is correct
24 Correct 3 ms 2772 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 3 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 59 ms 8552 KB Output is correct
49 Correct 66 ms 9100 KB Output is correct
50 Correct 99 ms 9260 KB Output is correct
51 Correct 44 ms 7748 KB Output is correct
52 Correct 59 ms 9148 KB Output is correct
53 Correct 52 ms 8416 KB Output is correct
54 Correct 45 ms 9444 KB Output is correct
55 Correct 87 ms 16548 KB Output is correct
56 Correct 112 ms 12560 KB Output is correct
57 Correct 73 ms 11336 KB Output is correct
58 Correct 110 ms 10292 KB Output is correct
59 Correct 59 ms 9196 KB Output is correct
60 Correct 60 ms 9356 KB Output is correct
61 Correct 47 ms 9468 KB Output is correct
62 Correct 61 ms 9532 KB Output is correct
63 Correct 48 ms 9180 KB Output is correct
64 Correct 67 ms 9536 KB Output is correct
65 Correct 65 ms 9556 KB Output is correct
66 Correct 44 ms 9108 KB Output is correct
67 Correct 35 ms 7756 KB Output is correct
68 Correct 58 ms 8524 KB Output is correct
69 Correct 51 ms 9796 KB Output is correct
70 Correct 57 ms 9128 KB Output is correct
71 Correct 35 ms 7516 KB Output is correct
72 Correct 39 ms 8180 KB Output is correct
73 Correct 71 ms 9604 KB Output is correct
74 Correct 53 ms 9068 KB Output is correct
75 Correct 72 ms 8972 KB Output is correct
76 Correct 66 ms 8952 KB Output is correct
77 Correct 77 ms 9224 KB Output is correct
78 Correct 48 ms 7064 KB Output is correct
79 Correct 45 ms 7748 KB Output is correct
80 Correct 84 ms 8964 KB Output is correct
81 Correct 66 ms 9368 KB Output is correct
82 Correct 65 ms 9424 KB Output is correct