結果

問題 No.1424 Ultrapalindrome
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-12 22:02:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 2,150 ms
コンパイル使用メモリ 206,796 KB
実行使用メモリ 48,460 KB
最終ジャッジ日時 2023-08-04 19:03:03
合計ジャッジ時間 4,811 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 57 ms
8,068 KB
testcase_10 AC 57 ms
8,012 KB
testcase_11 AC 39 ms
6,476 KB
testcase_12 AC 52 ms
7,792 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 42 ms
6,696 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 34 ms
6,108 KB
testcase_17 AC 39 ms
6,692 KB
testcase_18 AC 40 ms
6,044 KB
testcase_19 AC 65 ms
7,012 KB
testcase_20 AC 17 ms
4,380 KB
testcase_21 AC 56 ms
7,000 KB
testcase_22 AC 35 ms
4,880 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 16 ms
4,416 KB
testcase_25 AC 14 ms
4,380 KB
testcase_26 AC 80 ms
7,520 KB
testcase_27 AC 105 ms
10,596 KB
testcase_28 AC 141 ms
48,460 KB
testcase_29 AC 140 ms
48,384 KB
testcase_30 AC 76 ms
12,040 KB
testcase_31 AC 65 ms
9,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int n, base;
vector<vector<int>> g;
vector<int> mind, maxd;

bool solve();
void dfs(int now = 0, int par = -1);
bool reroot(int now = 0, int par = -1);

int main() {
  cin >> n;
  g.resize(n);
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    g[--a].push_back(--b);
    g[b].push_back(a);
  }
  if (solve())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

bool solve() {
  mind.resize(n);
  maxd.resize(n);
  for (int i = 0; i < n; ++i)
    if (g[i].size() == 1) {
      dfs(i, -1);
      base = mind[i];
      return reroot(i, -1);
    }
  return 0;
}

void dfs(int now, int par) {
  if (par >= 0 && g[now].size() == 1) {
    mind[now] = maxd[now] = 0;
    return;
  }
  mind[now] = n + 1;
  maxd[now] = -1;
  for (auto to : g[now])
    if (to != par) {
      dfs(to, now);
      mind[now] = min(mind[now], mind[to] + 1);
      maxd[now] = max(maxd[now], maxd[to] + 1);
    }
}

bool reroot(int now, int par) {
  int tmpmin = mind[now], tmpmax = maxd[now];
  mind[now] = n + 1, maxd[now] = -1;
  vector<int> minvl, minvr, maxvl, maxvr;
  int len = g[now].size();
  minvl.push_back(n + 1);
  minvr.push_back(n + 1);
  maxvl.push_back(-1);
  maxvr.push_back(-1);
  for (int i = 0; i < len; ++i) {
    int to = g[now][i];
    int d = min(minvl.back(), mind[to] + 1);
    minvl.push_back(d);
    d = max(maxvl.back(), maxd[to] + 1);
    maxvl.push_back(d);
    to = g[now][len - 1 - i];
    d = min(minvr.back(), mind[to] + 1);
    minvr.push_back(d);
    d = max(maxvr.back(), maxd[to] + 1);
    maxvr.push_back(d);
  }
  int res = 1;
  if (g[now].size() == 1 &&
      (minvl.back() != maxvl.back() || minvl.back() != base))
    res = 0;
  else {
    for (int i = 0; i < len; ++i) {
      int to = g[now][i];
      if (to == par) continue;
      if (len != 1) {
        mind[now] = min(minvl[i], minvr[len - 1 - i]);
        maxd[now] = max(maxvl[i], maxvr[len - 1 - i]);
      } else
        mind[now] = maxd[now] = 0;
      res &= reroot(to, now);
    }
  }
  mind[now] = tmpmin, maxd[now] = tmpmax;
  return res;
}
0