Submission #824738

# Submission time Handle Problem Language Result Execution time Memory
824738 2023-08-14T09:18:39 Z tch1cherin Village (BOI20_village) C++17
56 / 100
105 ms 20144 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 5, MAX_L = __lg(MAX_N) + 1;
vector<int> G[MAX_N];
int up[MAX_L][MAX_N];
int depth[MAX_N];
vector<int> ord;

void DFS(int u, int p) {
  depth[u] = (u == 0 ? 0 : depth[p] + 1);
  ord.push_back(u);
  up[0][u] = p;
  for (int i = 1; i < MAX_L; i++) {
    up[i][u] = up[i - 1][up[i - 1][u]];
  }
  for (int v : G[u]) {
    if (v != p) {
      DFS(v, u);
    }
  }
}

int lca(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  for (int i = MAX_L - 1; i >= 0; i--) {
    if (depth[u] - depth[v] >= (1 << i)) {
      u = up[i][u];
    }
  }
  if (u == v) {
    return u;
  }
  for (int i = MAX_L - 1; i >= 0; i--) {
    if (up[i][u] != up[i][v]) {
      u = up[i][u];
      v = up[i][v];
    }
  }
  return up[0][u];
}

int distance(int u, int v) {
  return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  DFS(0, 0);
  int64_t smallest = 0, largest = 0;
  queue<int> leaves;
  vector<int> value(N), degree(N);
  iota(value.begin(), value.end(), 0);
  for (int i = 0; i < N; i++) {
    degree[i] = (int)G[i].size();
    if (degree[i] == 1) {
      leaves.push(i);
    }
  }
  vector<bool> deleted(N, false);
  while (!leaves.empty()) {
    int u = leaves.front();
    leaves.pop();
    deleted[u] = true;
    for (int v : G[u]) {
      if (!deleted[v]) {
        degree[v]--;
        if (degree[v] == 1) {
          if (value[u] == u) {
            smallest += 2;
            swap(value[u], value[v]);
          }
          leaves.push(v);
        }
      }
    }
    if (value[u] == u) {
      smallest += 2;
      swap(value[u], value[G[u][0]]);
    }
  }
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    assert(value[i] != i);
    sum += distance(i, value[i]);
  }
  assert(sum == smallest);
  vector<int> large(N);
  for (int i = 0; i < N; i++) {
    largest += distance(ord[i], ord[(i + N / 2) % N]);
    large[ord[i]] = ord[(i + N / 2) % N];
  }
  cout << smallest << " " << largest << "\n";
  for (int i = 0; i < N; i++) {
    cout << 1 + value[i] << " \n"[i + 1 == N];
  }
  for (int i = 0; i < N; i++) {
    cout << 1 + large[i] << " \n"[i + 1 == N];
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2744 KB Output is correct
3 Correct 1 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 1 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 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Partially correct 1 ms 2772 KB Partially correct
4 Correct 2 ms 2772 KB Output is correct
5 Partially correct 2 ms 2772 KB Partially correct
6 Partially correct 2 ms 2772 KB Partially correct
7 Correct 2 ms 2900 KB Output is correct
8 Partially correct 2 ms 2772 KB Partially correct
9 Partially correct 2 ms 2772 KB Partially correct
10 Correct 2 ms 2772 KB Output is correct
11 Partially correct 2 ms 2772 KB Partially correct
12 Correct 2 ms 2780 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2784 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Partially correct 2 ms 2872 KB Partially correct
24 Partially correct 3 ms 2772 KB Partially correct
25 Correct 2 ms 2772 KB Output is correct
26 Partially correct 2 ms 2772 KB Partially correct
27 Correct 1 ms 2772 KB Output is correct
28 Partially correct 2 ms 2772 KB Partially correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2744 KB Output is correct
3 Correct 1 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 1 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 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Partially correct 1 ms 2772 KB Partially correct
21 Correct 2 ms 2772 KB Output is correct
22 Partially correct 2 ms 2772 KB Partially correct
23 Partially correct 2 ms 2772 KB Partially correct
24 Correct 2 ms 2900 KB Output is correct
25 Partially correct 2 ms 2772 KB Partially correct
26 Partially correct 2 ms 2772 KB Partially correct
27 Correct 2 ms 2772 KB Output is correct
28 Partially correct 2 ms 2772 KB Partially correct
29 Correct 2 ms 2780 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2784 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Partially correct 2 ms 2872 KB Partially correct
41 Partially correct 3 ms 2772 KB Partially correct
42 Correct 2 ms 2772 KB Output is correct
43 Partially correct 2 ms 2772 KB Partially correct
44 Correct 1 ms 2772 KB Output is correct
45 Partially correct 2 ms 2772 KB Partially correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 2 ms 2772 KB Output is correct
48 Partially correct 71 ms 14768 KB Partially correct
49 Partially correct 82 ms 15988 KB Partially correct
50 Partially correct 80 ms 15896 KB Partially correct
51 Partially correct 51 ms 13008 KB Partially correct
52 Partially correct 94 ms 15804 KB Partially correct
53 Partially correct 70 ms 14584 KB Partially correct
54 Correct 42 ms 11388 KB Output is correct
55 Correct 78 ms 20144 KB Output is correct
56 Partially correct 91 ms 17880 KB Partially correct
57 Partially correct 105 ms 17088 KB Partially correct
58 Partially correct 82 ms 16504 KB Partially correct
59 Partially correct 90 ms 15936 KB Partially correct
60 Correct 58 ms 16324 KB Output is correct
61 Correct 58 ms 16356 KB Output is correct
62 Correct 67 ms 16560 KB Output is correct
63 Correct 71 ms 15756 KB Output is correct
64 Correct 68 ms 16376 KB Output is correct
65 Correct 68 ms 16568 KB Output is correct
66 Correct 67 ms 15692 KB Output is correct
67 Correct 52 ms 12868 KB Output is correct
68 Correct 65 ms 14492 KB Output is correct
69 Correct 63 ms 16584 KB Output is correct
70 Correct 74 ms 15736 KB Output is correct
71 Correct 40 ms 12336 KB Output is correct
72 Correct 45 ms 13716 KB Output is correct
73 Correct 64 ms 16560 KB Output is correct
74 Correct 59 ms 15416 KB Output is correct
75 Partially correct 83 ms 15696 KB Partially correct
76 Partially correct 68 ms 15812 KB Partially correct
77 Correct 77 ms 15804 KB Output is correct
78 Correct 38 ms 11784 KB Output is correct
79 Partially correct 53 ms 13172 KB Partially correct
80 Partially correct 100 ms 15696 KB Partially correct
81 Correct 65 ms 15912 KB Output is correct
82 Correct 64 ms 16324 KB Output is correct