結果
問題 |
No.3272 Separate Contractions
|
ユーザー |
|
提出日時 | 2025-09-13 03:10:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 423 ms / 3,000 ms |
コード長 | 8,938 bytes |
コンパイル時間 | 2,812 ms |
コンパイル使用メモリ | 189,780 KB |
実行使用メモリ | 60,240 KB |
最終ジャッジ日時 | 2025-09-13 03:10:27 |
合計ジャッジ時間 | 17,016 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 |
コンパイルメッセージ
main.cpp: In function ‘void solve()’: main.cpp:187:10: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions] 187 | auto [s, t] = diam.get_endpoints(); | ^
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template<typename T> struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template<typename T> struct edges : std::vector<edge<T>>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge<T>& a, const edge<T>& b){ return a.cost < b.cost; } ); } }; template<typename T = bool> struct graph : std::vector<edges<T>>{ private: int n = 0; int m = 0; edges<T> es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge<T>(from, to, cost, m)); (*this)[from].push_back(edge<T>(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge<T>(from, to, cost, m)); (*this)[from].push_back(edge<T>(from, to, cost, m)); (*this)[to].push_back(edge<T>(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge<T> get_edge(int i){ return es[i]; } edges<T> get_edge_set(){ return es; } }; template<typename T> struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template<typename T> using Edges = vector<edge<T>>; template<typename T> using weighted_graph = vector<Edges<T>>; template<typename T> using tree = vector<Edges<T>>; using unweighted_graph = vector<vector<int>>; template<typename T> using residual_graph = vector<vector<redge<T>>>; template<typename S> struct diameter{ int n; int s=0, t=0; vector<int> path; S diam = 0; diameter(graph<S> &T){ n = (int)T.size(); vector<S> dist1(n, 0); function<void(int, int)> dfs1 = [&](int v, int p){ for(auto e : T[v]) if(e.to!=p){ dist1[e.to] = dist1[v] + e.cost; dfs1(e.to, v); } }; dfs1(0, -1); S sdist = 0; for(int v=0; v<n; v++){ if(sdist < dist1[v]){ s = v; sdist = dist1[v]; } } vector<S> dist2(n, 0); function<void(int, int)> dfs2 = [&](int v, int p){ for(auto e : T[v]) if(e.to!=p){ dist2[e.to] = dist2[v] + e.cost; dfs2(e.to, v); } }; dfs2(s, -1); S tdist = 0; for(int v=0; v<n; v++){ if(tdist < dist2[v]){ t = v; tdist = dist2[v]; } } diam = tdist; function<bool(int, int)> dfs3 = [&](int v, int p){ if(v == t){ path.pb(v); return true; } bool flag = false; for(auto e : T[v]) if(e.to!=p){ flag = flag | dfs3(e.to, v); } if(flag) path.pb(v); return flag; }; dfs3(s, -1); reverse(path.begin(), path.end()); } pair<int, int> get_endpoints(){return {s, t};} vector<int> get_path(){return path;} S get_distance(){return diam;} }; void solve(){ int n; cin >> n; graph<int> T(n, false); for(int i=0; i<n-1; i++){ int u, v; cin >> u >> v; u--; v--; T.add_edge(u, v); } diameter<int> diam(T); auto [s, t] = diam.get_endpoints(); auto P = diam.get_path(); int D = diam.get_distance(); vector<int> ePath; for(int i=0; i<D; i++){ int v = P[i], w = P[i+1]; for(auto e : T[v]) if(e.to==w) ePath.pb(e.id); } function<void(int, int, vector<int>&)> calc_dist = [&](int v, int p, vector<int> &dist){ for(auto e : T[v]) if(e.to!=p){ dist[e.to] = dist[v] + 1; calc_dist(e.to, v, dist); } }; vector<int> sdist(n, 0), tdist(n, 0); calc_dist(s, -1, sdist); calc_dist(t, -1, tdist); vector<bool> onP(n, false); for(int v : P) onP[v] = true; vector<ll> ans(n-1, 0); ll U = 0; for(int i=0; i<n; i++) U += max(sdist[i], tdist[i]); vector<vector<int>> anc(n); // Case1 : P上にない辺の答えを計算 { vector<int> siz(n, 1); function<void(int, int, int)> dfs = [&](int v, int p, int r){ anc[r].pb(v); for(auto e : T[v]) if(e.to!=p && !onP[e.to]){ dfs(e.to, v, r); ans[e.id] = U - (siz[e.to]-1) - max(sdist[e.to], tdist[e.to]); siz[v] += siz[e.to]; } }; for(int v : P) dfs(v, -1, v); } // Case2 : D is even if(D%2 == 0){ // Case2-1 : e_0, e_1, ..., e_(D/2-1)を縮約 { vector<int> d(D+1, 0); for(int i=D-1; i>=0; i--){ d[i] = d[i+1]; for(int v : anc[P[i]]) d[i] = max(d[i], tdist[v]); } // 前から探索 int lsum = 0; int rsum = 0; for(int i=D; i>D/2; i--) rsum += anc[P[i]].size(); for(int i=0; i<D/2; i++){ // 辺ePath[i]を縮約 lsum += anc[P[i]].size(); if(d[i+1] == D){ ans[ePath[i]] = U - (lsum-1) - tdist[P[i]]; }else{ ans[ePath[i]] = U - (lsum-1) - tdist[P[i]] - rsum; } } } // Case2-2 : e_(D/2), e_(D/2+1), ..., e_(D-1)を縮約 { vector<int> d(D+1, 0); for(int i=1; i<=D; i++){ d[i] = d[i-1]; for(int v : anc[P[i]]) d[i] = max(d[i], sdist[v]); } // 後ろから探索 int lsum = 0; for(int i=0; i<D/2; i++) lsum += anc[P[i]].size(); int rsum = 0; for(int i=D-1; i>=D/2; i--){ // 辺ePath[i]を縮約 rsum += anc[P[i+1]].size(); if(d[i] == D){ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]]; }else{ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]] - lsum; } } } } // Case3 : D is odd if(D%2 == 1){ // Case3-1 : e_0, e_1, ..., e_(D/2-1)を縮約 { vector<int> d(D+1, 0); for(int i=D-1; i>=0; i--){ d[i] = d[i+1]; for(int v : anc[P[i]]) d[i] = max(d[i], tdist[v]); } // 前から探索 int lsum = 0; int rsum = 0; for(int i=D; i>D/2; i--) rsum += anc[P[i]].size(); for(int i=0; i<D/2; i++){ // 辺ePath[i]を縮約 lsum += anc[P[i]].size(); if(d[i+1] == D){ ans[ePath[i]] = U - (lsum-1) - tdist[P[i]]; }else{ ans[ePath[i]] = U - (lsum-1) - tdist[P[i]] - rsum; } } } // Case3-2 : e_(D/2) を縮約 ans[ePath[D/2]] = U-(n-1)-max(sdist[P[D/2]], tdist[P[D/2]]); // Case3-3 : e_(D/2+1), e_(D/2+2), ..., e_(D-1)を縮約 { vector<int> d(D+1, 0); for(int i=1; i<=D; i++){ d[i] = d[i-1]; for(int v : anc[P[i]]) d[i] = max(d[i], sdist[v]); } // 後ろから探索 int lsum = 0; for(int i=0; i<=D/2; i++) lsum += anc[P[i]].size(); int rsum = 0; for(int i=D-1; i>=D/2+1; i--){ // 辺ePath[i]を縮約 rsum += anc[P[i+1]].size(); if(d[i] == D){ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]]; }else{ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]] - lsum; } } } } for(int i=0; i<n-1; i++) cout << ans[i] << "\n"; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }