結果

問題 No.3113 The farthest point
ユーザー umezo
提出日時 2025-04-19 16:15:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 4,138 ms
コンパイル使用メモリ 285,252 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2025-04-19 16:15:25
合計ジャッジ時間 6,704 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
template <class T> using V=vector<T>;
template <class T> using VV=V<V<T>>;

ll dp[200200],dp2[200200];
V<pair<int,ll>> G[200200];
const ll INF=1e18;
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  rep(i,n-1){
    int u,v;
    ll w;
    cin>>u>>v>>w;
    u--,v--;
    G[u].push_back({v,w});
    G[v].push_back({u,w});
  }
  auto f=[&](auto f,int v,int p)->void{
    V<ll> A;
    for(auto [nv,w]:G[v]){
      if(nv==p) continue;
      f(f,nv,v);
      A.push_back(max(w,w+dp[nv]));
    }
    sort(ALL(A));
    reverse(ALL(A));
    int a=A.size();
    if(a==0) return;
    dp[v]=A[0];
    dp2[v]=A[0];
    if(a>1 && A[1]>0) dp2[v]+=A[1];
  };
  f(f,0,-1);
  ll ans=-INF;
  rep(i,n) ans=max(ans,dp2[i]);
  cout<<ans<<endl;
    
  return 0;
}
0