結果

問題 No.1928 Make a Binary Tree
ユーザー cologne
提出日時 2022-05-10 15:00:51
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 260 ms / 3,000 ms
コード長 1,005 bytes
コンパイル時間 2,895 ms
コンパイル使用メモリ 253,872 KB
実行使用メモリ 48,656 KB
最終ジャッジ日時 2024-07-17 20:54:22
合計ジャッジ時間 13,296 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

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