結果

問題 No.1424 Ultrapalindrome
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-03-13 18:27:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,315 bytes
コンパイル時間 1,804 ms
コンパイル使用メモリ 169,228 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-04-23 08:57:08
合計ジャッジ時間 3,995 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,948 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 58 ms
7,552 KB
testcase_10 AC 56 ms
7,552 KB
testcase_11 AC 42 ms
6,940 KB
testcase_12 AC 55 ms
7,296 KB
testcase_13 AC 15 ms
6,940 KB
testcase_14 AC 45 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 35 ms
6,940 KB
testcase_17 AC 42 ms
6,940 KB
testcase_18 AC 29 ms
6,940 KB
testcase_19 AC 49 ms
6,944 KB
testcase_20 AC 12 ms
6,940 KB
testcase_21 AC 40 ms
6,940 KB
testcase_22 AC 25 ms
6,944 KB
testcase_23 AC 5 ms
6,944 KB
testcase_24 AC 11 ms
6,940 KB
testcase_25 AC 9 ms
6,940 KB
testcase_26 AC 60 ms
7,168 KB
testcase_27 WA -
testcase_28 RE -
testcase_29 AC 72 ms
13,440 KB
testcase_30 AC 51 ms
9,208 KB
testcase_31 AC 66 ms
9,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<vector<int>> g;
int leaf1 = -1, leaf2 = -1;

void dfs1(int v,int p = -1) {
    bool leaf = true;
    for(int u : g[v]) {
        if (u == p) continue;
        leaf = false;
        dfs1(u,v);
    }
    if (leaf) {
        if (leaf1 == -1) {
            leaf1 = v;
        }
        else if (leaf2 == -1) {
            leaf2 = v;
        }
    }
}

bool ok;

int dfs2(int v, int p=-1) {
    int tmp = -1;
    for(int u : g[v]) {
        if (u == p) continue;
        int ret = dfs2(u,v);
        if (ret == -1) return -1;
        // cout << v << " " << ret << endl;
        if (tmp == -1) {
            tmp = ret;
        }
        else if (ret != tmp) {
            ok = false;
            return -1;
        }
    }
    return tmp + 1;
};

int main() {
    int n;cin >> n;
    g.resize(n);
    for(int i = 0; i < n-1;i++) {
        int a,b;cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs1(0);
    // cout << leaf1 << " " << leaf2 << endl;
    ok = true;
    dfs2(leaf1);
    if (!ok) {
        cout << "No" << endl;
        return 0;
    }
    ok = true;
    dfs2(leaf2);
    if (!ok) {
        cout << "No" << endl;
    }
    else {
        cout << "Yes" << endl;
    }
    return 0;
}
0