結果
問題 |
No.806 木を道に
|
ユーザー |
![]() |
提出日時 | 2019-03-22 21:39:42 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,589 bytes |
コンパイル時間 | 1,624 ms |
コンパイル使用メモリ | 172,520 KB |
実行使用メモリ | 25,184 KB |
最終ジャッジ日時 | 2024-09-19 02:58:55 |
合計ジャッジ時間 | 4,253 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 6 WA * 21 |
ソースコード
#include <bits/stdc++.h> using namespace std; using LL = long long; const LL LINF = 1e18; class Edge{ public: LL to,from,value; Edge(LL t,LL f,LL c){ to = t; from = f; value = c; } }; #define P pair<LL,LL> std::vector<LL> dijkstra(int s,int N,vector<Edge> E){ std::vector<LL> ans(N,LINF); std::vector<std::vector<Edge>> Edges(N); for(auto e : E){ Edges.at(e.from).push_back(e); } priority_queue<P, std::vector<P>, greater<P>> que; ans.at(s)= 0; que.push(make_pair(0,s)); while(!que.empty()){ P p = que.top();que.pop(); if(ans.at(p.second) < p.first)continue; for(int i = 0;i < Edges.at(p.second).size();i++){ if(ans.at(p.second) + Edges.at(p.second).at(i).value < ans.at(Edges.at(p.second).at(i).to)){ ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).value; que.push(make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to)); } } } return ans; } int main(){ int N;cin >> N; vector<Edge> Edges; for(int a = 0;a < N-1;a++){ int b,c;cin >> b >> c; Edges.push_back(Edge(b-1,c-1,1)); Edges.push_back(Edge(c-1,b-1,1)); } vector<LL> di = dijkstra(0,N,Edges); int idx = -1; LL id = 0; for(int a = 0;a < N;a++){ if(id < di.at(a)){ id = di.at(a); idx = id; } } vector<LL> ansns = dijkstra(idx,N,Edges); cout<<N-*max_element(ansns.begin(),ansns.end())-1<<endl;; }