結果

問題 No.1424 Ultrapalindrome
ユーザー kappybarkappybar
提出日時 2021-03-12 22:21:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 2,140 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 169,720 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-22 17:11:28
合計ジャッジ時間 4,091 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 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 70 ms
7,808 KB
testcase_10 AC 72 ms
7,680 KB
testcase_11 AC 49 ms
6,528 KB
testcase_12 AC 66 ms
7,552 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 58 ms
6,784 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 43 ms
5,888 KB
testcase_17 AC 51 ms
6,656 KB
testcase_18 AC 33 ms
5,504 KB
testcase_19 AC 59 ms
6,528 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 46 ms
5,888 KB
testcase_22 AC 28 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 75 ms
7,296 KB
testcase_27 AC 88 ms
9,088 KB
testcase_28 AC 72 ms
8,704 KB
testcase_29 AC 76 ms
12,032 KB
testcase_30 AC 59 ms
9,464 KB
testcase_31 AC 71 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;
            }
        }
    }

    
    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