結果

問題 No.1424 Ultrapalindrome
ユーザー dekomori_sanaedekomori_sanae
提出日時 2021-03-12 21:52:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,784 bytes
コンパイル時間 1,078 ms
コンパイル使用メモリ 96,576 KB
実行使用メモリ 13,444 KB
最終ジャッジ日時 2024-04-22 12:45:47
合計ジャッジ時間 3,011 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 66 ms
11,008 KB
testcase_10 AC 65 ms
11,008 KB
testcase_11 AC 44 ms
8,704 KB
testcase_12 AC 60 ms
10,496 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 50 ms
9,216 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 40 ms
7,936 KB
testcase_17 AC 47 ms
8,832 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 6 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 82 ms
13,360 KB
testcase_28 AC 69 ms
12,544 KB
testcase_29 AC 68 ms
12,544 KB
testcase_30 AC 54 ms
13,444 KB
testcase_31 AC 66 ms
13,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

int main() {
    ll n;
    cin >> n;
    
    vector<vector<P>> G(n);
    vl deg(n);
    rep(i, n-1) {
        ll a, b;
        cin >> a >> b;
        a--;  b--;
        G[a].emplace_back(make_pair(b, 1));
        G[b].emplace_back(make_pair(a, 1));
        deg[a]++;  deg[b]++;
    }
    
    vl dist1(n, -1);  // dist1[v] : 頂点vの頂点0からの距離
    queue<ll> Q1; 
    dist1[0] = 0;
    Q1.push(0);
    while(!Q1.empty()) {
        ll v = Q1.front();
        Q1.pop();
        for(auto p : G[v]) {
            ll u = p.first;
            ll cost = p.second; 
            if(dist1[u] != -1) {
                continue;
            }
            dist1[u] = dist1[v] + cost;
            Q1.push(u);
        }
    }
    
    ll vertex1 = 0;  // 頂点0からの距離が最大の頂点
    rep(v, n) {
        if(dist1[vertex1] < dist1[v]) {
            vertex1 = v;
        }
    }
    
    vl dist2(n, -1);  // dist2[v] : 頂点vの頂点vertex1からの距離
    queue<ll> Q2;
    dist2[vertex1] = 0;
    Q2.push(vertex1);
    while(!Q2.empty()) {
        ll v = Q2.front();
        Q2.pop();
        for(auto p : G[v]) {
            ll u = p.first;
            ll cost = p.second; 
            if(dist2[u] != -1) {
                continue;
            }
            dist2[u] = dist2[v] + cost;
            Q2.push(u);
        }
    }
 
    ll vertex2 = vertex1;  // 頂点vertex1からの距離が最大の頂点
    ll TreeDiameter = 0;  // 木の直径
    rep(v, n) {
        if(dist2[vertex2] < dist2[v]) {
            vertex2 = v;
            TreeDiameter = dist2[v];
        }
    }

    bool ok = false;
    
    if(TreeDiameter == n-1) {
        ok = true;
    }

    ll cnt1 = 0, cnt2 = 0;
    rep(v, n) {
        if(deg[v] == n-1) {
            cnt1++;
        }
        else if(deg[v] == 1) {
            cnt2++;
        }
    }
    if(cnt1 == 1 && cnt2 == n-1) {
        ok = true;
    }

    if(ok) {
        out("Yes");
    }
    else {
        out("No");
    }
    
    re0;
}
0