結果

問題 No.1928 Make a Binary Tree
ユーザー NanashimaNanashima
提出日時 2022-05-06 23:45:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 257 ms / 3,000 ms
コード長 1,226 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 175,860 KB
実行使用メモリ 42,848 KB
最終ジャッジ日時 2023-09-20 07:21:48
合計ジャッジ時間 10,614 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 9 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 8 ms
4,376 KB
testcase_25 AC 9 ms
4,376 KB
testcase_26 AC 204 ms
14,272 KB
testcase_27 AC 119 ms
9,992 KB
testcase_28 AC 131 ms
10,580 KB
testcase_29 AC 235 ms
15,616 KB
testcase_30 AC 106 ms
9,744 KB
testcase_31 AC 149 ms
11,468 KB
testcase_32 AC 227 ms
15,412 KB
testcase_33 AC 120 ms
10,212 KB
testcase_34 AC 152 ms
11,692 KB
testcase_35 AC 138 ms
15,048 KB
testcase_36 AC 155 ms
30,308 KB
testcase_37 AC 155 ms
15,912 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 190 ms
29,496 KB
testcase_40 AC 190 ms
29,508 KB
testcase_41 AC 188 ms
29,576 KB
testcase_42 AC 189 ms
29,452 KB
testcase_43 AC 190 ms
29,292 KB
testcase_44 AC 190 ms
29,360 KB
testcase_45 AC 196 ms
29,388 KB
testcase_46 AC 191 ms
29,320 KB
testcase_47 AC 189 ms
29,564 KB
testcase_48 AC 193 ms
29,472 KB
testcase_49 AC 159 ms
42,848 KB
testcase_50 AC 225 ms
14,792 KB
testcase_51 AC 226 ms
14,724 KB
testcase_52 AC 225 ms
14,888 KB
testcase_53 AC 228 ms
14,832 KB
testcase_54 AC 223 ms
14,816 KB
testcase_55 AC 156 ms
32,008 KB
testcase_56 AC 257 ms
16,040 KB
testcase_57 AC 242 ms
15,916 KB
testcase_58 AC 241 ms
15,932 KB
testcase_59 AC 154 ms
17,624 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=1;

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