結果
問題 | No.1976 Cut then Connect |
ユーザー | noya2 |
提出日時 | 2023-05-08 19:28:04 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 168 ms / 2,000 ms |
コード長 | 4,229 bytes |
コンパイル時間 | 2,727 ms |
コンパイル使用メモリ | 220,564 KB |
実行使用メモリ | 18,700 KB |
最終ジャッジ日時 | 2024-11-25 09:31:46 |
合計ジャッジ時間 | 6,628 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 153 ms
17,884 KB |
testcase_03 | AC | 74 ms
11,808 KB |
testcase_04 | AC | 24 ms
6,400 KB |
testcase_05 | AC | 68 ms
10,684 KB |
testcase_06 | AC | 114 ms
14,664 KB |
testcase_07 | AC | 72 ms
11,096 KB |
testcase_08 | AC | 168 ms
18,612 KB |
testcase_09 | AC | 130 ms
16,296 KB |
testcase_10 | AC | 30 ms
7,168 KB |
testcase_11 | AC | 165 ms
18,300 KB |
testcase_12 | AC | 5 ms
5,248 KB |
testcase_13 | AC | 113 ms
14,732 KB |
testcase_14 | AC | 103 ms
13,828 KB |
testcase_15 | AC | 94 ms
13,464 KB |
testcase_16 | AC | 137 ms
17,072 KB |
testcase_17 | AC | 41 ms
8,448 KB |
testcase_18 | AC | 9 ms
5,248 KB |
testcase_19 | AC | 102 ms
14,600 KB |
testcase_20 | AC | 159 ms
18,700 KB |
testcase_21 | AC | 80 ms
12,552 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 2 ms
5,248 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 2 ms
5,248 KB |
testcase_32 | AC | 2 ms
5,248 KB |
ソースコード
#line 1 "test/tree/RerootingDP.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1976" #line 2 "template/template.hpp" #include <bits/stdc++.h> #define rep(i, s, n) for (int i = s; i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() using ll = long long; using ld = long double; using ull = unsigned long long; template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } namespace lib { using namespace std; } // namespace lib // using namespace lib; #line 2 "tree/RerootingDP.hpp" #line 4 "tree/RerootingDP.hpp" namespace lib { template <class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)> struct RerootingDP { struct edge { int to, idx, xdi; }; RerootingDP(int _n = 0) : n(_n) { es.resize(n); } void add_edge(int u, int v, int idx1, int idx2) { es[u].push_back({v, idx1, idx2}); es[v].push_back({u, idx2, idx1}); } vector<V> build(int v = 0) { root = v; outs.resize(n); subdp.resize(n); in.resize(n), up.resize(n); int tnow = 0; dfs(root, -1, tnow); return subdp; } vector<V> reroot() { reverse_edge.resize(n); reverse_edge[root] = e(); reverse_dp.resize(n); answers.resize(n); bfs(root); return answers; } V get(int r, int v) { if (r == v) return answers[r]; if (!(in[v] < in[r] && up[r] <= up[v])) return subdp[v]; int le = 0, ri = outs[v].size(); while (ri - le > 1) { int md = (le + ri) / 2; if (in[es[v][md].to] <= in[r]) le = md; else ri = md; } return reverse_dp[es[v][le].to]; } const vector<edge> &operator[](int idx) const { return es[idx]; } private: int n, root; vector<vector<edge>> es; vector<vector<E>> outs; vector<E> reverse_edge; vector<V> subdp, reverse_dp, answers; vector<int> in, up; void dfs(int v, int p, int &t) { E val = e(); in[v] = t++; for (auto &u : es[v]) { if (u.to == p && u.to != es[v].back().to) swap(u, es[v].back()); if (u.to == p) continue; dfs(u.to, v, t); E nval = put_edge(subdp[u.to], u.idx); outs[v].emplace_back(nval); val = merge(val, nval); } subdp[v] = put_vertex(val, v); up[v] = t; } void bfs(int v) { int siz = outs[v].size(); vector<E> lui(siz + 1), rui(siz + 1); lui[0] = e(), rui[siz] = e(); for (int i = 0; i < siz; i++) lui[i + 1] = merge(lui[i], outs[v][i]); for (int i = siz - 1; i >= 0; i--) rui[i] = merge(outs[v][i], rui[i + 1]); for (int i = 0; i < siz; i++) { reverse_dp[es[v][i].to] = put_vertex( merge(merge(lui[i], rui[i + 1]), reverse_edge[v]), v); reverse_edge[es[v][i].to] = put_edge(reverse_dp[es[v][i].to], es[v][i].xdi); bfs(es[v][i].to); } answers[v] = put_vertex(merge(lui[siz], reverse_edge[v]), v); } }; } // namespace lib #line 5 "test/tree/RerootingDP.test.cpp" using namespace lib; using P = pair<int,int>; P merge(P a, P b){ return P(max(max(a.first,b.first),a.second+b.second),max(a.second,b.second)); } P e(){ return P(0,0); } P pute(P v, int id){ return P(max(v.first,v.second+1),v.second+1); } P putv(P e, int id){ return e; } int main(){ int n; cin >> n; RerootingDP<P,P,merge,e,pute,putv> g(n); for (int i = 0; i < n-1; i++){ int u, v; cin >> u >> v; u--, v--; g.add_edge(u,v,i,i); } g.build(); g.reroot(); int ans = n; for (int u = 0; u < n; u++){ for (auto [v, idx, dxi] : g[u]){ int x = g.get(u,v).first; int y = g.get(v,u).first; ans = min(ans,max({x,y,(x+1)/2+(y+1)/2+1})); } } cout << ans << endl; }