답안 #1052371

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1052371 2024-08-10T14:01:06 Z duckindog Village (BOI20_village) C++17
100 / 100
76 ms 42676 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], rvs[N], it;
  void dfs0(int u, int p = -1) { 
    sz[u] = 1;
    st[u] = ++it; rvs[it] = u;
    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<int> vt(n); iota(vt.begin(), vt.end(), 1);
    sort(vt.begin(), vt.end(), [&](const auto& a, const auto& b) { 
      return st[a] < st[b];
    });
    vt.insert(vt.end(), vt.begin(), vt.begin() + (n + 1) / 2);
    vt.erase(vt.begin(), vt.begin() + (n + 1) / 2);

    answer.resize(n);
    for (int i = 1; i <= n; ++i) answer[rvs[i] - 1] = vt[i - 1];
  }
}
 
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 Correct 2 ms 10328 KB Output is correct
2 Correct 2 ms 10328 KB Output is correct
3 Correct 2 ms 10332 KB Output is correct
4 Correct 2 ms 10328 KB Output is correct
5 Correct 2 ms 10332 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 2 ms 10332 KB Output is correct
8 Correct 2 ms 10328 KB Output is correct
9 Correct 2 ms 10332 KB Output is correct
10 Correct 2 ms 10328 KB Output is correct
11 Correct 2 ms 10332 KB Output is correct
12 Correct 2 ms 10332 KB Output is correct
13 Correct 2 ms 10332 KB Output is correct
14 Correct 1 ms 10468 KB Output is correct
15 Correct 2 ms 10332 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 2 ms 10332 KB Output is correct
4 Correct 3 ms 10328 KB Output is correct
5 Correct 2 ms 10332 KB Output is correct
6 Correct 3 ms 10328 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10332 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10332 KB Output is correct
13 Correct 2 ms 10332 KB Output is correct
14 Correct 2 ms 10332 KB Output is correct
15 Correct 2 ms 10332 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10332 KB Output is correct
18 Correct 2 ms 10332 KB Output is correct
19 Correct 2 ms 10340 KB Output is correct
20 Correct 2 ms 10332 KB Output is correct
21 Correct 2 ms 10332 KB Output is correct
22 Correct 2 ms 10332 KB Output is correct
23 Correct 2 ms 10332 KB Output is correct
24 Correct 2 ms 10332 KB Output is correct
25 Correct 2 ms 10332 KB Output is correct
26 Correct 2 ms 10332 KB Output is correct
27 Correct 2 ms 10336 KB Output is correct
28 Correct 2 ms 10340 KB Output is correct
29 Correct 2 ms 10340 KB Output is correct
30 Correct 2 ms 10336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10328 KB Output is correct
2 Correct 2 ms 10328 KB Output is correct
3 Correct 2 ms 10332 KB Output is correct
4 Correct 2 ms 10328 KB Output is correct
5 Correct 2 ms 10332 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 2 ms 10332 KB Output is correct
8 Correct 2 ms 10328 KB Output is correct
9 Correct 2 ms 10332 KB Output is correct
10 Correct 2 ms 10328 KB Output is correct
11 Correct 2 ms 10332 KB Output is correct
12 Correct 2 ms 10332 KB Output is correct
13 Correct 2 ms 10332 KB Output is correct
14 Correct 1 ms 10468 KB Output is correct
15 Correct 2 ms 10332 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10584 KB Output is correct
18 Correct 2 ms 10332 KB Output is correct
19 Correct 2 ms 10332 KB Output is correct
20 Correct 2 ms 10332 KB Output is correct
21 Correct 3 ms 10328 KB Output is correct
22 Correct 2 ms 10332 KB Output is correct
23 Correct 3 ms 10328 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 2 ms 10588 KB Output is correct
26 Correct 2 ms 10332 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10332 KB Output is correct
30 Correct 2 ms 10332 KB Output is correct
31 Correct 2 ms 10332 KB Output is correct
32 Correct 2 ms 10332 KB Output is correct
33 Correct 2 ms 10332 KB Output is correct
34 Correct 2 ms 10332 KB Output is correct
35 Correct 2 ms 10332 KB Output is correct
36 Correct 2 ms 10340 KB Output is correct
37 Correct 2 ms 10332 KB Output is correct
38 Correct 2 ms 10332 KB Output is correct
39 Correct 2 ms 10332 KB Output is correct
40 Correct 2 ms 10332 KB Output is correct
41 Correct 2 ms 10332 KB Output is correct
42 Correct 2 ms 10332 KB Output is correct
43 Correct 2 ms 10332 KB Output is correct
44 Correct 2 ms 10336 KB Output is correct
45 Correct 2 ms 10340 KB Output is correct
46 Correct 2 ms 10340 KB Output is correct
47 Correct 2 ms 10336 KB Output is correct
48 Correct 50 ms 19780 KB Output is correct
49 Correct 54 ms 20868 KB Output is correct
50 Correct 50 ms 20796 KB Output is correct
51 Correct 39 ms 18528 KB Output is correct
52 Correct 49 ms 20628 KB Output is correct
53 Correct 44 ms 19608 KB Output is correct
54 Correct 33 ms 25968 KB Output is correct
55 Correct 76 ms 42676 KB Output is correct
56 Correct 64 ms 30652 KB Output is correct
57 Correct 57 ms 27276 KB Output is correct
58 Correct 57 ms 24344 KB Output is correct
59 Correct 50 ms 20608 KB Output is correct
60 Correct 37 ms 18792 KB Output is correct
61 Correct 37 ms 19612 KB Output is correct
62 Correct 37 ms 19324 KB Output is correct
63 Correct 38 ms 18872 KB Output is correct
64 Correct 53 ms 19952 KB Output is correct
65 Correct 37 ms 19964 KB Output is correct
66 Correct 34 ms 19068 KB Output is correct
67 Correct 28 ms 17204 KB Output is correct
68 Correct 33 ms 18576 KB Output is correct
69 Correct 38 ms 20092 KB Output is correct
70 Correct 34 ms 19388 KB Output is correct
71 Correct 26 ms 17064 KB Output is correct
72 Correct 28 ms 17888 KB Output is correct
73 Correct 36 ms 20096 KB Output is correct
74 Correct 33 ms 19164 KB Output is correct
75 Correct 50 ms 19968 KB Output is correct
76 Correct 52 ms 20092 KB Output is correct
77 Correct 44 ms 20412 KB Output is correct
78 Correct 29 ms 16836 KB Output is correct
79 Correct 32 ms 17580 KB Output is correct
80 Correct 57 ms 19836 KB Output is correct
81 Correct 46 ms 20412 KB Output is correct
82 Correct 40 ms 19080 KB Output is correct