結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-01 11:41:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 953 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 94,440 KB
実行使用メモリ 434,628 KB
最終ジャッジ日時 2024-04-23 22:03:28
合計ジャッジ時間 9,068 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 11 ms
6,940 KB
testcase_16 AC 8 ms
6,944 KB
testcase_17 AC 6 ms
6,940 KB
testcase_18 AC 10 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 5 ms
6,940 KB
testcase_22 AC 8 ms
6,944 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 9 ms
6,940 KB
testcase_25 AC 10 ms
6,940 KB
testcase_26 AC 243 ms
23,144 KB
testcase_27 AC 143 ms
15,788 KB
testcase_28 AC 159 ms
17,088 KB
testcase_29 AC 284 ms
25,980 KB
testcase_30 AC 127 ms
14,720 KB
testcase_31 AC 181 ms
18,544 KB
testcase_32 AC 278 ms
25,452 KB
testcase_33 AC 146 ms
15,928 KB
testcase_34 AC 186 ms
18,792 KB
testcase_35 AC 164 ms
26,992 KB
testcase_36 TLE -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

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){
      while(!vec[i].empty()){
        vec[parent].push(vec[i].top());
        vec[i].pop();
      }
    }
  }
  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