結果

問題 No.1928 Make a Binary Tree
ユーザー NanashimaNanashima
提出日時 2022-05-06 23:23:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,169 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 175,108 KB
実行使用メモリ 36,640 KB
最終ジャッジ日時 2023-09-20 06:18:05
合計ジャッジ時間 11,482 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 9 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,380 KB
testcase_20 AC 6 ms
4,384 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 218 ms
14,416 KB
testcase_27 AC 127 ms
9,920 KB
testcase_28 AC 143 ms
10,912 KB
testcase_29 AC 249 ms
15,744 KB
testcase_30 AC 116 ms
9,788 KB
testcase_31 AC 163 ms
11,784 KB
testcase_32 AC 243 ms
15,664 KB
testcase_33 AC 130 ms
10,296 KB
testcase_34 AC 155 ms
11,684 KB
testcase_35 AC 144 ms
15,184 KB
testcase_36 TLE -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF ((1LL<<62)-(1LL<<31))
#define all(a)  (a).begin(),(a).end()
#define rall(a)  (a).rbegin(),(a).rend()
typedef long long ll;
typedef pair<ll,ll> pl;

ll ans=0;

priority_queue<ll,vector<ll>> dfs(vector<vector<int>> &g,int pos,int par) {
    ll cnt=0,ret=1;
    priority_queue<ll,vector<ll>> pq;
    for(auto nv:g[pos]) {
        if(nv==par) continue;
        cnt++;
        priority_queue<ll,vector<ll>> pq2=dfs(g,nv,pos);
        while(!pq2.empty()) {
            pq.push(pq2.top());
            pq2.pop();
        }
    }
    if(cnt==0) {
        pq.push(1);
        return pq;
    }
    rep(i,2) {
        if(pq.empty()) break;
        ret+=pq.top();
        pq.pop();
    }
    if(pos==0) ans=ret;
    pq.push(ret);
    return pq;
}

int main() {
    ll n;
    cin >> n;
    vector<vector<int>> g(n,vector<int> ());
    vector<int> deg(n,0);
    rep(i,n-1) {
        int u,v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    priority_queue<ll,vector<ll>> pq=dfs(g,0,-1);
    cout << ans << endl;
    return 0;
}
0