結果

問題 No.806 木を道に
ユーザー leaf_1415leaf_1415
提出日時 2019-03-22 21:31:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 647 bytes
コンパイル時間 719 ms
コンパイル使用メモリ 61,276 KB
実行使用メモリ 13,740 KB
最終ジャッジ日時 2023-10-19 06:31:19
合計ジャッジ時間 2,712 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,932 KB
testcase_01 AC 3 ms
5,932 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
5,932 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
5,932 KB
testcase_08 AC 3 ms
5,932 KB
testcase_09 AC 3 ms
5,936 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 42 ms
11,076 KB
testcase_25 AC 63 ms
13,740 KB
testcase_26 AC 22 ms
6,984 KB
testcase_27 AC 67 ms
9,372 KB
testcase_28 AC 4 ms
6,044 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:12: warning: ‘root’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |         dfs(root, -1, 0);
      |         ~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> G[100005];
int dist[100005];

void dfs(int v, int pre, int d)
{
	dist[v] = d;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == pre) continue;
		dfs(G[v][i], v, d+1);
	}
}

int main(void)
{
	cin >> n;
	int u, v;
	for(int i = 0; i < n-1; i++){
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	
	dfs(1, -1, 0);
	int mx = 0, root;
	for(int i = 1; i <= n; i++){
		if(mx < dist[i]){
			mx = dist[i];
			root = i;
		}
	}
	
	dfs(root, -1, 0);
	int dia = 0;
	for(int i = 1; i <= n; i++) dia = max(dia, dist[i]);
	
	cout << (n-1) - dia << endl;
	return 0;
}
0