結果
問題 | No.1976 Cut then Connect |
ユーザー | kwm_t |
提出日時 | 2022-06-12 14:44:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 56 ms / 2,000 ms |
コード長 | 3,996 bytes |
コンパイル時間 | 2,463 ms |
コンパイル使用メモリ | 219,044 KB |
実行使用メモリ | 9,984 KB |
最終ジャッジ日時 | 2024-11-24 12:17:16 |
合計ジャッジ時間 | 3,862 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 50 ms
9,600 KB |
testcase_03 | AC | 28 ms
6,912 KB |
testcase_04 | AC | 11 ms
6,820 KB |
testcase_05 | AC | 26 ms
6,816 KB |
testcase_06 | AC | 39 ms
8,064 KB |
testcase_07 | AC | 26 ms
6,816 KB |
testcase_08 | AC | 54 ms
9,984 KB |
testcase_09 | AC | 47 ms
8,960 KB |
testcase_10 | AC | 13 ms
6,820 KB |
testcase_11 | AC | 53 ms
9,856 KB |
testcase_12 | AC | 3 ms
6,820 KB |
testcase_13 | AC | 38 ms
8,192 KB |
testcase_14 | AC | 34 ms
7,936 KB |
testcase_15 | AC | 34 ms
7,680 KB |
testcase_16 | AC | 49 ms
9,344 KB |
testcase_17 | AC | 18 ms
6,816 KB |
testcase_18 | AC | 5 ms
6,820 KB |
testcase_19 | AC | 40 ms
8,192 KB |
testcase_20 | AC | 56 ms
9,984 KB |
testcase_21 | AC | 32 ms
7,296 KB |
testcase_22 | AC | 2 ms
6,820 KB |
testcase_23 | AC | 2 ms
6,816 KB |
testcase_24 | AC | 1 ms
6,820 KB |
testcase_25 | AC | 1 ms
6,816 KB |
testcase_26 | AC | 1 ms
6,820 KB |
testcase_27 | AC | 2 ms
6,816 KB |
testcase_28 | AC | 1 ms
6,816 KB |
testcase_29 | AC | 2 ms
6,816 KB |
testcase_30 | AC | 2 ms
6,816 KB |
testcase_31 | AC | 2 ms
6,820 KB |
testcase_32 | AC | 2 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<long long,long long> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct ReRooting { struct Edge { int to; Edge(int _to) :to(_to) {} }; struct Data { int diameter; //直径 int rootmax1st; //根からのmax int rootmax2nd; //根からの2ndmax Data() :diameter(0), rootmax1st(0), rootmax2nd(0) {} Data(int _diameter, int _rootmax1st, int _rootmax2nd) :diameter(_diameter), rootmax1st(_rootmax1st), rootmax2nd(_rootmax2nd) {} }; vector<vector<Edge>>g; vector<Data>dp; int ans; int sz; ReRooting(int _sz) :sz(_sz) { g.resize(sz); dp.reserve(sz); } void ReadGraph() { rep(i, sz - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({ b }); g[b].push_back({ a }); } } Data merge(const Data &lh, const Data &rh) { Data ret = lh; chmax(ret.diameter, rh.diameter); chmax(ret.diameter, rh.rootmax1st + 1); if (rh.rootmax1st + 1 >= ret.rootmax1st) { ret.rootmax2nd = ret.rootmax1st; ret.rootmax1st = rh.rootmax1st + 1; } else if (rh.rootmax1st + 1 >= ret.rootmax2nd) { ret.rootmax2nd = rh.rootmax1st + 1; } chmax(ret.diameter, ret.rootmax1st + ret.rootmax2nd); return ret; } Data mergeSub(const Data &lh, const Data &rh) {//★★ Data ret; ret.diameter = max(lh.diameter, rh.diameter); chmax(ret.diameter, lh.rootmax1st + rh.rootmax1st); vector<int>v = { lh.rootmax1st,lh.rootmax2nd,rh.rootmax1st,rh.rootmax2nd }; sort(allR(v)); ret.rootmax1st = v[0]; ret.rootmax2nd = v[1]; //cout << lh.diameter << lh.rootmax1st << lh.rootmax2nd << endl; //cout << rh.diameter << rh.rootmax1st << rh.rootmax2nd << endl; //Data ret = lh; //ret.val *= rh.val; return ret; } void dfs(int v, int p = -1) { Data val; for (auto e : g[v]) { if (p == e.to)continue; dfs(e.to, v); val = merge(val, dp[e.to]); } dp[v] = val; } void dfsR(int v, int p = -1) { if (-1 != p) { Data data = dp[p]; int anssub = 0; chmax(anssub, data.diameter); chmax(anssub, dp[v].diameter); chmax(anssub, 1 + (dp[v].diameter + 1) / 2 + (data.diameter + 1) / 2); chmin(ans, anssub); //cout << v << endl; //cout << "child " << dp[v].diameter << endl; //cout << "head " << dp[p].diameter << " " << dp[p].rootmax1st << " " << dp[p].rootmax2nd << endl; //cout << "child " << dp[v].diameter << " " << dp[v].rootmax1st << " " << dp[v].rootmax2nd << endl; //cout << anssub << endl; //cout << endl; } vector<Data>vdata; for (auto e : g[v]) vdata.push_back(dp[e.to]); int sz = vdata.size(); vector<Data>sumL(sz + 1), sumR(sz + 1); rep(i, sz)sumL[i + 1] = merge(sumL[i], vdata[i]); rep(i, sz)sumR[i + 1] = merge(sumR[i], vdata[sz - i - 1]); for (int i = 0; i < g[v].size(); ++i) { auto e = g[v][i]; if (p == e.to)continue; dp[v] = mergeSub(sumL[i], sumR[sz - (i + 1)]); dfsR(e.to, v); } } void output() { cout << ans << endl; } void debug() { rep(i, sz)cout << dp[i].diameter << " " << dp[i].rootmax1st << " " << dp[i].rootmax2nd << endl; } void solve() { ans = INF; ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }