結果

問題 No.1424 Ultrapalindrome
ユーザー kappybarkappybar
提出日時 2021-03-12 22:16:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,953 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 169,740 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-22 13:18:01
合計ジャッジ時間 3,866 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 70 ms
7,808 KB
testcase_10 AC 70 ms
7,680 KB
testcase_11 AC 48 ms
6,528 KB
testcase_12 AC 69 ms
7,680 KB
testcase_13 AC 18 ms
5,376 KB
testcase_14 AC 54 ms
6,784 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 42 ms
5,888 KB
testcase_17 AC 51 ms
6,784 KB
testcase_18 AC 33 ms
5,632 KB
testcase_19 AC 55 ms
6,656 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 45 ms
5,888 KB
testcase_22 AC 29 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 11 ms
5,376 KB
testcase_26 AC 69 ms
7,296 KB
testcase_27 WA -
testcase_28 AC 70 ms
8,792 KB
testcase_29 AC 77 ms
12,032 KB
testcase_30 AC 58 ms
9,460 KB
testcase_31 AC 70 ms
9,332 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;
            }
        }
    }

    
    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