結果
| 問題 |
No.3113 The farthest point
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2025-04-19 17:06:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 332 ms / 2,000 ms |
| コード長 | 3,451 bytes |
| コンパイル時間 | 2,102 ms |
| コンパイル使用メモリ | 170,636 KB |
| 実行使用メモリ | 20,420 KB |
| 最終ジャッジ日時 | 2025-04-19 17:06:14 |
| 合計ジャッジ時間 | 8,570 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_clz(unsigned int x){unsigned long r;_BitScanReverse(&r,x);return 31-r;}
#endif // _MSC_VER
template<class V> struct SparseTable { // [L,R)
const V def = -infl;
inline V comp(V a, V b) { return max(a,b); }
int n; vector<V> a, b[22]; inline int __lg(int x) { return 32 - 1 - __builtin_clz(x); }
void init(vector<V> v) {
int nn = v.size(); n = 1; while (n < nn) n *= 2; a.resize(n);
rep(i, 0, 22) b[i].resize(n); rep(i, 0, nn) a[i] = v[i];
int d = 1 << __lg(n - 1), e = d << 1;
for (int h = 0, f; (f = 1 << h) <= d; ++h) {
for (int i = f, r = f << 1; i < e; i += r) {
b[h][i - 1] = a[i - 1];
for (int j = i - 2; j >= i - f; --j) b[h][j] = comp(b[h][j + 1], a[j]);
b[h][i] = a[i];
for (int j = i + 1; j < i + f; ++j) b[h][j] = comp(b[h][j - 1], a[j]);
}
}
}
V get(int L,int R){assert(0<=L&&L<=R);if(L==R)return def;R--;if(L==R)return a[L];int h=__lg(L^R);
return comp(b[h][L],b[h][R]);}};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N;
vector<pair<int,ll>> E[201010];
ll dp[201010];
void dfs1(int cu, int pa = -1) {
dp[cu] = 0;
fore(p, E[cu]) {
int to = p.first;
ll c = p.second;
if (to == pa) continue;
dfs1(to, cu);
chmax(dp[cu], dp[to] + c);
}
}
ll ans = 0;
void dfs2(int cu, int pa = -1) {
fore(p, E[cu]) {
int to = p.first;
ll c = p.second;
chmax(ans, dp[to] + c);
}
int n = E[cu].size();
SparseTable<ll> st;
vector<ll> v(n, -infl);
rep(i, 0, n) {
int to = E[cu][i].first;
ll c = E[cu][i].second;
v[i] = dp[to] + c;
}
st.init(v);
rep(i, 0, n) {
int to = E[cu][i].first;
ll c = E[cu][i].second;
if (to == pa) continue;
dp[cu] = max(st.get(0, i), st.get(i + 1, n));
dfs2(to, cu);
}
}
void _main() {
cin >> N;
rep(i, 0, N - 1) {
int a, b; ll c;
cin >> a >> b >> c;
a--; b--;
E[a].push_back({b, c});
E[b].push_back({a, c});
}
rep(i, 0, N) dp[i] = -infl;
dfs1(0);
dfs2(0);
cout << ans << endl;
}
はまやんはまやん