답안 #546663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546663 2022-04-08T02:12:16 Z Olympia Village (BOI20_village) C++17
100 / 100
220 ms 33752 KB
#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <type_traits>
#include <string>
#include <queue>
#include <map>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")


using namespace std;

vector<vector<int>> adj;
vector<int> ord;
int t = 0;
int c = 0;
vector<int64_t> pre, post, depth, sub;
vector<vector<int>> dp;

bool isAncestor(int u, int v) {
    return (pre[u] <= pre[v] && post[u] >= post[v]);
}

int lca(int u, int v) {
    if (isAncestor(u, v)) return u;
    if (isAncestor(v, u)) return v;
    for (int i = 19; i >= 0; i--) if (!isAncestor(dp[u][i], v))u = dp[u][i];
    return dp[u][0];
}

void find_sub(int curNode, int prevNode) {
    sub[curNode] = 1;
    for (int i: adj[curNode]) if (i != prevNode) find_sub(i, curNode), sub[curNode] += sub[i];
}

int get_centroid(int curNode, int prevNode) {
    for (int i: adj[curNode]) {
        if (i == prevNode) continue;
        if (sub[i] > (int) adj.size() / 2) return get_centroid(i, curNode);
    }
    return curNode;
}

void f(int curNode, int prevNode) {
    depth[curNode] = depth[prevNode] + 1;
    dp[curNode].resize(20);
    dp[curNode][0] = prevNode;
    for (int i = 1; i < 20; i++)dp[curNode][i] = dp[dp[curNode][i - 1]][i - 1];
    ord[curNode] = t++, pre[curNode] = c++;
    for (int i:adj[curNode]) if (i != prevNode)f(i, curNode);
    post[curNode] = c++;
}

vector<int> ans;
int tot = 0;
void dfs (int curNode, int prevNode) {
    for (int i: adj[curNode]) {
        if (i == prevNode) continue;
        dfs(i, curNode);
        if (ans[curNode] == -1 && ans[i] == -1) ans[curNode] = i, ans[i] = curNode, tot++;
        else if (ans[i] == -1) ans[i] = ans[curNode], ans[curNode] = i, tot++;
    }

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    adj.resize(n), depth.assign(n, false), dp.resize(n), pre.resize(n), post.resize(n), sub.resize(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--, adj[u].push_back(v), adj[v].push_back(u);
    }
    find_sub(0, -1);
    ord.resize(n), ans.assign(n, -1);
    int centroid = get_centroid(0, -1);
    f(centroid, centroid);
    dfs(0, -1);
    if (ans[0] == -1) ans[0] = ans[adj[0].back()], ans[adj[0].back()] = 0, tot++;
    int64_t al = 0;
    map<int, int> myMap;
    for (int i = 0; i < ord.size(); i++) myMap[ord[i]] = i;
    vector<int> ansr(n);
    for (int i = 0; i < n; i++) ansr[myMap[i]] = myMap[(i + n / 2 + n) % n];
    for (int i = 0; i < n; i++) al += depth[i] + depth[ansr[i]] - 2 * depth[lca(i, ansr[i])];
    cout << tot * 2 << " " << al << '\n';
    for (int i = 0; i < n; i++) cout << ans[i] + 1 << ' ';
    cout << '\n';
    for (int i: ansr) cout << i + 1 << ' ';


}

Compilation message

Village.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   18 | #pragma GCC optimization ("O3")
      | 
Village.cpp:19: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   19 | #pragma GCC optimization ("unroll-loops")
      | 
Village.cpp: In function 'int main()':
Village.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < ord.size(); i++) myMap[ord[i]] = i;
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 1 ms 596 KB Output is correct
38 Correct 2 ms 596 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 596 KB Output is correct
47 Correct 1 ms 596 KB Output is correct
48 Correct 142 ms 25336 KB Output is correct
49 Correct 177 ms 27852 KB Output is correct
50 Correct 157 ms 27760 KB Output is correct
51 Correct 116 ms 21708 KB Output is correct
52 Correct 154 ms 27524 KB Output is correct
53 Correct 136 ms 24864 KB Output is correct
54 Correct 87 ms 16792 KB Output is correct
55 Correct 220 ms 33752 KB Output is correct
56 Correct 203 ms 30564 KB Output is correct
57 Correct 198 ms 29224 KB Output is correct
58 Correct 186 ms 28760 KB Output is correct
59 Correct 163 ms 27980 KB Output is correct
60 Correct 146 ms 28100 KB Output is correct
61 Correct 150 ms 28236 KB Output is correct
62 Correct 157 ms 28236 KB Output is correct
63 Correct 141 ms 26456 KB Output is correct
64 Correct 146 ms 28096 KB Output is correct
65 Correct 155 ms 28236 KB Output is correct
66 Correct 133 ms 26476 KB Output is correct
67 Correct 102 ms 20816 KB Output is correct
68 Correct 118 ms 23992 KB Output is correct
69 Correct 141 ms 28280 KB Output is correct
70 Correct 129 ms 26512 KB Output is correct
71 Correct 94 ms 19776 KB Output is correct
72 Correct 106 ms 22332 KB Output is correct
73 Correct 140 ms 28300 KB Output is correct
74 Correct 126 ms 25932 KB Output is correct
75 Correct 154 ms 27724 KB Output is correct
76 Correct 148 ms 27688 KB Output is correct
77 Correct 135 ms 26616 KB Output is correct
78 Correct 89 ms 18788 KB Output is correct
79 Correct 108 ms 21740 KB Output is correct
80 Correct 156 ms 27672 KB Output is correct
81 Correct 159 ms 26700 KB Output is correct
82 Correct 150 ms 27900 KB Output is correct