結果

問題 No.1424 Ultrapalindrome
ユーザー nok0
提出日時 2020-12-30 22:13:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 947 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 198,548 KB
最終ジャッジ日時 2025-01-17 08:39:17
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Graph = vector<vector<int>>;

int main() {
	int n;
	cin >> n;
	Graph G(n);
	for(int i = 0; i < n - 1; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	int center = -1;
	for(int i = 0; i < n; ++i) {
		if(int(G[i].size()) > 2) {
			if(center == -1)
				center = i;
			else {
				puts("No");
				return 0;
			}
		}
	}

	if(center == -1) {
		puts("Yes");
		return 0;
	}

	bool ok = true;

	auto dfs = [&G, &ok](auto self, auto now, auto per) -> int {
		if(int(G[now].size()) > 2) ok = false;

		int ret = 0;

		for(auto to : G[now]) {
			if(to == per) continue;
			ret += self(self, to, now) + 1;
		}

		return ret;
	};

	vector<int> result;
	for(auto to : G[center]) result.push_back(dfs(dfs, to, center));

	auto p = minmax_element(result.begin(), result.end());
	ok &= (*p.first == *p.second);

	if(ok)
		puts("Yes");
	else
		puts("No");

	return 0;
}
0