結果

問題 No.3113 The farthest point
ユーザー Facade
提出日時 2025-04-19 08:03:59
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 954 bytes
コンパイル時間 4,109 ms
コンパイル使用メモリ 288,156 KB
実行使用メモリ 19,600 KB
最終ジャッジ日時 2025-04-19 08:04:11
合計ジャッジ時間 9,136 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int n;cin>>n;
    vector<vector<pair<int,int>>>connect(n);
    for(int i=0;i<n-1;i++){
        int a,b,c;cin>>a>>b>>c;
        a--;b--;
        connect[a].push_back({b,c});
        connect[b].push_back({a,c});
    }
    vector<bool>vis(n,false);
    vis[0]=true;
    vector<int>dist(n,0);
    auto DFS=[&](auto DFS,int now)->void{
        for(auto[to,weight]:connect[now]){
            if(!vis[to]){
                vis[to]=true;
                dist[to]=dist[now]+weight;
                DFS(DFS,to);
            }
        }
    };
    DFS(DFS,0);
    int nxt=0;
    int d=0;
    for(int i=0;i<n;i++){
        if(d<dist[i]){
            d=dist[i];
            nxt=i;
        }
    }
    dist.assign(n,0);
    vis.assign(n,false);
    vis[nxt]=true;
    DFS(DFS,nxt);
    int ans=0;
    for(int i=0;i<n;i++){
        ans=max(ans,dist[i]);
    }
    cout<<ans<<endl;
}
0