結果

問題 No.1424 Ultrapalindrome
ユーザー TrpFrogTrpFrog
提出日時 2021-03-12 22:29:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 3,202 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 207,876 KB
実行使用メモリ 17,616 KB
最終ジャッジ日時 2023-08-04 19:08:27
合計ジャッジ時間 4,956 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 72 ms
14,284 KB
testcase_10 AC 74 ms
14,276 KB
testcase_11 AC 49 ms
11,096 KB
testcase_12 AC 66 ms
13,932 KB
testcase_13 AC 17 ms
6,244 KB
testcase_14 AC 54 ms
11,796 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 40 ms
10,096 KB
testcase_17 AC 55 ms
11,356 KB
testcase_18 AC 39 ms
8,328 KB
testcase_19 AC 65 ms
11,292 KB
testcase_20 AC 14 ms
5,112 KB
testcase_21 AC 50 ms
9,604 KB
testcase_22 AC 30 ms
7,276 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 13 ms
5,040 KB
testcase_25 AC 12 ms
4,644 KB
testcase_26 AC 76 ms
12,700 KB
testcase_27 AC 104 ms
17,464 KB
testcase_28 AC 70 ms
17,616 KB
testcase_29 AC 77 ms
16,920 KB
testcase_30 AC 90 ms
17,308 KB
testcase_31 AC 101 ms
16,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define pb push_back
#define eb emplace_back
#define unused [[maybe_unused]]
#define tempT template<class T>
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl
#define ans_exit(x) { cout << x << endl; exit(0); }
#define ans_return(x) { cout << x << endl; return; }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define r_rep(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++)
#define r_reps(i, n, m) for (ll i = (ll)(m) - 1; i >= (ll)(n); i--)
#define debugarr(a) {cerr << #a << ":"; \
                    for(auto _e:a){cerr << " " <<_e;} cerr << endl;}

using ll   unused = long long;
using ld   unused = long double;
using vl   unused = vector<ll>;
using vvl  unused = vector<vl>;
using vvvl unused = vector<vvl>;
using lp   unused = pair<ll, ll>;
using lmap unused = map<int, int>;

unused constexpr ll MOD = 1e9 + 7;
unused constexpr ll INF = 1 << 29;
unused constexpr ll LINF = 1LL << 61;
unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1};
unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1};
unused inline bool bit(ll b, ll i) { return b & (1LL << i); }
unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; }
unused inline void modadd(ll &a, ll b, ll m = MOD) { a = mod(a + b, m); }
unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); }
unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); }
tempT unused inline bool in_range(T a, T x, T b) { return a <= x && x < b; }
unused inline bool in_range(ll x, ll b) { return in_range(0LL, x, b); }
tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; }
tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; }

int count_leafs(vector<set<int>> &graph) {
    int leafs = 0;
    rep(i, graph.size()) {
        if((int)graph[i].size() == 1) leafs++;
    }
    return leafs;
}

bool is_path(vector<set<int>> &graph) {
    return count_leafs(graph) == 2; 
}

int find_center(vector<set<int>> &graph) {
    int leafs = count_leafs(graph);
    int center = INF;
    rep(i, graph.size()) {
        if((int)graph[i].size() == leafs) {
            if(center == INF) {
                center = i;
            } else {
                center = INF;
                break;
            }
        }
    }
    if(center == INF) ans_exit("No");
    return center;
}

int depth(vector<set<int>> &graph, int i, int prev, int d) {
    for(int nxt : graph[i]) {
        if(nxt == prev) continue;
        return depth(graph, nxt, i, d + 1);
    }
    return d;
}

int main() {
    int n; cin >> n;
    vector<set<int>> graph(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].insert(b);
        graph[b].insert(a);
    }

    if(is_path(graph)) ans_exit("Yes");
    
    int center = find_center(graph);
    set<int> d;
    for(int i : graph[center]) {
        d.insert(depth(graph, i, center, 0));
    }

    cout << ((int)d.size() == 1 ? "Yes" : "No") << endl;
}
0