結果

問題 No.1424 Ultrapalindrome
ユーザー trineutrontrineutron
提出日時 2021-03-12 21:39:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,350 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 202,020 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-04-22 12:28:44
合計ジャッジ時間 4,550 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 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 82 ms
7,936 KB
testcase_10 AC 84 ms
7,936 KB
testcase_11 AC 53 ms
6,944 KB
testcase_12 AC 74 ms
7,552 KB
testcase_13 AC 19 ms
6,944 KB
testcase_14 AC 59 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 44 ms
6,944 KB
testcase_17 AC 56 ms
6,944 KB
testcase_18 AC 36 ms
6,940 KB
testcase_19 AC 66 ms
6,940 KB
testcase_20 AC 13 ms
6,944 KB
testcase_21 AC 48 ms
6,944 KB
testcase_22 AC 30 ms
6,944 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 12 ms
6,940 KB
testcase_25 AC 12 ms
6,940 KB
testcase_26 AC 85 ms
7,296 KB
testcase_27 WA -
testcase_28 AC 80 ms
16,896 KB
testcase_29 WA -
testcase_30 AC 59 ms
9,456 KB
testcase_31 AC 72 ms
9,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using graph = vector<vector<int>>;

void dfs(const graph &to, vector<int> &d, int v, int p)
{
    for (auto &&c : to.at(v))
    {
        if (c == p)
        {
            continue;
        }
        d.at(c) = d.at(v) + 1;
        dfs(to, d, c, v);
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    graph to(n);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }
    vector<int> d(n);
    dfs(to, d, 0, -1);
    int v = max_element(d.begin(), d.end()) - d.begin();
    d.at(v) = 0;
    dfs(to, d, v, -1);
    int dmax = *max_element(d.begin(), d.end());
    if (dmax == n - 1)
    {
        cout << "Yes" << endl;
        return 0;
    }
    if (dmax % 2)
    {
        cout << "No" << endl;
        return 0;
    }
    int center = 0;
    for (int i = 0; i < n; i++)
    {
        if (d.at(i) == dmax / 2)
        {
            center = i;
            break;
        }
    }
    d.at(center) = 0;
    dfs(to, d, center, -1);
    for (int i = 0; i < n; i++)
    {
        if (to.at(i).size() == 1 and d.at(i) != dmax / 2)
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}
0