結果

問題 No.806 木を道に
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-08-29 02:43:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 852 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 173,424 KB
実行使用メモリ 21,648 KB
最終ジャッジ日時 2024-04-26 15:52:00
合計ジャッジ時間 4,688 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 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 51 ms
12,436 KB
testcase_25 AC 75 ms
17,016 KB
testcase_26 AC 28 ms
8,852 KB
testcase_27 AC 91 ms
21,648 KB
testcase_28 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

vector<int> BFS(vector<vector<int>> Hen, int s) {
	const int INF = 1e9;
	int n = Hen.size();
	vector<int> dist(n, INF);
	queue<int> que;
	dist[s] = 0;
	que.push(s);
	while(que.size()) {
		auto cur = que.front(); que.pop();
		for(auto i : Hen[cur]) {
			if(dist[i] != INF)continue;
			dist[i] = dist[cur] + 1;
			que.push(i);
		}
	}
	return dist;
}


int diameter(vector<vector<int>>(hen)) {
	auto dist = BFS(hen, 0);
	int idx = max_element(dist.begin(),dist.end()) - dist.begin();
	dist = BFS(hen, idx);
	return *max_element(dist.begin(), dist.end());
}

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> hen(n);
	for(int i = 0; i < n - 1; i++) {
		int a, b; cin >> a >> b;
		a--, b--;
		hen[a].emplace_back(b);
		hen[b].emplace_back(a);
	}
	cout << n-1 - diameter(hen) << endl;
}
0