結果

問題 No.1928 Make a Binary Tree
ユーザー Nanashima
提出日時 2022-05-06 22:54:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 967 bytes
コンパイル時間 1,605 ms
コンパイル使用メモリ 176,560 KB
実行使用メモリ 33,772 KB
最終ジャッジ日時 2024-07-06 00:46:02
合計ジャッジ時間 8,348 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23 WA * 34
権限があれば一括ダウンロードができます

ソースコード

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 dfs(vector<vector<int>> &g,int pos,int par,priority_queue<ll,vector<ll>> &pq) {
    ll cnt=0,ret=1;
    for(auto nv:g[pos]) {
        if(nv==par) continue;
        cnt++;
        ll res=dfs(g,nv,pos,pq);
        pq.push(res);
    }
    if(cnt==0) return 1;
    rep(i,2) {
        if(pq.empty()) break;
        ret+=pq.top();
        pq.pop();
    }
    return ret;
}

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;
    ll ans=dfs(g,0,-1,pq);
    cout << ans << endl;
    return 0;
}
0