結果
問題 |
No.806 木を道に
|
ユーザー |
![]() |
提出日時 | 2019-03-22 21:36:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,497 bytes |
コンパイル時間 | 1,347 ms |
コンパイル使用メモリ | 173,060 KB |
実行使用メモリ | 19,360 KB |
最終ジャッジ日時 | 2024-09-19 02:56:03 |
合計ジャッジ時間 | 3,417 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 WA * 18 |
ソースコード
#include <bits/stdc++.h> using namespace std; using LL = long long; const LL LINF = 1e18; class Edge{ public: int to,from,value; Edge(int t,int f,int 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 = max_element(di.begin(),di.end())-di.begin(); vector<LL> ansns = dijkstra(idx,N,Edges); cout<<N-*max_element(ansns.begin(),ansns.end())-1<<endl;; }