답안 #261662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261662 2020-08-12T00:09:05 Z rama_pang Village (BOI20_village) C++14
100 / 100
188 ms 26060 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;

namespace minimum {

int N;
vector<int> adj[MAXN];
int ans[MAXN];
int dp[MAXN];
int score = 0;

void Dfs(int u, int p) {
  for (auto v : adj[u]) if (v != p) {
    Dfs(v, u);
    if (ans[v] == 0 && ans[u] == 0) {
      ans[u] = v;
      ans[v] = u;
      score += 2;
    } else if (ans[v] == 0) {
      ans[v] = ans[u];
      ans[u] = v;
      score += 2;
    }
  }

  if (ans[u] == 0 && p == 0) {
    int ch = ans[adj[u][0]];
    ans[u] = ch;
    ans[adj[u][0]] = u;
    score += 2;
  }
}

pair<long long, vector<int>> SolveMinimum(int N, vector<int> U, vector<int> V) {
  minimum::N = N;
  for (int i = 0; i + 1 < N; i++) {
    int u = U[i], v = V[i];
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }
  Dfs(1, 0);
  
  vector<int> res;
  for (int i = 1; i <= N; i++) {
    assert(ans[i] != i);
    res.emplace_back(ans[i]);
  }
  return {score, res};
}

} /// namespace minimum

