結果

問題 No.1424 Ultrapalindrome
ユーザー outline
提出日時 2021-03-12 22:59:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,431 bytes
コンパイル時間 1,557 ms
コンパイル使用メモリ 140,276 KB
最終ジャッジ日時 2025-01-19 15:33:16
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    vector<vector<int>> g(N);
    for(int i = 1; i < N; ++i){
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }

    vector<int> dist(N);
    auto dfs = [&](auto&& self, int from = 0, int d = 0, int par = -1) -> void {
        dist[from] = d;
        for(int to : g[from]){
            if(to != par)   self(self, to, d + 1, from);
        }
    };
    dfs(dfs);
    int s = max_element(begin(dist), end(dist)) - begin(dist);
    dfs(dfs, s);
    int D = *max_element(begin(dist), end(dist));

    if(D % 2 == 1){
        int leaf_cnt = 0;
        for(int i = 0; i < N; ++i){
            leaf_cnt += g[i].size() == 1;
        }
        cout << (leaf_cnt == 2 ? "Yes\n" : "No\n");
    }
    else{
        for(int i = 0; i < N; ++i){
            if(dist[i] == D / 2){
                s = i;
                break;
            }
        }
        auto dfs2 = [&](auto&& self, int from = 0, int par = -1) -> int {
            set<int> st;
            int child_cnt = 0;
            for(int to : g[from]){
                if(to != par){
                    child_cnt += 1;
                    st.emplace(self(self, to, from));
                }
            }
            if(st.size() > 1){
                cout << "No\n";
                exit(0);
            }
            return child_cnt;
        };
        dfs2(dfs2);
        cout << "Yes\n";
    }

    return 0;
}
0