結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 20:02:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 3,000 ms
コード長 1,308 bytes
コンパイル時間 1,353 ms
コンパイル使用メモリ 95,992 KB
実行使用メモリ 54,736 KB
最終ジャッジ日時 2024-04-23 22:09:11
合計ジャッジ時間 10,656 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 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,948 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 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,944 KB
testcase_15 AC 10 ms
6,944 KB
testcase_16 AC 8 ms
6,940 KB
testcase_17 AC 6 ms
6,940 KB
testcase_18 AC 9 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 6 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 8 ms
6,940 KB
testcase_23 AC 6 ms
6,944 KB
testcase_24 AC 9 ms
6,944 KB
testcase_25 AC 11 ms
6,940 KB
testcase_26 AC 241 ms
22,288 KB
testcase_27 AC 137 ms
15,032 KB
testcase_28 AC 154 ms
16,044 KB
testcase_29 AC 284 ms
24,900 KB
testcase_30 AC 128 ms
14,060 KB
testcase_31 AC 175 ms
17,676 KB
testcase_32 AC 266 ms
24,116 KB
testcase_33 AC 142 ms
15,444 KB
testcase_34 AC 180 ms
17,808 KB
testcase_35 AC 168 ms
27,120 KB
testcase_36 AC 178 ms
38,400 KB
testcase_37 AC 176 ms
25,172 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 226 ms
39,952 KB
testcase_40 AC 226 ms
39,780 KB
testcase_41 AC 229 ms
39,776 KB
testcase_42 AC 222 ms
39,764 KB
testcase_43 AC 225 ms
39,780 KB
testcase_44 AC 226 ms
39,984 KB
testcase_45 AC 229 ms
39,840 KB
testcase_46 AC 229 ms
39,752 KB
testcase_47 AC 229 ms
39,780 KB
testcase_48 AC 229 ms
39,996 KB
testcase_49 AC 189 ms
54,736 KB
testcase_50 AC 275 ms
23,564 KB
testcase_51 AC 272 ms
23,720 KB
testcase_52 AC 281 ms
23,656 KB
testcase_53 AC 270 ms
23,560 KB
testcase_54 AC 274 ms
23,572 KB
testcase_55 AC 172 ms
37,640 KB
testcase_56 AC 285 ms
25,008 KB
testcase_57 AC 286 ms
24,840 KB
testcase_58 AC 288 ms
24,992 KB
testcase_59 AC 165 ms
22,692 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 max_size=vec[i].size();
  int id=i;  
  for(int to:Graph[i]){
    if(to!=parent){
      if(vec[to].size()>max_size){
        id=to;
        max_size=vec[to].size();
      }
    }
  }
  if(id!=i){
    while(!vec[i].empty()){
      vec[id].push(vec[i].top());
      vec[i].pop();
    }
  }
  for(int to:Graph[i]){
    if(id!=to && to!=parent){
      while(!vec[to].empty()){
        vec[id].push(vec[to].top());
        vec[to].pop();
      }
    }
  }
  vec[i].swap(vec[id]);
  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();
    }
  }
  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