結果

問題 No.1424 Ultrapalindrome
ユーザー mdstoymdstoy
提出日時 2021-03-12 22:40:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 3,040 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 201,248 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2024-04-22 17:13:57
合計ジャッジ時間 3,915 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 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 32 ms
7,424 KB
testcase_10 AC 31 ms
7,424 KB
testcase_11 AC 22 ms
6,400 KB
testcase_12 AC 31 ms
7,296 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 24 ms
6,656 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 19 ms
5,888 KB
testcase_17 AC 22 ms
6,400 KB
testcase_18 AC 16 ms
5,376 KB
testcase_19 AC 27 ms
6,528 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 22 ms
6,016 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 33 ms
6,944 KB
testcase_27 AC 44 ms
8,832 KB
testcase_28 AC 33 ms
8,576 KB
testcase_29 AC 38 ms
17,920 KB
testcase_30 AC 26 ms
8,972 KB
testcase_31 AC 30 ms
8,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void prn(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << ' ';}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printvm(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i].val() << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvvm(vector<vector<T>>& v) {for (auto u : v) printvm(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n;
    cin >> n;
    vvi g(n, vi());
    int a, b;
    REP(i, n - 1) {
        cin >> a >> b;
        a--;
        b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    if (n <= 4) {
        cout << "Yes" << endl;
        return;
    }
    int c = -1;
    REP(i, n) {
        if (sz(g[i]) > 2) {
            if (c != -1) {
                cout << "No" << endl;
                return;
            } else {
                c = i;
            }
        }
    }
    if (c == -1) {
        cout << "Yes" << endl;
        return;
    }

    int ans = -1;
    function<bool(int, int, int)> dfs = [&](int c, int p, int d) {
        if (sz(g[c]) == 1) {
            if (ans != -1 and ans != d) return false;
            else {
                ans = d;
                return true;
            }
        }
        bool ret = true;
        for (auto e : g[c]) {
            if (e == p) continue;
            ret &= dfs(e, c, d + 1);
        }
        return ret;
    };

    cout << (dfs(c, -1, 0) ? "Yes" : "No") << endl;
    // cout << 100000 << endl;
    // REP(i, 99999) cout << "1 " << i + 1 << endl;
}
0