結果

問題 No.1424 Ultrapalindrome
ユーザー Example0911Example0911
提出日時 2021-03-17 13:32:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,179 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 203,408 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-04-27 00:02:38
合計ジャッジ時間 4,575 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 35 ms
7,552 KB
testcase_10 AC 37 ms
7,552 KB
testcase_11 AC 22 ms
6,272 KB
testcase_12 AC 30 ms
7,296 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 25 ms
6,656 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 20 ms
6,016 KB
testcase_17 AC 26 ms
6,400 KB
testcase_18 AC 18 ms
5,504 KB
testcase_19 AC 29 ms
6,784 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 23 ms
5,888 KB
testcase_22 AC 15 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 36 ms
7,296 KB
testcase_27 AC 47 ms
9,088 KB
testcase_28 AC 30 ms
8,796 KB
testcase_29 AC 38 ms
15,488 KB
testcase_30 AC 28 ms
9,480 KB
testcase_31 AC 30 ms
9,612 KB
権限があれば一括ダウンロードができます

ソースコード

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