結果

問題 No.1424 Ultrapalindrome
ユーザー tkmst201tkmst201
提出日時 2021-05-02 21:23:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,946 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 207,008 KB
実行使用メモリ 12,412 KB
最終ジャッジ日時 2023-09-28 12:07:12
合計ジャッジ時間 4,999 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 47 ms
7,608 KB
testcase_10 AC 46 ms
7,856 KB
testcase_11 AC 31 ms
6,408 KB
testcase_12 AC 44 ms
7,248 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 33 ms
6,892 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 26 ms
5,988 KB
testcase_17 AC 34 ms
6,544 KB
testcase_18 AC 20 ms
5,496 KB
testcase_19 AC 45 ms
6,600 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 35 ms
5,940 KB
testcase_22 AC 18 ms
5,092 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 7 ms
4,380 KB
testcase_25 AC 7 ms
4,380 KB
testcase_26 AC 63 ms
7,096 KB
testcase_27 AC 57 ms
9,184 KB
testcase_28 AC 40 ms
12,412 KB
testcase_29 AC 40 ms
12,372 KB
testcase_30 AC 29 ms
9,468 KB
testcase_31 AC 34 ms
10,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

int main() {
	int N;
	cin >> N;
	vector<vector<int>> g(N);
	REP(i, N - 1) {
		int a, b;
		scanf("%d %d", &a, &b);
		--a; --b;
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	
	auto dfs = [&](auto && self, auto & d, int u, int p) -> void {
		for (int v : g[u]) if (v != p) {
			d[v] = d[u] + 1;
			self(self, d, v, u);
		}
	};
	vector<int> d(N, -1);
	d[0] = 0;
	dfs(dfs, d, 0, -1);
	
	int mxu = max_element(ALL(d)) - d.begin();
	d.assign(N, -1);
	d[mxu] = 0;
	dfs(dfs, d, mxu, -1);
	
	int diam = *max_element(ALL(d));
	vector<int> cent;
	
	REP(i, N) {
		if (diam % 2 == 0 && d[i] != diam / 2) continue;
		if (diam % 2 == 1 && !(d[i] == diam / 2 || d[i] == (diam + 1) / 2)) continue;
		cent.emplace_back(i);
	}
	
	auto solve = [&](auto && self, int u, int p) -> pii { // max depth, child cnt
		int dep = -1, cnt = 0;
		for (int v : g[u]) {
			if (v == p) continue;
			++cnt;
			auto [cd, cc] = self(self, v, u);
			if (cc > 1) return {INF, INF};
			if (dep == -1) dep = cd;
			else if (dep != cd) return {INF, INF};
		}
		if (dep == -1) dep = 0;
		return {dep, cnt};
	};
	
	puts([&]() -> bool {
		if (cent.size() == 1) return solve(solve, cent[0], -1).second < INF;
		int u = cent[0], v = cent[1];
		if (solve(solve, u, v).second > 1) return false;
		if (solve(solve, v, u).second > 1) return false;
		return true;
	}() ? "Yes": "No");
}
0