結果

問題 No.1424 Ultrapalindrome
ユーザー trineutrontrineutron
提出日時 2021-03-12 21:42:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,594 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 200,240 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2024-04-22 17:01:12
合計ジャッジ時間 4,123 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 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 67 ms
7,808 KB
testcase_10 AC 64 ms
7,936 KB
testcase_11 AC 44 ms
6,528 KB
testcase_12 AC 61 ms
7,552 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 49 ms
6,912 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 38 ms
6,016 KB
testcase_17 AC 45 ms
6,528 KB
testcase_18 AC 31 ms
5,504 KB
testcase_19 AC 51 ms
6,784 KB
testcase_20 AC 12 ms
5,376 KB
testcase_21 AC 43 ms
6,144 KB
testcase_22 AC 26 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 62 ms
7,296 KB
testcase_27 AC 80 ms
9,344 KB
testcase_28 AC 74 ms
17,024 KB
testcase_29 AC 76 ms
16,896 KB
testcase_30 AC 54 ms
9,588 KB
testcase_31 AC 65 ms
9,456 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);
    int diff = 0;
    for (int i = 0; i < n; i++)
    {
        if (to.at(i).size() == 1 and d.at(i) != dmax / 2)
        {
            cout << "No" << endl;
            return 0;
        }
        if (to.at(i).size() == 1)
        {
            diff++;
        }
        if (d.at(i) == 1)
        {
            diff--;
        }
    }
    if (diff == 0)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}
0