結果

問題 No.1719 Tree and Permutation
ユーザー square1001square1001
提出日時 2021-10-22 23:00:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 452 ms / 2,000 ms
コード長 1,059 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 85,604 KB
実行使用メモリ 10,488 KB
最終ジャッジ日時 2023-10-24 14:32:23
合計ジャッジ時間 4,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 319 ms
4,348 KB
testcase_02 AC 274 ms
4,348 KB
testcase_03 AC 452 ms
10,488 KB
testcase_04 AC 406 ms
9,112 KB
testcase_05 AC 424 ms
9,728 KB
testcase_06 AC 410 ms
9,932 KB
権限があれば一括ダウンロードができます

ソースコード

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 degree1 = 0;
		for (int i = 0; i < N; ++i) {
			if (G[i].size() == 1) {
				++degree1;
			}
		}
		cout << (degree1 * 2 >= N ? "Yes" : "No") << endl;
	}
	return 0;
}
0