結果

問題 No.1928 Make a Binary Tree
ユーザー NanashimaNanashima
提出日時 2022-05-06 23:44:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,226 bytes
コンパイル時間 1,981 ms
コンパイル使用メモリ 176,948 KB
実行使用メモリ 42,772 KB
最終ジャッジ日時 2023-09-20 07:19:42
合計ジャッジ時間 11,575 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 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,384 KB
testcase_18 AC 8 ms
4,384 KB
testcase_19 AC 3 ms
4,384 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 4 ms
4,384 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 9 ms
4,384 KB
testcase_26 AC 210 ms
14,140 KB
testcase_27 AC 121 ms
10,016 KB
testcase_28 AC 133 ms
10,664 KB
testcase_29 AC 248 ms
15,784 KB
testcase_30 AC 111 ms
9,684 KB
testcase_31 AC 168 ms
11,456 KB
testcase_32 AC 236 ms
15,416 KB
testcase_33 AC 125 ms
10,172 KB
testcase_34 AC 157 ms
11,948 KB
testcase_35 AC 140 ms
15,164 KB
testcase_36 AC 157 ms
30,220 KB
testcase_37 AC 157 ms
16,036 KB
testcase_38 WA -
testcase_39 AC 196 ms
29,560 KB
testcase_40 AC 196 ms
29,508 KB
testcase_41 AC 198 ms
29,396 KB
testcase_42 AC 197 ms
29,456 KB
testcase_43 AC 197 ms
29,248 KB
testcase_44 AC 198 ms
29,360 KB
testcase_45 AC 198 ms
29,436 KB
testcase_46 AC 198 ms
29,376 KB
testcase_47 AC 203 ms
29,408 KB
testcase_48 AC 201 ms
29,360 KB
testcase_49 AC 162 ms
42,772 KB
testcase_50 AC 244 ms
14,756 KB
testcase_51 AC 245 ms
14,892 KB
testcase_52 AC 244 ms
14,792 KB
testcase_53 AC 244 ms
14,788 KB
testcase_54 AC 248 ms
14,832 KB
testcase_55 AC 158 ms
32,024 KB
testcase_56 AC 258 ms
15,908 KB
testcase_57 AC 256 ms
16,056 KB
testcase_58 AC 254 ms
15,972 KB
testcase_59 AC 167 ms
17,680 KB
権限があれば一括ダウンロードができます

ソースコード

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);
        if((int)pq2.size()>(int)pq.size()) swap(pq,pq2);
        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