結果

問題 No.1424 Ultrapalindrome
ユーザー outlineoutline
提出日時 2021-03-12 22:55:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,257 bytes
コンパイル時間 1,721 ms
コンパイル使用メモリ 139,128 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-04-22 14:22:43
合計ジャッジ時間 3,344 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 44 ms
7,808 KB
testcase_10 AC 45 ms
7,936 KB
testcase_11 AC 28 ms
6,528 KB
testcase_12 AC 39 ms
7,424 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 33 ms
6,784 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 24 ms
6,144 KB
testcase_17 AC 30 ms
6,784 KB
testcase_18 AC 19 ms
5,504 KB
testcase_19 AC 33 ms
6,656 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 28 ms
6,016 KB
testcase_22 AC 17 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 41 ms
7,168 KB
testcase_27 WA -
testcase_28 AC 37 ms
11,904 KB
testcase_29 WA -
testcase_30 AC 28 ms
9,488 KB
testcase_31 AC 31 ms
9,356 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;
                dfs(dfs, s);
                break;
            }
        }
        int d = -1;
        for(int i = 0; i < N; ++i){
            if(g[i].size() == 1){
                if(d == -1) d = dist[i];
                else if(d != dist[i]){
                    cout << "No\n";
                    return 0;
                }
            }
        }
        cout << "Yes\n";
    }

    return 0;
}
0