結果

問題 No.1424 Ultrapalindrome
ユーザー kappybarkappybar
提出日時 2021-03-12 22:13:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,984 bytes
コンパイル時間 1,872 ms
コンパイル使用メモリ 169,092 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-22 13:14:57
合計ジャッジ時間 4,317 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 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 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 80 ms
7,808 KB
testcase_10 AC 73 ms
7,936 KB
testcase_11 AC 48 ms
6,528 KB
testcase_12 AC 61 ms
7,552 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 47 ms
6,912 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 37 ms
6,016 KB
testcase_17 AC 44 ms
6,656 KB
testcase_18 AC 29 ms
5,504 KB
testcase_19 AC 49 ms
6,784 KB
testcase_20 AC 12 ms
5,376 KB
testcase_21 AC 39 ms
6,016 KB
testcase_22 AC 24 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 56 ms
7,296 KB
testcase_27 WA -
testcase_28 AC 66 ms
8,704 KB
testcase_29 AC 70 ms
12,032 KB
testcase_30 AC 52 ms
9,464 KB
testcase_31 AC 66 ms
9,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;



int main(){
    int n;
    cin >> n;
    vector<vector<int>> G(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a;--b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    bool ok = true;
    int one = 0;
    rep(i,n){
        if((int)G[i].size() == 1){
            ++one;
        }
        else if((int)G[i].size() != 2){
            ok = false;
        }
    }
    if(one == 2 && ok){
        cout << "Yes" << endl;
        return 0;
    }
    vector<int> dis(n,0);
    auto dfs = [&](auto&& dfs,int i,int p,int c) -> void{
        dis[i] = c;
        for(int j:G[i]){
            if(j == p) continue;
            dfs(dfs,j,i,c + 1);
        }
        return;
    };
    dfs(dfs,0,-1,0);
    int ma = 0,ind = -1;
    rep(i,n){
        if(ma < dis[i]){
            ma = dis[i];
            ind = i;
        }
    }
    dfs(dfs,ind,-1,0);
    ma = 0;
    rep(i,n) chmax(ma,dis[i]);

    if(ma % 2 == 1){
        cout << "No" << endl;
        return 0;
    }

    rep(i,n){
        if((int)G[i].size() == 1){
            if(dis[i] != ma && dis[i] != 0){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;

    /*
    ind = -1;
    rep(i,n){
        if(dis[i] == (ma / 2)){
            ind = i;
            break;
        }
    }
    dfs(dfs,ind,-1,0);
    rep(i,n){
        if((int)G[i].size() == 1){
            if(dis[i] != ma / 2){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    */
    return 0;
}
0