結果

問題 No.1719 Tree and Permutation
ユーザー square1001square1001
提出日時 2021-10-22 22:31:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,072 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 85,808 KB
実行使用メモリ 10,460 KB
最終ジャッジ日時 2023-10-24 13:49:55
合計ジャッジ時間 4,010 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	int Q;
	cin >> Q;
	while (Q--) {
		int N;
		cin >> N;
		vector<vector<int> > G(N);
		for (int i = 0; i < N - 1; ++i) {
			int a, b;
			cin >> a >> b; --a, --b;
			G[a].push_back(b);
			G[b].push_back(a);
		}
		function<vector<int>(int)> shortest_path = [&](int pos) {
			queue<int> que;
			que.push(pos);
			vector<int> resdist(N, -1);
			resdist[pos] = 0;
			while (!que.empty()) {
				int u = que.front(); que.pop();
				for (int i : G[u]) {
					if (resdist[i] == -1) {
						resdist[i] = resdist[u] + 1;
						que.push(i);
					}
				}
			}
			return resdist;
		};
		vector<int> v0 = shortest_path(0);
		int p1 = max_element(v0.begin(), v0.end()) - v0.begin();
		vector<int> v1 = shortest_path(p1);
		int diameter = *max_element(v1.begin(), v1.end());
		int max_degree = 0;
		for (int i = 0; i < N; ++i) {
			max_degree = max(max_degree, int(G[i].size()));
		}
		cout << (diameter <= max_degree ? "Yes" : "No") << endl;
	}
	return 0;
}
0