結果

問題 No.1424 Ultrapalindrome
ユーザー se1ka2se1ka2
提出日時 2021-03-12 22:02:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 3,649 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 81,240 KB
実行使用メモリ 15,328 KB
最終ジャッジ日時 2024-04-22 17:07:53
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 57 ms
10,368 KB
testcase_10 AC 59 ms
10,368 KB
testcase_11 AC 41 ms
8,448 KB
testcase_12 AC 56 ms
9,984 KB
testcase_13 AC 16 ms
5,504 KB
testcase_14 AC 47 ms
8,832 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 34 ms
7,680 KB
testcase_17 AC 41 ms
8,576 KB
testcase_18 AC 34 ms
7,808 KB
testcase_19 AC 55 ms
10,368 KB
testcase_20 AC 13 ms
5,376 KB
testcase_21 AC 42 ms
9,088 KB
testcase_22 AC 27 ms
7,040 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 11 ms
5,376 KB
testcase_26 AC 63 ms
11,648 KB
testcase_27 AC 75 ms
12,416 KB
testcase_28 AC 65 ms
12,160 KB
testcase_29 AC 75 ms
15,328 KB
testcase_30 AC 55 ms
13,492 KB
testcase_31 AC 70 ms
13,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

struct Tree
{
    int n;
    int r;
    int l;
    std::vector<std::vector<int>> prit;
    std::vector<std::vector<int>> t;
    std::vector<int> par;
    std::vector<int> dpt;
    std::vector<int> tour;
    std::vector<std::vector<int>> appear_on_tour;
    std::vector<std::vector<int>> anc;
    
    Tree(){}
    
    Tree(int n, int r) : n(n), r(r){
        prit.resize(n);
        t.resize(n);
        par.resize(n);
        dpt.resize(n);
    }
    
    void add_edge(int u, int v){
        prit[u].push_back(v);
        prit[v].push_back(u);
    }
    
    void build(int r_){
        r = r_;
        for(int i = 0; i < n; i++) t[i].clear();
        for(int i = 0; i < n; i++) dpt[i] = -1;
        std::queue<int> que;
        par[r] = -1;
        dpt[r] = 0;
        que.push(r);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(int v : prit[u]){
                if(dpt[v] == -1){
                    t[u].push_back(v);
                    par[v] = u;
                    dpt[v] = dpt[u] + 1;
                    que.push(v);
                }
            }
        }
    }
    
/*
    std::vector<int> toposort(){
        std::vector<std::pair<int, int>> p(n);
        for(int i = 0; i < n; i++) p[i] = std::pair<int, int>(dpt[i], i);
        sort(p.begin(), p.end(), std::greater<std::pair<int, int>>());
        std::vector<int> res(n);
        for(int i = 0; i < n; i++) res[i] = p[i].second;
        return res;
    }
*/
    
    void dfs(int u, int &k){
        appear_on_tour[u].push_back(k);
        tour[k++] = u;
        for(int v : t[u]){
            dfs(v, k);
            appear_on_tour[u].push_back(k);
            tour[k++] = u;
        }
    }
    
    void build_euler_tour(){
        tour.resize(n * 2 - 1);
        appear_on_tour.resize(n);
        int k = 0;
        dfs(0, k);
    }
    
    void build_lca(){
        for(l = 1; l <= n; l++){
            if((1 << (l - 1)) >= n) break;
        }
        anc.resize(l);
        for(int j = 0; j < l; j++) anc[j].resize(n);
        for(int i = 0; i < n; i++) anc[0][i] = par[i];
        for(int j = 1; j < l; j++){
            for(int i = 0; i < n; i++){
                if(anc[j - 1][i] == -1) anc[j][i] = -1;
                else anc[j][i] = anc[j - 1][anc[j - 1][i]];
            }
        }
    }
    
    int lca(int u, int v){
        if(dpt[u] < dpt[v]) std::swap(u, v);
        int dpt_diff = dpt[u] - dpt[v];
        for(int j = l - 1; j >= 0; j--){
            if((dpt_diff >> j) & 1) u = anc[j][u];
        }
        if(u == v) return u;
        for(int j = l - 1; j >= 0; j--){
            if(anc[j][u] != anc[j][v]){
                u = anc[j][u];
                v = anc[j][v];
            }
        }
        return anc[0][u];
    }
};

int main()
{
    int n;
    cin >> n;
    Tree t(n, 0);
    int d[100005]{0};
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        d[a]++; d[b]++;
        t.add_edge(a, b);
    }
    int r = -1;
    for(int i = 0; i < n; i++){
        if(d[i] >= 3){
            if(r == -1) r = i;
            else{
                cout << "No" << endl;
                return 0;
            }
        }
    }
    if(r == -1){
        cout << "Yes" << endl;
        return 0;
    }
    t.build(r);
    int s = -1;
    for(int i = 0; i < n; i++){
        if((int)t.t[i].size() == 0){
            if(s == -1) s = t.dpt[i];
            else if(s != t.dpt[i]){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
}
0