結果

問題 No.1424 Ultrapalindrome
ユーザー outlineoutline
提出日時 2021-03-12 22:59:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,431 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 144,232 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-22 14:27:33
合計ジャッジ時間 3,288 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 31 ms
7,936 KB
testcase_10 AC 31 ms
7,936 KB
testcase_11 AC 23 ms
6,528 KB
testcase_12 AC 31 ms
7,552 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 25 ms
7,168 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 20 ms
6,016 KB
testcase_17 AC 24 ms
6,656 KB
testcase_18 AC 18 ms
5,504 KB
testcase_19 AC 31 ms
6,528 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 25 ms
6,144 KB
testcase_22 AC 16 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 37 ms
7,296 KB
testcase_27 AC 40 ms
9,600 KB
testcase_28 AC 32 ms
12,032 KB
testcase_29 WA -
testcase_30 AC 26 ms
9,360 KB
testcase_31 AC 27 ms
9,484 KB
権限があれば一括ダウンロードができます

ソースコード

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