結果

問題 No.1928 Make a Binary Tree
ユーザー monnumonnu
提出日時 2022-05-06 22:20:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,599 bytes
コンパイル時間 4,384 ms
コンパイル使用メモリ 232,300 KB
実行使用メモリ 64,756 KB
最終ジャッジ日時 2023-09-20 03:17:55
合計ジャッジ時間 13,932 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 168 ms
30,856 KB
testcase_36 AC 173 ms
45,304 KB
testcase_37 AC 176 ms
28,584 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 224 ms
46,584 KB
testcase_40 AC 226 ms
46,456 KB
testcase_41 AC 224 ms
46,648 KB
testcase_42 AC 222 ms
46,584 KB
testcase_43 AC 221 ms
46,520 KB
testcase_44 AC 224 ms
46,524 KB
testcase_45 AC 221 ms
46,580 KB
testcase_46 AC 222 ms
46,516 KB
testcase_47 AC 222 ms
46,696 KB
testcase_48 AC 222 ms
46,496 KB
testcase_49 AC 195 ms
64,756 KB
testcase_50 AC 266 ms
27,372 KB
testcase_51 AC 263 ms
27,364 KB
testcase_52 AC 264 ms
27,332 KB
testcase_53 AC 267 ms
27,292 KB
testcase_54 AC 270 ms
27,360 KB
testcase_55 AC 180 ms
47,544 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 174 ms
27,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define INF 1000000000
#define MOD 998244353
#define MAX 300000

int ans=0;

priority_queue<int>* dfs(Graph &G,int v,int p,vector<int> &sz){
  priority_queue<int>* pq = new priority_queue<int>();
  vector<priority_queue<int>*> pqs;
  for(int nv:G[v]){
    if(nv==p){
      continue;
    }
    pqs.push_back(dfs(G,nv,v,sz));
    pq->push(sz[nv]);
  }
  if(pq->size()==1){
    if(pqs[0]->size()>0){
      ans+=pqs[0]->top();
      pqs[0]->pop();
    }
  }
  sz[v]=1;
  for(int i=0;i<2;i++){
    if(pq->empty()){
      break;
    }
    sz[v]+=pq->top();
    pq->pop();
  }
  int best_i=-1;
  int best_n=pq->size();
  for(int i=0;i<pqs.size();i++){
    if(pqs[i]->size()>best_n){
      best_i=i;
      best_n=pqs[i]->size();
    }
  }
  if(best_i==-1){
    for(int i=0;i<pqs.size();i++){
      while(!pqs[i]->empty()){
        pq->push(pqs[i]->top());
        pqs[i]->pop();
      }
    }
    return pq;
  }else{
    for(int i=0;i<pqs.size();i++){
      if(i==best_i){
        continue;
      }
      while(!pqs[i]->empty()){
        pqs[best_i]->push(pqs[i]->top());
        pqs[i]->pop();
      }
    }
    while(!pq->empty()){
      pqs[best_i]->push(pq->top());
      pq->pop();
    }
    return pqs[best_i];
  }
}

int main(){
  int N;
  cin>>N;
  Graph G(N);
  for(int i=0;i<N-1;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  vector<int> sz(N);
  dfs(G,0,-1,sz);
  ans+=sz[0];
  cout<<ans<<endl;
}
0