結果

問題 No.1424 Ultrapalindrome
ユーザー nok0nok0
提出日時 2020-12-30 22:13:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 947 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 203,056 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-22 16:55:32
合計ジャッジ時間 4,149 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 58 ms
7,552 KB
testcase_10 AC 60 ms
7,552 KB
testcase_11 AC 42 ms
6,400 KB
testcase_12 AC 59 ms
7,296 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 46 ms
6,528 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 36 ms
5,888 KB
testcase_17 AC 43 ms
6,528 KB
testcase_18 AC 30 ms
5,376 KB
testcase_19 AC 46 ms
6,272 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 38 ms
5,760 KB
testcase_22 AC 23 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 10 ms
5,376 KB
testcase_25 AC 11 ms
5,376 KB
testcase_26 AC 54 ms
7,040 KB
testcase_27 AC 77 ms
8,832 KB
testcase_28 AC 71 ms
8,704 KB
testcase_29 AC 74 ms
12,032 KB
testcase_30 AC 57 ms
9,844 KB
testcase_31 AC 68 ms
9,844 KB
権限があれば一括ダウンロードができます

ソースコード

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