結果

問題 No.1424 Ultrapalindrome
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-12 22:02:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 2,572 ms
コンパイル使用メモリ 201,456 KB
実行使用メモリ 53,248 KB
最終ジャッジ日時 2024-04-22 17:07:44
合計ジャッジ時間 5,145 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 72 ms
8,064 KB
testcase_10 AC 72 ms
8,192 KB
testcase_11 AC 50 ms
6,656 KB
testcase_12 AC 67 ms
7,808 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 55 ms
7,040 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 42 ms
6,272 KB
testcase_17 AC 52 ms
6,912 KB
testcase_18 AC 49 ms
6,016 KB
testcase_19 AC 89 ms
6,940 KB
testcase_20 AC 19 ms
5,376 KB
testcase_21 AC 68 ms
7,424 KB
testcase_22 AC 41 ms
5,376 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 18 ms
5,376 KB
testcase_25 AC 15 ms
5,376 KB
testcase_26 AC 106 ms
7,680 KB
testcase_27 AC 147 ms
10,880 KB
testcase_28 AC 152 ms
53,120 KB
testcase_29 AC 151 ms
53,248 KB
testcase_30 AC 83 ms
12,232 KB
testcase_31 AC 70 ms
9,716 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