結果

問題 No.1928 Make a Binary Tree
ユーザー t98slidert98slider
提出日時 2022-05-07 07:36:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 3,000 ms
コード長 1,156 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 174,920 KB
実行使用メモリ 52,232 KB
最終ジャッジ日時 2023-09-20 16:18:38
合計ジャッジ時間 14,005 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 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 10 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 7 ms
4,376 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 10 ms
4,380 KB
testcase_26 AC 231 ms
22,784 KB
testcase_27 AC 132 ms
15,188 KB
testcase_28 AC 142 ms
16,384 KB
testcase_29 AC 269 ms
25,352 KB
testcase_30 AC 123 ms
14,336 KB
testcase_31 AC 171 ms
17,920 KB
testcase_32 AC 258 ms
24,660 KB
testcase_33 AC 136 ms
15,384 KB
testcase_34 AC 173 ms
18,180 KB
testcase_35 AC 145 ms
21,216 KB
testcase_36 AC 166 ms
40,652 KB
testcase_37 AC 171 ms
27,404 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 212 ms
38,884 KB
testcase_40 AC 211 ms
38,804 KB
testcase_41 AC 213 ms
38,784 KB
testcase_42 AC 210 ms
38,848 KB
testcase_43 AC 211 ms
38,660 KB
testcase_44 AC 212 ms
38,884 KB
testcase_45 AC 214 ms
38,912 KB
testcase_46 AC 212 ms
38,780 KB
testcase_47 AC 212 ms
38,720 KB
testcase_48 AC 211 ms
38,804 KB
testcase_49 AC 166 ms
52,232 KB
testcase_50 AC 261 ms
24,156 KB
testcase_51 AC 261 ms
24,316 KB
testcase_52 AC 261 ms
24,240 KB
testcase_53 AC 266 ms
24,224 KB
testcase_54 AC 261 ms
24,132 KB
testcase_55 AC 161 ms
39,888 KB
testcase_56 AC 281 ms
25,564 KB
testcase_57 AC 288 ms
25,492 KB
testcase_58 AC 288 ms
25,520 KB
testcase_59 AC 197 ms
28,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int n;
    cin >> n;
    vector<int> p(n);
    vector<vector<int>> g(n);
    vector<priority_queue<int>> t(n);
    function<int(int)> leader=[&](int v){
        if(p[v] <= 0)return v;
        return p[v] = leader(p[v]);
    };
    auto merge=[&](int u,int v){
        int x = leader(u), y = leader(v);
        if(-p[x] < -p[y]) swap(x, y);
        while(!t[y].empty()){
            t[x].push(t[y].top());
            t[y].pop();
        }
        p[x] += p[y];
        p[y] = x;
    };
    int u, v;
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    function<void(int,int)> dfs=[&](int v,int q){
        for(auto &&u:g[v]){
            if(u == q)continue;
            dfs(u, v);
            merge(u, v);
        }
        int add = 1, x = leader(v);
        for(int i = 0; i < 2; i++){
            if(!t[x].empty()){
                add += t[x].top();
                t[x].pop();
            }
        }
        t[x].push(add);
        p[x] = -t[x].size();
    };
    dfs(0, -1);
    cout << t[leader(0)].top() << endl;
}
0