結果

問題 No.3113 The farthest point
ユーザー hiikunZ
提出日時 2025-04-18 20:19:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 1,336 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 202,836 KB
実行使用メモリ 58,440 KB
最終ジャッジ日時 2025-04-18 20:20:07
合計ジャッジ時間 10,377 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
constexpr ll MAX = 2000000000000000000;
constexpr ld PI = 3.14159265358979;
constexpr ll MOD = 0;//2024948111;
ld dotorad(ld K){ return PI * K / 180.0; }
ld radtodo(ld K){ return K * 180.0 / PI; }
mt19937 mt;
void randinit(){ srand((unsigned)time(NULL));mt = mt19937(rand()); }
ll N;
vector<vector<ll>> GG(300000),G(300000),WW(300000),W(300000);
vector<ll> par(300000,-1);

ll ans = 0;
vector<ll> DP(300000,0);
void dfs1(ll x){
    vector<ll> s = {0,0};
    for(ll i = 0; i < GG[x].size(); i++){
        ll next = GG[x][i];
        if(par[next] == -1){
            G[x].emplace_back(next);
            W[x].emplace_back(WW[x][i]);
            par[next] = x;
            dfs1(next);

            s.emplace_back(DP[next] + WW[x][i]);
        }
    }
    sort(s.begin(),s.end(),greater<ll>());
    ans = max(ans,s[0] + s[1]);
    DP[x] = s[0];
}
int main(){
    cin >> N;
    for(ll i = 0; i < N - 1; i++){
        ll a,b,w;
        cin >> a >> b >> w;
        a--;b--;
        GG[a].emplace_back(b);
        GG[b].emplace_back(a);
        WW[a].emplace_back(w);
        WW[b].emplace_back(w);
    }
    par[0] = 0;
    dfs1(0);
    cout << ans << endl;
}
0