結果

問題 No.3113 The farthest point
ユーザー lk step
提出日時 2025-04-18 21:29:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 753 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 197,600 KB
実行使用メモリ 20,040 KB
最終ジャッジ日時 2025-04-18 21:29:44
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> edge;
const ll INF = (1LL<<60);
ll ans=0;

ll n;
vector<edge> T[200000];

bool visited[200000];
ll x[200000];

ll dfs(ll p){
  visited[p]=true;
  ll maxm=0;
  
  for(int i=0;i<(int)T[p].size();i++){
    edge e=T[p][i];
    if(visited[e.first])continue;
    visited[e.first]=true;

    ll y = dfs(e.first)+e.second;
    ans=max(ans, maxm+y);
    maxm= max(maxm, y);
  }
  
  return maxm;
}

int main(){
  cin>>n;
  for(int i=0;i<n-1;i++){
    ll a,b,c;
    cin>>a>>b>>c;
    a--,b--;
    T[a].push_back( edge(b,c) );
    T[b].push_back( edge(a,c) );
  }
  for(int i=0;i<n;i++){
    visited[i]=false;
    x[i]=-INF;
  }

  
  dfs(0);
  cout<<ans<<endl;
  return 0;
}

0