結果

問題 No.1424 Ultrapalindrome
ユーザー Example0911Example0911
提出日時 2021-03-17 13:32:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,179 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 200,848 KB
最終ジャッジ日時 2025-01-19 17:54:20
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"


using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;

vector<int>dist;
void dfs(vector<vector<int>> &G, int v, int p, int d) {
	dist[v] = d;
	for (auto nv : G[v]) {
		if (nv == p)continue;
		dfs(G, nv, v, d + 1);
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	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);
	}
	int cnt = 0;
	int idx = -1;
	for (int i = 0; i < N; i++) {
		if (G[i].size() >= 3) {
			cnt++;
			idx = i;
		}
	}
	// 次数3以上の頂点が2つ以上存在する
	if (cnt >= 2) {
		cout << "No" << endl; return 0;
	}
	// 次数3以上の頂点が一つもない
	if (idx == -1) {
		cout << "Yes" << endl; return 0;
	}
	dist.resize(N);
	dfs(G, idx, -1, 0);
	set<int>st;
	for (int i = 0; i < N; i++) {
		if (G[i].size() == 1) {
			st.insert(dist[i]);
		}
	}
	// 次数3以上の頂点から与えられた木の葉までの距離がすべて等しい
	if (st.size() == 1) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}
0