結果

問題 No.1424 Ultrapalindrome
ユーザー tnakao0123tnakao0123
提出日時 2021-03-14 19:38:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 95,972 KB
実行使用メモリ 10,308 KB
最終ジャッジ日時 2024-04-24 02:06:40
合計ジャッジ時間 2,666 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 2 ms
6,016 KB
testcase_02 AC 3 ms
6,016 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 2 ms
6,016 KB
testcase_05 AC 2 ms
6,016 KB
testcase_06 AC 3 ms
6,144 KB
testcase_07 AC 3 ms
6,016 KB
testcase_08 AC 3 ms
6,144 KB
testcase_09 AC 28 ms
8,576 KB
testcase_10 AC 27 ms
8,448 KB
testcase_11 AC 19 ms
7,808 KB
testcase_12 AC 24 ms
8,448 KB
testcase_13 AC 10 ms
6,912 KB
testcase_14 AC 21 ms
8,064 KB
testcase_15 AC 3 ms
5,888 KB
testcase_16 AC 17 ms
7,552 KB
testcase_17 AC 20 ms
7,936 KB
testcase_18 AC 17 ms
7,808 KB
testcase_19 AC 24 ms
8,448 KB
testcase_20 AC 7 ms
6,656 KB
testcase_21 AC 19 ms
8,192 KB
testcase_22 AC 13 ms
7,376 KB
testcase_23 AC 5 ms
6,528 KB
testcase_24 AC 7 ms
6,656 KB
testcase_25 AC 7 ms
6,784 KB
testcase_26 AC 28 ms
9,088 KB
testcase_27 AC 37 ms
9,216 KB
testcase_28 AC 30 ms
9,216 KB
testcase_29 AC 31 ms
10,308 KB
testcase_30 AC 26 ms
10,296 KB
testcase_31 AC 25 ms
9,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1424.cc:  No.1424 Ultrapalindrome - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_N];
int ps[MAX_N], ds[MAX_N], cis[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    nbrs[a].push_back(b);
    nbrs[b].push_back(a);
  }

  int ln = 0, rt = -1;
  for (int u = 0; u < n; u++) {
    int szu = nbrs[u].size();
    if (szu == 1) ln++;
    else if (szu > 2) {
      if (rt < 0) rt = u;
      else { puts("No"); return 0; }
    }
  }
  if (ln == 2) { puts("Yes"); return 0; }

  int ld = -1;
  ps[rt] = -1, ds[rt] = 0;
  for (int u = rt; u >= 0;) {
    vi &nbru = nbrs[u];
    int &ciu = cis[u];
    if (ciu < nbru.size() && nbru[ciu] == ps[u]) ciu++;
    if (ciu < nbru.size()) {
      int v = nbru[ciu++];
      ps[v] = u, ds[v] = ds[u] + 1;
      if (nbrs[v].size() == 1) {
	if (ld < 0) ld = ds[v];
	else if (ld != ds[v]) { puts("No"); return 0; }
      }
      u = v;
    }
    else {
      u = ps[u];
    }
  }

  puts("Yes");
  return 0;
}
0