結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 WA -
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
6,944 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 30 ms
6,940 KB
testcase_19 AC 47 ms
6,940 KB
testcase_20 AC 12 ms
6,940 KB
testcase_21 AC 40 ms
6,940 KB
testcase_22 AC 24 ms
6,944 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 11 ms
6,944 KB
testcase_25 AC 10 ms
6,940 KB
testcase_26 AC 60 ms
7,040 KB
testcase_27 WA -
testcase_28 RE -
testcase_29 WA -
testcase_30 AC 52 ms
9,204 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
        if (tmp == -1) {
            ret = tmp;
        }
        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);
    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