結果

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

ソースコード

diff #

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

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using lll = __int128;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

vector<ll> dijkstra(vector<vector<pair<ll,ll>>>& ab,ll s=0){
    ll N=ab.size();
    vector<ll> dist(N,INF);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
    pq.push({0,s});
    while(pq.size()){
        auto [c,now]=pq.top();pq.pop();
        
        if(dist[now]!=INF) continue;
        dist[now]=c;
        for(auto [c2,to]:ab[now]){
       
                pq.push({c+c2,to});
            
        }
    }
    return dist;
}

pair<vector<ll>,ll> TreeDiameter(vector<vector<pair<ll,ll>>>& ab){
    vector<ll> dist=dijkstra(ab);
    ll s=max_element(all(dist))-dist.begin();
    dist=dijkstra(ab,s);
    ll t=max_element(all(dist))-dist.begin();

    vector<ll> path;
    ll now=t;
    while(now!=s){
        path.push_back(now);
        for(auto [c,to]:ab[now]){
            if(dist[now]==dist[to]+c){
                now=to;
                break;
            }
        }
    }
    path.push_back(s);
    ll diameter=dist[t];
    return{path,diameter};
} 

bool solve(){
    ll N;cin>>N;
    vector<vp> ab(N);
    rep(i,N-1){
        ll a,b,c;cin>>a>>b>>c;
        a--;b--;
        ab[a].push_back({c,b});
        ab[b].push_back({c,a});
    }
    auto [path,r]=TreeDiameter(ab);
    cout << r << endl;
    return 0;
}




int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll T=1;//cin>>T;
    rep(i,T) solve();
}
    
0