結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 19:52:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 885 bytes
コンパイル時間 1,234 ms
コンパイル使用メモリ 94,420 KB
実行使用メモリ 45,488 KB
最終ジャッジ日時 2024-04-23 22:09:00
合計ジャッジ時間 10,313 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 WA -
testcase_14 WA -
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
26,896 KB
testcase_36 AC 164 ms
30,936 KB
testcase_37 WA -
testcase_38 AC 2 ms
6,940 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 AC 181 ms
45,488 KB
testcase_50 AC 277 ms
23,508 KB
testcase_51 AC 268 ms
23,520 KB
testcase_52 AC 282 ms
23,684 KB
testcase_53 AC 278 ms
23,716 KB
testcase_54 AC 277 ms
23,564 KB
testcase_55 AC 166 ms
32,968 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 167 ms
22,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>
using namespace std;

vector<vector<int>> Graph;
vector<bool> checked;
vector<priority_queue<int>> vec;

int dfs(int i,int parent){
  checked[i]=true;
  for(int to:Graph[i]){
    if(!checked[to]){
      int p=dfs(to,i);
      vec[i].push(p);
    }
  }
  int res=1;
  if(vec[i].size()<=2){
    while(!vec[i].empty()){
      res+=vec[i].top();
      vec[i].pop();
    }
  }else{
    for(int j=0;j<2;j++){
      res+=vec[i].top();
      vec[i].pop();
    }
    if(parent!=-1){
      vec[parent].swap(vec[i]);
    }
  }
  return res;
}

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