結果

問題 No.1424 Ultrapalindrome
ユーザー tkmst201
提出日時 2021-05-02 21:26:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,924 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 200,964 KB
最終ジャッジ日時 2025-01-21 06:00:57
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:22:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |                 scanf("%d %d", &a, &b);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

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};
		}
		return {dep + 1, 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