답안 #1051834

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1051834 2024-08-10T10:05:18 Z duckindog Village (BOI20_village) C++17
50 / 100
73 ms 42428 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100'000 + 10;
const long long MAX = 1'000'000'000'000'000'000;
int n;
vector<int> ad[N];

namespace findMin { 
  long long f[N][2];
  vector<tuple<int, int, int>> trace[N][2];
  void dfs0(int u, int p = -1) { 
    f[u][1] = 0;
    
    vector<int> best;
    for (const auto& v : ad[u]) { 
      if (v == p) continue;
      dfs0(v, u);
      best.push_back(v);
    }

    if (best.size()) { 
      sort(best.begin(), best.end(), [&](const auto& a, const auto& b) { 
        return f[a][1] - f[a][0] > f[b][1] - f[b][0];
      });

      { //f[u][1]
        vector<int> vt = best;
        auto& ret = f[u][1];
        auto& tr = trace[u][1];

        while (vt.size() > 1) { 
          long long value = min(MAX, f[vt.back()][1] + f[vt.end()[-2]][1] + 4);  
          if (value <= min(MAX, f[vt.back()][0] + f[vt.end()[-2]][0])) { 
            ret = min(MAX, ret + value);
            tr.emplace_back(0, vt.back(), vt.end()[-2]);
            vt.pop_back();
            vt.pop_back();
          } else break;
        }
        for (const auto& x : vt) {
          ret = min(MAX, ret + f[x][0]);
          tr.emplace_back(1, x, 0);
        }
      }
      
      { //f[u][0]
        vector<int> vt = best;
        auto& ret = f[u][0];
        auto& tr = trace[u][0];

        ret = f[vt.back()][1] + 2;
        tr.emplace_back(2, vt.back(), 1);
        vt.pop_back();
        for (const auto& x : vt) { 
          if (f[x][1] + 2 <= f[x][0]) { 
            ret = min(MAX, ret + f[x][1] + 2);
            tr.emplace_back(2, x, 1);
          } else { 
            ret = min(MAX, ret + f[x][0]);
            tr.emplace_back(2, x, 0);
          }
        }
      }
      

      if (f[u][0] > f[u][1] + 2) { 
        f[u][0] = min(MAX, f[u][1] + 2);
        trace[u][0] = {make_tuple(3, best.back(), 0)};
      }
    }
    if (f[u][1] >= MAX || f[u][1] < 0) f[u][1] = 2 * MAX;
    if (f[u][0] >= MAX || f[u][0] < 0) f[u][0] = MAX;
  }

  int vt[N];
  void dfs1(int u, int st) { 
    for (const auto& [type, a, b] : trace[u][st]) { 
      if (!type) { 
        dfs1(a, 1); dfs1(b, 1);
        swap(vt[a], vt[b]);
      } else if (type == 1) {
        dfs1(a, 0);
      } else if (type == 2) { 
        dfs1(a, b);
        if (b) swap(vt[u], vt[a]);
      } else { 
        dfs1(u, 1);
        swap(vt[u], vt[a]);
      }
    }
  }
  int length;
  vector<int> answer;
  void solve() { 
    memset(f, 14, sizeof f);
    dfs0(1);
    iota(vt + 1, vt + n + 1, 1);
    dfs1(1, 0);
    length = f[1][0];
    answer = vector<int>(vt + 1, vt + n + 1);
  }
}

namespace findMax { 
  long long length;
  int sz[N], st[N], it;
  void dfs0(int u, int p = -1) { 
    st[u] = ++it;
    sz[u] = 1;
    for (const auto& v : ad[u]) { 
      if (v == p) continue;
      dfs0(v, u);
      sz[u] += sz[v];
      length += 2 * min(sz[v], n - sz[v]);
    }
  }

