結果

問題 No.1424 Ultrapalindrome
ユーザー norikamenorikame
提出日時 2021-03-12 23:51:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,298 bytes
コンパイル時間 2,398 ms
コンパイル使用メモリ 203,048 KB
実行使用メモリ 10,612 KB
最終ジャッジ日時 2024-04-22 17:17:27
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 61 ms
7,424 KB
testcase_10 AC 60 ms
7,424 KB
testcase_11 AC 43 ms
6,944 KB
testcase_12 AC 63 ms
7,296 KB
testcase_13 AC 16 ms
6,940 KB
testcase_14 AC 49 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 38 ms
6,944 KB
testcase_17 AC 45 ms
6,940 KB
testcase_18 AC 31 ms
6,940 KB
testcase_19 AC 51 ms
6,944 KB
testcase_20 AC 12 ms
6,940 KB
testcase_21 AC 37 ms
6,944 KB
testcase_22 AC 24 ms
6,944 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 11 ms
6,944 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 65 ms
7,296 KB
testcase_27 AC 87 ms
8,704 KB
testcase_28 AC 69 ms
8,576 KB
testcase_29 AC 72 ms
9,088 KB
testcase_30 AC 58 ms
10,612 KB
testcase_31 AC 70 ms
10,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
using ull = unsigned long long;
#define v(t) vector<t>
#define p(t) pair<t, t>
#define p2(t, s) pair<t, s>
#define vp(t) v(p(t))

#define rep(i, n) for (int i=0,i##_len=((int)(n)); i<i##_len; ++i)
#define rep2(i, a, n) for (int i=((int)(a)),i##_len=((int)(n)); i<=i##_len; ++i)
#define repr(i, n) for (int i=((int)(n)-1); i>=0; --i)
#define rep2r(i, a, n) for (int i=((int)(n)),i##_len=((int)(a)); i>=i##_len; --i)

#define repi(itr, c) for (__typeof((c).begin()) itr=(c).begin(); itr!=(c).end(); ++itr)
#define repir(itr, c) for (__typeof((c).rbegin()) itr=(c).rbegin(); itr!=(c).rend(); ++itr)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define eb emplace_back

#define INF (1e9)
#define LINF (1e18)
#define PI (acos(-1))
#define EPS (1e-7)
#define DEPS (1e-10)

int main(){
    int n;
    cin >> n;
    v(v(int)) g(n);
    rep(i, n-1) {
        int ai, bi;
        cin >> ai >> bi;
        --ai; --bi;
        g[ai].pb(bi);
        g[bi].pb(ai);
    }
    bool ok = false;
    int leaf = 0, cent = -1;
    rep(i, n) if (sz(g[i]) == 1) ++leaf;
    if (leaf <= 2) ok = true;
    else {
        rep(i, n) if (sz(g[i]) == leaf) cent = i;
        if (cent != -1) {
            queue<int> que;
            v(int) dist(n, (int)(INF));
            que.push(cent);
            dist[cent] = 0;
            v(int) dlst;
            while (!que.empty()) {
                int pi = que.front(); que.pop();
                int d = dist[pi], nd = d+1;
                if (sz(g[pi])==1 && pi!=cent) dlst.pb(d);
                rep(j, sz(g[pi])) {
                    int to = g[pi][j];
                    if (dist[to] <= nd) continue;
                    que.push(to);
                    dist[to] = nd;
                }
            }
            bool ok2 = true;
            rep(i, sz(dlst)-1) if (dlst[i] != dlst[i+1]) ok2 = false;
            if (ok2) ok = true;
        }
    }
    if (ok) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}
0