結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 WA -
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 AC 79 ms
7,936 KB
testcase_10 AC 82 ms
8,064 KB
testcase_11 AC 53 ms
6,944 KB
testcase_12 AC 73 ms
7,680 KB
testcase_13 AC 19 ms
6,944 KB
testcase_14 AC 60 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 44 ms
6,940 KB
testcase_17 AC 54 ms
6,944 KB
testcase_18 AC 35 ms
6,944 KB
testcase_19 AC 64 ms
6,940 KB
testcase_20 AC 14 ms
6,944 KB
testcase_21 AC 51 ms
6,940 KB
testcase_22 AC 30 ms
6,940 KB
testcase_23 AC 6 ms
6,944 KB
testcase_24 AC 13 ms
6,940 KB
testcase_25 AC 12 ms
6,944 KB
testcase_26 AC 80 ms
7,424 KB
testcase_27 WA -
testcase_28 AC 72 ms
8,704 KB
testcase_29 WA -
testcase_30 AC 58 ms
9,588 KB
testcase_31 AC 70 ms
9,456 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;
    }

    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