結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 colognecologne
提出日時 2022-05-10 15:00:51
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 1,005 bytes
コンパイル時間 2,944 ms
コンパイル使用メモリ 251,944 KB
実行使用メモリ 48,408 KB
最終ジャッジ日時 2023-09-24 20:09:36
合計ジャッジ時間 14,723 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 5 ms
4,384 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 193 ms
13,108 KB
testcase_27 AC 102 ms
9,436 KB
testcase_28 AC 107 ms
10,036 KB
testcase_29 AC 210 ms
14,716 KB
testcase_30 AC 93 ms
8,944 KB
testcase_31 AC 127 ms
11,228 KB
testcase_32 AC 197 ms
14,404 KB
testcase_33 AC 107 ms
9,548 KB
testcase_34 AC 133 ms
10,896 KB
testcase_35 AC 136 ms
14,348 KB
testcase_36 AC 153 ms
32,252 KB
testcase_37 AC 150 ms
14,740 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 178 ms
31,560 KB
testcase_40 AC 173 ms
31,488 KB
testcase_41 AC 182 ms
31,756 KB
testcase_42 AC 183 ms
31,592 KB
testcase_43 AC 187 ms
31,768 KB
testcase_44 AC 186 ms
31,732 KB
testcase_45 AC 184 ms
31,840 KB
testcase_46 AC 184 ms
31,760 KB
testcase_47 AC 189 ms
31,508 KB
testcase_48 AC 181 ms
31,592 KB
testcase_49 AC 172 ms
48,408 KB
testcase_50 AC 197 ms
13,964 KB
testcase_51 AC 208 ms
14,140 KB
testcase_52 AC 200 ms
13,928 KB
testcase_53 AC 199 ms
14,000 KB
testcase_54 AC 215 ms
14,040 KB
testcase_55 AC 162 ms
34,332 KB
testcase_56 AC 226 ms
14,884 KB
testcase_57 AC 224 ms
14,932 KB
testcase_58 AC 234 ms
14,876 KB
testcase_59 AC 143 ms
16,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<vector<int>> adj(N);
    for (int i = 0; i < N - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[--u].push_back(--v);
        adj[v].push_back(u);
    }
    function<priority_queue<int> *(int, int)> dfs = [&](int a, int p) -> priority_queue<int> *
    {
        priority_queue<int> *S = new priority_queue<int>();
        for (auto x : adj[a])
            if (x != p)
            {
                priority_queue<int> *T = dfs(x, a);
                if (S->size() < T->size())
                    swap(S, T);
                while (!T->empty())
                    S->push(T->top()), T->pop();
                delete T;
            }
        int v = 1;
        for (int i = 0; i < 2; i++)
            if (!S->empty())
                v += S->top(), S->pop();
        S->push(v);
        return S;
    };

    auto S = dfs(0, -1);
    cout << S->top() << endl;
    delete S;

    return 0;
}
0