結果

問題 No.1719 Tree and Permutation
ユーザー square1001square1001
提出日時 2021-10-22 22:52:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,061 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 85,732 KB
実行使用メモリ 10,516 KB
最終ジャッジ日時 2023-10-24 14:18:38
合計ジャッジ時間 4,220 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 degree1 = 0;
		for (int i = 0; i < N; ++i) {
			if (G[i].size() == 1) {
				++degree1;
			}
		}
		cout << (diameter < degree1 ? "Yes" : "No") << endl;
	}
	return 0;
}
0