結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 91 ms
8,192 KB
testcase_10 AC 60 ms
8,320 KB
testcase_11 AC 63 ms
6,940 KB
testcase_12 AC 57 ms
7,808 KB
testcase_13 AC 17 ms
6,944 KB
testcase_14 AC 48 ms
7,040 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 55 ms
6,944 KB
testcase_17 AC 48 ms
6,940 KB
testcase_18 AC 43 ms
6,940 KB
testcase_19 AC 66 ms
6,940 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 8 ms
6,940 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 121 ms
10,880 KB
testcase_28 AC 142 ms
53,248 KB
testcase_29 AC 143 ms
53,248 KB
testcase_30 WA -
testcase_31 AC 87 ms
12,108 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();
      base = mind[i];
      break;
    }
  return reroot();
}

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);
  }
  if (g[now].size() == 1 &&
      (minvl.back() != maxvl.back() || minvl.back() != base))
    return 0;
  int res = 1;
  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