結果

問題 No.1424 Ultrapalindrome
ユーザー kappybarkappybar
提出日時 2021-03-12 22:21:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 2,140 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 171,968 KB
実行使用メモリ 11,768 KB
最終ジャッジ日時 2023-08-04 19:07:17
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,388 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 57 ms
7,540 KB
testcase_10 AC 59 ms
7,860 KB
testcase_11 AC 41 ms
6,544 KB
testcase_12 AC 57 ms
7,352 KB
testcase_13 AC 15 ms
4,392 KB
testcase_14 AC 43 ms
6,760 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 34 ms
5,968 KB
testcase_17 AC 41 ms
6,428 KB
testcase_18 AC 27 ms
5,412 KB
testcase_19 AC 48 ms
6,464 KB
testcase_20 AC 12 ms
4,384 KB
testcase_21 AC 39 ms
5,940 KB
testcase_22 AC 23 ms
4,888 KB
testcase_23 AC 5 ms
4,384 KB
testcase_24 AC 10 ms
4,380 KB
testcase_25 AC 10 ms
4,384 KB
testcase_26 AC 58 ms
7,256 KB
testcase_27 AC 70 ms
9,136 KB
testcase_28 AC 59 ms
8,768 KB
testcase_29 AC 65 ms
11,768 KB
testcase_30 AC 50 ms
9,384 KB
testcase_31 AC 58 ms
9,492 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;
            }
        }
    }

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