結果

問題 No.1424 Ultrapalindrome
ユーザー 👑 箱星箱星
提出日時 2020-12-28 05:30:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 97,016 KB
実行使用メモリ 10,128 KB
最終ジャッジ日時 2024-04-22 16:50:02
合計ジャッジ時間 3,497 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 35 ms
7,936 KB
testcase_10 AC 33 ms
7,808 KB
testcase_11 AC 23 ms
6,528 KB
testcase_12 AC 30 ms
7,424 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 25 ms
6,656 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 20 ms
5,888 KB
testcase_17 AC 24 ms
6,400 KB
testcase_18 AC 17 ms
5,632 KB
testcase_19 AC 25 ms
6,400 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 20 ms
5,760 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 30 ms
7,040 KB
testcase_27 AC 41 ms
8,960 KB
testcase_28 AC 32 ms
8,704 KB
testcase_29 AC 33 ms
8,704 KB
testcase_30 AC 29 ms
10,128 KB
testcase_31 AC 32 ms
10,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using ll = long long;
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<int>> adj(n, vector<int>());
    for (int i = 0; i < n - 1; i++)
    {
        int v1, v2;
        cin >> v1 >> v2;
        v1--;
        v2--;
        adj[v1].push_back(v2);
        adj[v2].push_back(v1);
    }
    vector<int> lst;
    for (int i = 0; i < n; i++)
    {
        if (adj[i].size() >= 3)
        {
            lst.push_back(i);
        }
    }
    if (lst.size() >= 2)
    {
        cout << "No" << endl;
    }
    else if (lst.size() == 0)
    {
        cout << "Yes" << endl;
    }
    else {
        set<int> lens;
        queue<pair<int, pair<int, int>>> que;
        que.push(make_pair(lst[0], make_pair(-1, 0)));
        while (!que.empty())
        {
            auto data = que.front();
            que.pop();
            int v = data.first;
            int p = data.second.first;
            int d = data.second.second;
            bool isleaf = true;
            for (int w : adj[v])
            {
                if (w != p)
                {
                    isleaf = false;
                    que.push(make_pair(w, make_pair(v, d + 1)));
                }
            }
            if (isleaf)
            {
                lens.insert(d);
            }
        }
        cout << (lens.size() == 1 ? "Yes" : "No") << endl;
    }
}
0