結果

問題 No.806 木を道に
ユーザー kotatsugamekotatsugame
提出日時 2019-04-19 23:47:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 905 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 79,468 KB
実行使用メモリ 17,052 KB
最終ジャッジ日時 2023-10-24 13:59:18
合計ジャッジ時間 5,164 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 48 ms
12,300 KB
testcase_25 AC 72 ms
17,052 KB
testcase_26 AC 24 ms
7,060 KB
testcase_27 AC 76 ms
15,236 KB
testcase_28 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   34 | main()
      | ^~~~

ソースコード

diff #

#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;
}
0