結果
問題 |
No.806 木を道に
|
ユーザー |
|
提出日時 | 2019-04-19 23:47:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 905 bytes |
コンパイル時間 | 806 ms |
コンパイル使用メモリ | 80,576 KB |
実行使用メモリ | 16,896 KB |
最終ジャッジ日時 | 2024-09-23 06:20:43 |
合計ジャッジ時間 | 3,593 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 WA * 18 |
コンパイルメッセージ
main.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 34 | main() | ^~~~
ソースコード
#include<iostream> using namespace std; #include<vector> #include<algorithm> template<typename T> pair<T,int>diameter_dfs(const vector<vector<pair<int,T> > >&G,int u,int p) { pair<T,int>ans(0,u); for(const pair<int,T>&e:G[u]) { if(e.first!=p) { pair<T,int>q=diameter_dfs(G,e.first,u); q.first+=e.second; ans=max(ans,q); } } return ans; } template<typename T> T diameter(const vector<vector<pair<int,T> > >&G) { pair<T,int>p=diameter_dfs(G,0,-1); pair<T,int>q=diameter_dfs(G,p.second,-1); return q.first; } int diameter(const vector<vector<int> >&G) { vector<vector<pair<int,int> > >H(G.size()); for(int i=0;i<G.size();i++)for(const int&e:G[i])H[i].push_back(make_pair(e,1)); return diameter(H); } int N; main() { cin>>N; vector<vector<int> >G(N); for(int i=1;i<N;i++) { int a,b;cin>>a>>b; G[a-1].push_back(b-1); G[b-1].push_back(a-1); } cout<<N-1-diameter(G)<<endl; }