結果

問題 No.1424 Ultrapalindrome
ユーザー 🍮かんプリン
提出日時 2021-03-13 18:27:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,315 bytes
コンパイル時間 1,804 ms
コンパイル使用メモリ 173,100 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-10-15 08:52:01
合計ジャッジ時間 4,298 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 WA * 1 RE * 1
権限があれば一括ダウンロードができます

ソースコード

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