답안 #343075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343075 2021-01-03T11:56:38 Z mjhmjh1104 Village (BOI20_village) C++14
100 / 100
200 ms 30180 KB
#include <queue>
#include <cstdio>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;

const int MAX = 100006;

int n, maxDepth[MAX], depth[MAX], childs[MAX], sz[MAX];
vector<int> adj[MAX], c[MAX];
int nextShort[MAX], e[MAX], resShort;
int nextLong[MAX];
long long resLong;
bool visited[MAX];

vector<int> tmp[MAX];
int tok[MAX];

int dfs(int x, int prev = -1) {
    sz[x] = 1;
    for (auto &i: adj[x]) if (i != prev) {
        c[x].push_back(i);
        sz[x] += dfs(i, x);
    }
    return sz[x];
}

int centroid(int x, int prev = -1) {
    for (auto &i: adj[x]) if (i != prev && sz[i] > n / 2) return centroid(i, x);
    return x;
}

void dfsShort(int x) {
    e[x] = x;
    vector<int> zero;
    for (auto &i: c[x]) {
        if (!childs[i]) zero.push_back(i), e[x] = i;
        else {
            dfsShort(i);
            if (childs[i]) childs[x]--;
            else zero.push_back(i), e[x] = i;
        }
    }
    if (!childs[x]) return;
    if (zero.empty()) return;
    nextShort[x] = zero[0];
    for (int i = 0; i < (int)zero.size(); i++) {
        if (i != (int)zero.size() - 1) nextShort[zero[i]] = zero[i + 1];
        else nextShort[zero[i]] = x;
        resShort += 2;
    }
}

void depthFrom(int x, int prev = -1) {
    maxDepth[x] = depth[x];
    for (auto &i: adj[x]) if (i != prev && !visited[i]) {
        depth[i] = depth[x] + 1;
        depthFrom(i, x);
        maxDepth[x] = max(maxDepth[x], maxDepth[i]);
    }
}

pair<int, int> farthest(int x, int prev = -1) {
    for (auto &i: adj[x]) if (i != prev && !visited[i]) {
        if (maxDepth[i] == maxDepth[x]) {
            pair<int, int> t = farthest(i, x);
            return { t.first, t.second + 1 };
        }
    }
    return { x, 0 };
}

void fl(int x, int prev, vector<int> &f) {
    f.push_back(x);
    for (auto &i: adj[x]) if (i != prev) fl(i, x, f);
}

