結果
問題 |
No.3113 The farthest point
|
ユーザー |
![]() |
提出日時 | 2025-04-18 20:32:42 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,929 bytes |
コンパイル時間 | 3,489 ms |
コンパイル使用メモリ | 293,720 KB |
実行使用メモリ | 22,596 KB |
最終ジャッジ日時 | 2025-04-18 20:32:55 |
合計ジャッジ時間 | 10,190 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 WA * 13 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(),(x).end() #define IO ios::sync_with_stdio(false),cin.tie(nullptr); #define LB(v, x) (ll)(lower_bound(ALL(v),x)-(v).begin()) #define UQ(v) sort(ALL(v)),(v).erase(unique(ALL(v)),v.end()) #define REP(i, n) for(ll i=0; i<(ll)(n); i++) #define FOR(i, a, b) for(ll i=(ll)(a); (a)<(b) ? i<(b) : i>(b); i+=((a)<(b) ? 1 : -1)) #define chmax(a, b) ((a)<(b) ? ((a)=(b), 1) : 0) #define chmin(a, b) ((a)>(b) ? ((a)=(b), 1) : 0) template<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>; using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using ull=uint64_t; using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>; using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>; // #include"kyopro_library/graph/shortest_path/dijkstra.hpp" /// @brief 重みなしグラフ g の頂点 start からの最短距離を求める /// @note O(E+V) VL BFS(const VVI& g, int start=0){ int n=g.size(); VL ret(n,INF); ret[start]=0; queue<int> que; que.push(start); while(!que.empty()){ int now=que.front();que.pop(); for(int nxt:g[now])if(chmin(ret[nxt],ret[now]+1)) que.push(nxt); } return ret; } VL Dijkstra(const WG& g, int start=0){ int n=g.size(); VL ret(n,INFL); ret[start]=0; rpriority_queue<pair<ll,int>> pq; pq.push({0,start}); VI seen(n); seen[start]=1; while(!pq.empty()){ auto [tmp,now]=pq.top();pq.pop(); if(ret[now]<tmp) continue; seen[now]=true; for(auto [nxt,cost]:g[now])if(!seen[nxt]&&chmin(ret[nxt],ret[now]+cost)) pq.push({ret[nxt],nxt}); } return ret; } pair<VI,ll>TreeDiameter(const WG& g){ VL dist=Dijkstra(g); int s=max_element(ALL(dist))-dist.begin(); dist=Dijkstra(g,s); int t=max_element(ALL(dist))-dist.begin(); VI path; int now=t; while(now!=s){ path.push_back(now); for(auto[nxt,cost]:g[now]){ if(dist[now]==dist[nxt]+cost){ now=nxt; break; } } } path.push_back(s); ll diameter=dist[t]; return {path,diameter}; } pair<VI,ll>TreeDiameter(const vector<VI>& g){ VL dist=BFS(g); int s=max_element(ALL(dist))-dist.begin(); dist=BFS(g,s); int t=max_element(ALL(dist))-dist.begin(); VI path; int now=t; while(now!=s){ path.push_back(now); for(int nxt:g[now]){ if(dist[now]==dist[nxt]+1){ now=nxt; break; } } } path.push_back(s); ll diameter=dist[t]; return {path,diameter}; } int main(){ int N; cin>>N; WG G(N); REP(i,N-1){ ll u,v,w; cin>>u>>v>>w; u--,v--; G[u].push_back({v,w}); G[v].push_back({u,w}); } cout<<TreeDiameter(G).second<<endl; }