結果

問題 No.1424 Ultrapalindrome
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-12 22:00:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,101 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 201,704 KB
実行使用メモリ 53,248 KB
最終ジャッジ日時 2024-04-22 12:57:10
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 46 ms
5,888 KB
testcase_19 AC 76 ms
6,944 KB
testcase_20 AC 18 ms
5,376 KB
testcase_21 AC 66 ms
7,424 KB
testcase_22 AC 38 ms
5,376 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 17 ms
5,376 KB
testcase_25 AC 15 ms
5,376 KB
testcase_26 AC 96 ms
7,680 KB
testcase_27 WA -
testcase_28 AC 150 ms
53,216 KB
testcase_29 WA -
testcase_30 AC 82 ms
12,360 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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))
    ;
  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