結果
問題 | No.1976 Cut then Connect |
ユーザー | noya2 |
提出日時 | 2023-04-17 22:48:50 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 111 ms / 2,000 ms |
コード長 | 3,268 bytes |
コンパイル時間 | 2,417 ms |
コンパイル使用メモリ | 219,096 KB |
実行使用メモリ | 18,604 KB |
最終ジャッジ日時 | 2024-11-24 12:19:54 |
合計ジャッジ時間 | 4,785 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 102 ms
17,884 KB |
testcase_03 | AC | 55 ms
11,804 KB |
testcase_04 | AC | 19 ms
6,400 KB |
testcase_05 | AC | 47 ms
10,680 KB |
testcase_06 | AC | 79 ms
14,700 KB |
testcase_07 | AC | 51 ms
11,056 KB |
testcase_08 | AC | 106 ms
18,604 KB |
testcase_09 | AC | 91 ms
16,292 KB |
testcase_10 | AC | 23 ms
7,040 KB |
testcase_11 | AC | 108 ms
18,300 KB |
testcase_12 | AC | 4 ms
5,248 KB |
testcase_13 | AC | 78 ms
14,732 KB |
testcase_14 | AC | 71 ms
13,828 KB |
testcase_15 | AC | 66 ms
13,464 KB |
testcase_16 | AC | 94 ms
17,072 KB |
testcase_17 | AC | 34 ms
8,420 KB |
testcase_18 | AC | 7 ms
5,248 KB |
testcase_19 | AC | 80 ms
14,668 KB |
testcase_20 | AC | 111 ms
18,572 KB |
testcase_21 | AC | 61 ms
12,424 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 1 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 1 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 | 1 ms
5,248 KB |
testcase_32 | AC | 1 ms
5,248 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; template<class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)> struct Rerooting { struct edge{ int to, idx, xdi; }; Rerooting (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); } }; 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; Rerooting<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; }