namespace maximum {

int N;
vector<int> adj[MAXN];

int ans[MAXN];
int identity[MAXN];
long long score = 0;

int sz[MAXN];
int par[MAXN];

void Calc(int u, int p) {
  sz[u] = 1;
  for (auto v : adj[u]) if (v != p) {
    Calc(v, u);
    sz[u] += sz[v];
    score += 2 * min(sz[v], N - sz[v]);
  }
}

void Dfs(int u, int p) {
  par[u] = p, sz[u] = 1;
  for (auto v : adj[u]) if (v != p) {
    Dfs(v, u);
    sz[u] += sz[v];
  }
}

int Centroid(int u) {
  Dfs(u, 0);
  int total_sz = sz[u];
  while (true) {
    pair<int, int> mx = {-1, -1};
    for (auto v : adj[u]) if (v != par[u]) {
      mx = max(mx, {sz[v], v});
    }
    if (mx.first * 2 <= total_sz) {
      return u;
    }
    u = mx.second;
  }
  return -1;
}

void GetNodes(int u, int p, vector<int> &nodes) {
  nodes.emplace_back(u);
  for (auto v : adj[u]) if (v != p) {
    GetNodes(v, u, nodes);
  }
}

void Solve() {
  int u = Centroid(1);
  Dfs(u, 0);
  vector<int> nodes = {u};
  pair<int, int> mx = {1, u};
  for (auto v : adj[u]) {
    mx = max(mx, {sz[v], v});
    GetNodes(v, u, nodes);
  }
  vector<int> old_id(nodes.size());
  for (int i = 0; i < (int) nodes.size(); i++) {
    old_id[i] = identity[nodes[i]];
  }
  for (int i = 0; i < (int) nodes.size(); i++) {
    identity[nodes[i]] = old_id[(i - mx.first + (int) nodes.size()) % nodes.size()];
  }
}

pair<long long, vector<int>> SolveMaximum(int N, vector<int> U, vector<int> V) {
  maximum::N = N;
  iota(identity + 1, identity + N + 1, 1);
  for (int i = 0; i + 1 < N; i++) {
    int u = U[i], v = V[i];
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }
  Calc(1, 0);
  Solve();
  for (int i = 1; i <= N; i++) {
    ans[identity[i]] = i;
  }

  vector<int> res;
  for (int i = 1; i <= N; i++) {
    assert(ans[i] != i);
    res.emplace_back(ans[i]);
  }
  return {score, res};
}

} /// namespace maximum

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N;
  cin >> N;
  vector<int> U(N - 1), V(N - 1);
  for (int i = 0; i + 1 < N; i++) {
    cin >> U[i] >> V[i];
  }

  long long score_min, score_max;
  vector<int> ans_min, ans_max;
  tie(score_min, ans_min) = minimum::SolveMinimum(N, U, V);
  tie(score_max, ans_max) = maximum::SolveMaximum(N, U, V);
  
  cout << score_min << " " << score_max << "\n";
  for (int i = 0; i < N; i++) {
    cout << ans_min[i] << " \n"[i + 1 == N];
  }
  for (int i = 0; i < N; i++) {
    cout << ans_max[i] << " \n"[i + 1 == N];
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 3 ms 5120 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 5120 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5120 KB Output is correct
2 Correct 5 ms 5100 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 4 ms 5248 KB Output is correct
9 Correct 4 ms 5248 KB Output is correct
10 Correct 4 ms 5248 KB Output is correct
11 Correct 5 ms 5236 KB Output is correct
12 Correct 4 ms 5248 KB Output is correct
13 Correct 4 ms 5248 KB Output is correct
14 Correct 4 ms 5248 KB Output is correct
15 Correct 4 ms 5248 KB Output is correct
16 Correct 4 ms 5248 KB Output is correct
17 Correct 4 ms 5248 KB Output is correct
18 Correct 4 ms 5248 KB Output is correct
19 Correct 4 ms 5248 KB Output is correct
20 Correct 4 ms 5248 KB Output is correct
21 Correct 4 ms 5248 KB Output is correct
22 Correct 4 ms 5248 KB Output is correct
23 Correct 5 ms 5248 KB Output is correct
24 Correct 5 ms 5248 KB Output is correct
25 Correct 4 ms 5120 KB Output is correct
26 Correct 4 ms 5248 KB Output is correct
27 Correct 4 ms 5120 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 6 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 3 ms 5120 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 5120 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 5 ms 5100 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 4 ms 5248 KB Output is correct
22 Correct 4 ms 5248 KB Output is correct
23 Correct 4 ms 5248 KB Output is correct
24 Correct 5 ms 5248 KB Output is correct
25 Correct 4 ms 5248 KB Output is correct
26 Correct 4 ms 5248 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 5 ms 5236 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5248 KB Output is correct
31 Correct 4 ms 5248 KB Output is correct
32 Correct 4 ms 5248 KB Output is correct
33 Correct 4 ms 5248 KB Output is correct
34 Correct 4 ms 5248 KB Output is correct
35 Correct 4 ms 5248 KB Output is correct
36 Correct 4 ms 5248 KB Output is correct
37 Correct 4 ms 5248 KB Output is correct
38 Correct 4 ms 5248 KB Output is correct
39 Correct 4 ms 5248 KB Output is correct
40 Correct 5 ms 5248 KB Output is correct
41 Correct 5 ms 5248 KB Output is correct
42 Correct 4 ms 5120 KB Output is correct
43 Correct 4 ms 5248 KB Output is correct
44 Correct 4 ms 5120 KB Output is correct
45 Correct 5 ms 5248 KB Output is correct
46 Correct 4 ms 5248 KB Output is correct
47 Correct 6 ms 5248 KB Output is correct
48 Correct 119 ms 17628 KB Output is correct
49 Correct 142 ms 18768 KB Output is correct
50 Correct 166 ms 18916 KB Output is correct
51 Correct 93 ms 15788 KB Output is correct
52 Correct 125 ms 18656 KB Output is correct
53 Correct 108 ms 17280 KB Output is correct
54 Correct 63 ms 15332 KB Output is correct
55 Correct 174 ms 26060 KB Output is correct
56 Correct 188 ms 22092 KB Output is correct
57 Correct 154 ms 20828 KB Output is correct
58 Correct 141 ms 19920 KB Output is correct
59 Correct 130 ms 18760 KB Output is correct
60 Correct 88 ms 19144 KB Output is correct
61 Correct 96 ms 19400 KB Output is correct
62 Correct 116 ms 19536 KB Output is correct
63 Correct 104 ms 18760 KB Output is correct
64 Correct 112 ms 19408 KB Output is correct
65 Correct 122 ms 19664 KB Output is correct
66 Correct 90 ms 18740 KB Output is correct
67 Correct 67 ms 15712 KB Output is correct
68 Correct 84 ms 17464 KB Output is correct
69 Correct 100 ms 19660 KB Output is correct
70 Correct 104 ms 18992 KB Output is correct
71 Correct 64 ms 15520 KB Output is correct
72 Correct 81 ms 16660 KB Output is correct
73 Correct 110 ms 19740 KB Output is correct
74 Correct 97 ms 18512 KB Output is correct
75 Correct 168 ms 18636 KB Output is correct
76 Correct 137 ms 18508 KB Output is correct
77 Correct 118 ms 18992 KB Output is correct
78 Correct 70 ms 14548 KB Output is correct
79 Correct 99 ms 15916 KB Output is correct
80 Correct 149 ms 18468 KB Output is correct
81 Correct 112 ms 19248 KB Output is correct
82 Correct 105 ms 19164 KB Output is correct