  vector<int> answer;
  void solve() { 
    dfs0(1);
    
    vector<pair<int, int>> vt;
    for (int i = 1; i <= n; ++i) vt.push_back({i, i});
    sort(vt.begin(), vt.end(), [&](const auto& a, const auto& b) { return st[a.first] < st[b.first]; });
    for (int i = 1; i <= n; ++i) swap(vt[i - 1].first, vt[(i + n / 2) % n].first);
    
    answer.resize(n);
    for (const auto& x : vt) answer[x.second - 1] = x.first;
  }
}

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i < n; ++i) { 
    int u, v; cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }

  findMin::solve();
  findMax::solve();

  cout << findMin::length << " " << findMax::length << "\n";
  for (int i = 0; i < n; ++i) cout << findMin::answer[i] << " \n"[i == n - 1];
  for (int i = 0; i < n; ++i) cout << findMax::answer[i] << " \n"[i == n - 1];
}
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 10076 KB Partially correct
2 Partially correct 2 ms 10076 KB Partially correct
3 Partially correct 2 ms 10076 KB Partially correct
4 Partially correct 2 ms 10072 KB Partially correct
5 Partially correct 2 ms 10076 KB Partially correct
6 Partially correct 2 ms 10076 KB Partially correct
7 Correct 2 ms 10076 KB Output is correct
8 Partially correct 2 ms 10076 KB Partially correct
9 Partially correct 2 ms 9820 KB Partially correct
10 Partially correct 2 ms 9820 KB Partially correct
11 Partially correct 1 ms 10076 KB Partially correct
12 Partially correct 2 ms 9820 KB Partially correct
13 Correct 2 ms 10076 KB Output is correct
14 Correct 1 ms 9820 KB Output is correct
15 Partially correct 1 ms 9820 KB Partially correct
16 Correct 2 ms 9820 KB Output is correct
17 Partially correct 2 ms 10076 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 10072 KB Partially correct
2 Partially correct 2 ms 10076 KB Partially correct
3 Partially correct 2 ms 10076 KB Partially correct
4 Partially correct 2 ms 10076 KB Partially correct
5 Partially correct 2 ms 10076 KB Partially correct
6 Partially correct 2 ms 10076 KB Partially correct
7 Partially correct 2 ms 10332 KB Partially correct
8 Partially correct 2 ms 10120 KB Partially correct
9 Partially correct 2 ms 10076 KB Partially correct
10 Partially correct 2 ms 10076 KB Partially correct
11 Partially correct 2 ms 10076 KB Partially correct
12 Correct 2 ms 10076 KB Output is correct
13 Partially correct 2 ms 10076 KB Partially correct
14 Partially correct 2 ms 10076 KB Partially correct
15 Partially correct 2 ms 10076 KB Partially correct
16 Partially correct 2 ms 10076 KB Partially correct
17 Partially correct 2 ms 10076 KB Partially correct
18 Partially correct 2 ms 10072 KB Partially correct
19 Partially correct 2 ms 10076 KB Partially correct
20 Partially correct 2 ms 10076 KB Partially correct
21 Partially correct 2 ms 10076 KB Partially correct
22 Partially correct 2 ms 10076 KB Partially correct
23 Partially correct 2 ms 10076 KB Partially correct
24 Partially correct 2 ms 9944 KB Partially correct
25 Partially correct 2 ms 10076 KB Partially correct
26 Partially correct 2 ms 10176 KB Partially correct
27 Partially correct 2 ms 10076 KB Partially correct
28 Partially correct 2 ms 10072 KB Partially correct
29 Partially correct 2 ms 10140 KB Partially correct
30 Partially correct 2 ms 10032 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 10076 KB Partially correct
2 Partially correct 2 ms 10076 KB Partially correct
3 Partially correct 2 ms 10076 KB Partially correct
4 Partially correct 2 ms 10072 KB Partially correct
5 Partially correct 2 ms 10076 KB Partially correct
6 Partially correct 2 ms 10076 KB Partially correct
7 Correct 2 ms 10076 KB Output is correct
8 Partially correct 2 ms 10076 KB Partially correct
9 Partially correct 2 ms 9820 KB Partially correct
10 Partially correct 2 ms 9820 KB Partially correct
11 Partially correct 1 ms 10076 KB Partially correct
12 Partially correct 2 ms 9820 KB Partially correct
13 Correct 2 ms 10076 KB Output is correct
14 Correct 1 ms 9820 KB Output is correct
15 Partially correct 1 ms 9820 KB Partially correct
16 Correct 2 ms 9820 KB Output is correct
17 Partially correct 2 ms 10076 KB Partially correct
18 Partially correct 2 ms 10072 KB Partially correct
19 Partially correct 2 ms 10076 KB Partially correct
20 Partially correct 2 ms 10076 KB Partially correct
21 Partially correct 2 ms 10076 KB Partially correct
22 Partially correct 2 ms 10076 KB Partially correct
23 Partially correct 2 ms 10076 KB Partially correct
24 Partially correct 2 ms 10332 KB Partially correct
25 Partially correct 2 ms 10120 KB Partially correct
26 Partially correct 2 ms 10076 KB Partially correct
27 Partially correct 2 ms 10076 KB Partially correct
28 Partially correct 2 ms 10076 KB Partially correct
29 Correct 2 ms 10076 KB Output is correct
30 Partially correct 2 ms 10076 KB Partially correct
31 Partially correct 2 ms 10076 KB Partially correct
32 Partially correct 2 ms 10076 KB Partially correct
33 Partially correct 2 ms 10076 KB Partially correct
34 Partially correct 2 ms 10076 KB Partially correct
35 Partially correct 2 ms 10072 KB Partially correct
36 Partially correct 2 ms 10076 KB Partially correct
37 Partially correct 2 ms 10076 KB Partially correct
38 Partially correct 2 ms 10076 KB Partially correct
39 Partially correct 2 ms 10076 KB Partially correct
40 Partially correct 2 ms 10076 KB Partially correct
41 Partially correct 2 ms 9944 KB Partially correct
42 Partially correct 2 ms 10076 KB Partially correct
43 Partially correct 2 ms 10176 KB Partially correct
44 Partially correct 2 ms 10076 KB Partially correct
45 Partially correct 2 ms 10072 KB Partially correct
46 Partially correct 2 ms 10140 KB Partially correct
47 Partially correct 2 ms 10032 KB Partially correct
48 Partially correct 45 ms 19384 KB Partially correct
49 Partially correct 54 ms 20324 KB Partially correct
50 Partially correct 56 ms 20444 KB Partially correct
51 Partially correct 38 ms 18124 KB Partially correct
52 Partially correct 50 ms 20164 KB Partially correct
53 Partially correct 44 ms 19236 KB Partially correct
54 Partially correct 34 ms 25408 KB Partially correct
55 Partially correct 73 ms 42428 KB Partially correct
56 Partially correct 62 ms 30152 KB Partially correct
57 Partially correct 58 ms 27076 KB Partially correct
58 Partially correct 61 ms 23996 KB Partially correct
59 Partially correct 54 ms 20372 KB Partially correct
60 Correct 41 ms 19056 KB Output is correct
61 Partially correct 41 ms 19288 KB Partially correct
62 Partially correct 43 ms 19024 KB Partially correct
63 Partially correct 41 ms 18428 KB Partially correct
64 Partially correct 43 ms 19644 KB Partially correct
65 Partially correct 37 ms 19636 KB Partially correct
66 Partially correct 34 ms 18716 KB Partially correct
67 Partially correct 28 ms 16916 KB Partially correct
68 Partially correct 32 ms 18220 KB Partially correct
69 Partially correct 36 ms 19648 KB Partially correct
70 Partially correct 34 ms 18920 KB Partially correct
71 Partially correct 30 ms 16596 KB Partially correct
72 Partially correct 29 ms 17548 KB Partially correct
73 Partially correct 50 ms 19652 KB Partially correct
74 Partially correct 33 ms 18696 KB Partially correct
75 Partially correct 49 ms 19460 KB Partially correct
76 Partially correct 51 ms 19728 KB Partially correct
77 Partially correct 41 ms 19944 KB Partially correct
78 Partially correct 30 ms 16600 KB Partially correct
79 Partially correct 34 ms 17096 KB Partially correct
80 Partially correct 53 ms 19384 KB Partially correct
81 Partially correct 40 ms 19948 KB Partially correct
82 Partially correct 38 ms 18624 KB Partially correct