int main() {
    scanf("%d", &n);
    fill(nextShort, nextShort + n, -1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0);
    int ct = centroid(0);
    for (int i = 0; i < n; i++) childs[i] = (int)c[i].size();
    dfsShort(0);
    if (nextShort[0] == -1) {
        nextShort[0] = c[0][0];
        nextShort[e[c[0][0]]] = 0;
        resShort += 2;
    }
    for (int i = 0; i < n; i++) c[i].clear();
    dfs(ct);
    depth[ct] = 0;
    depthFrom(ct);

    vector<pair<int, int>> v;
    for (auto &i: adj[ct]) v.push_back({ sz[i], i });
    sort(v.begin(), v.end());
    swap(v.front(), v.back());
    int remainSum = 0;
    for (int i = 1; i < (int)v.size(); i++) remainSum += v[i].first;

    queue<pair<int, int>> q;

    vector<pair<int, int>> u;
    for (auto &i: adj[ct]) if (i != v[0].second) u.push_back({ i, sz[i] });

    vector<int> res;
    for (auto &i: u) q.push(i);

    while (!q.empty()) {
        auto t = q.front(); q.pop();
        if (!t.second) continue;
        t.second--;
        res.push_back(t.first);
        q.push(t);
    }

    int k = 0;
    vector<int> r;
    while (remainSum > v[0].first) {
        r.push_back(res[k++]);
        remainSum--;
    }
    while (v[0].first) {
        r.push_back(v[0].second);
        v[0].first--;
        if (remainSum) r.push_back(res[k++]), remainSum--;
    }
    for (int i = 0; i < (int)v.size(); i++) fl(v[i].second, ct, tmp[v[i].second]);

    vector<int> fin;
    for (auto &i: r) {
        resLong += depth[tmp[i][tok[i]]] * 2;
        fin.push_back(tmp[i][tok[i]++]);
    }
    for (int i = 0; i < n - 2; i++) nextLong[fin[i]] = fin[i + 1];
    nextLong[fin[n - 2]] = ct;
    nextLong[ct] = fin[0];

    printf("%d %lld\n", resShort, resLong);
    for (int i = 0; i < n; i++) printf("%d ", nextShort[i] + 1);
    puts("");
    for (int i = 0; i < n; i++) printf("%d ", nextLong[i] + 1);
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Village.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7532 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 5 ms 7404 KB Output is correct
17 Correct 5 ms 7404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7532 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 6 ms 7532 KB Output is correct
10 Correct 6 ms 7532 KB Output is correct
11 Correct 6 ms 7532 KB Output is correct
12 Correct 6 ms 7660 KB Output is correct
13 Correct 6 ms 7532 KB Output is correct
14 Correct 6 ms 7532 KB Output is correct
15 Correct 6 ms 7552 KB Output is correct
16 Correct 7 ms 7532 KB Output is correct
17 Correct 6 ms 7532 KB Output is correct
18 Correct 7 ms 7532 KB Output is correct
19 Correct 6 ms 7532 KB Output is correct
20 Correct 5 ms 7532 KB Output is correct
21 Correct 6 ms 7532 KB Output is correct
22 Correct 5 ms 7532 KB Output is correct
23 Correct 6 ms 7532 KB Output is correct
24 Correct 5 ms 7532 KB Output is correct
25 Correct 7 ms 7404 KB Output is correct
26 Correct 6 ms 7532 KB Output is correct
27 Correct 7 ms 7404 KB Output is correct
28 Correct 6 ms 7532 KB Output is correct
29 Correct 6 ms 7532 KB Output is correct
30 Correct 6 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7532 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 5 ms 7404 KB Output is correct
17 Correct 5 ms 7404 KB Output is correct
18 Correct 5 ms 7404 KB Output is correct
19 Correct 5 ms 7404 KB Output is correct
20 Correct 5 ms 7404 KB Output is correct
21 Correct 6 ms 7532 KB Output is correct
22 Correct 6 ms 7532 KB Output is correct
23 Correct 6 ms 7532 KB Output is correct
24 Correct 6 ms 7532 KB Output is correct
25 Correct 6 ms 7532 KB Output is correct
26 Correct 6 ms 7532 KB Output is correct
27 Correct 6 ms 7532 KB Output is correct
28 Correct 6 ms 7532 KB Output is correct
29 Correct 6 ms 7660 KB Output is correct
30 Correct 6 ms 7532 KB Output is correct
31 Correct 6 ms 7532 KB Output is correct
32 Correct 6 ms 7552 KB Output is correct
33 Correct 7 ms 7532 KB Output is correct
34 Correct 6 ms 7532 KB Output is correct
35 Correct 7 ms 7532 KB Output is correct
36 Correct 6 ms 7532 KB Output is correct
37 Correct 5 ms 7532 KB Output is correct
38 Correct 6 ms 7532 KB Output is correct
39 Correct 5 ms 7532 KB Output is correct
40 Correct 6 ms 7532 KB Output is correct
41 Correct 5 ms 7532 KB Output is correct
42 Correct 7 ms 7404 KB Output is correct
43 Correct 6 ms 7532 KB Output is correct
44 Correct 7 ms 7404 KB Output is correct
45 Correct 6 ms 7532 KB Output is correct
46 Correct 6 ms 7532 KB Output is correct
47 Correct 6 ms 7532 KB Output is correct
48 Correct 126 ms 17384 KB Output is correct
49 Correct 151 ms 18276 KB Output is correct
50 Correct 149 ms 18408 KB Output is correct
51 Correct 113 ms 16016 KB Output is correct
52 Correct 147 ms 18156 KB Output is correct
53 Correct 121 ms 17124 KB Output is correct
54 Correct 76 ms 18408 KB Output is correct
55 Correct 200 ms 30180 KB Output is correct
56 Correct 184 ms 23652 KB Output is correct
57 Correct 166 ms 21860 KB Output is correct
58 Correct 176 ms 20260 KB Output is correct
59 Correct 137 ms 18156 KB Output is correct
60 Correct 119 ms 21996 KB Output is correct
61 Correct 104 ms 17992 KB Output is correct
62 Correct 100 ms 17624 KB Output is correct
63 Correct 91 ms 17132 KB Output is correct
64 Correct 120 ms 17568 KB Output is correct
65 Correct 99 ms 18024 KB Output is correct
66 Correct 86 ms 17380 KB Output is correct
67 Correct 63 ms 15152 KB Output is correct
68 Correct 90 ms 16168 KB Output is correct
69 Correct 95 ms 18024 KB Output is correct
70 Correct 87 ms 17392 KB Output is correct
71 Correct 61 ms 14632 KB Output is correct
72 Correct 74 ms 15528 KB Output is correct
73 Correct 103 ms 18020 KB Output is correct
74 Correct 86 ms 17124 KB Output is correct
75 Correct 142 ms 17896 KB Output is correct
76 Correct 161 ms 17384 KB Output is correct
77 Correct 119 ms 17156 KB Output is correct
78 Correct 74 ms 13988 KB Output is correct
79 Correct 115 ms 15012 KB Output is correct
80 Correct 139 ms 17896 KB Output is correct
81 Correct 107 ms 17356 KB Output is correct
82 Correct 105 ms 17192 KB Output is correct