結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-01 11:37:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 903 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 91,544 KB
実行使用メモリ 813,224 KB
最終ジャッジ日時 2024-04-23 22:03:18
合計ジャッジ時間 9,072 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 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,944 KB
testcase_17 AC 6 ms
6,940 KB
testcase_18 AC 9 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 6 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 7 ms
6,944 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 8 ms
6,944 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 236 ms
21,760 KB
testcase_27 AC 134 ms
14,692 KB
testcase_28 AC 153 ms
16,128 KB
testcase_29 AC 274 ms
24,604 KB
testcase_30 AC 128 ms
14,080 KB
testcase_31 AC 174 ms
17,804 KB
testcase_32 AC 264 ms
23,960 KB
testcase_33 AC 137 ms
15,084 KB
testcase_34 AC 174 ms
17,716 KB
testcase_35 AC 167 ms
25,500 KB
testcase_36 MLE -
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>
using namespace std;

vector<vector<int>> Graph;
vector<bool> checked;
vector<vector<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_back(p);
    }
  }
  int res=1;
  sort(vec[i].rbegin(),vec[i].rend());
  if(vec[i].size()<=2){
    for(int j:vec[i]){
      res+=j;
    }
  }else{
    for(int j=0;j<2;j++){
      res+=vec[i][j];
    }
    if(parent!=-1){
      for(int j=2;j<vec[i].size();j++){
        vec[parent].push_back(vec[i][j]);
      }
    }
  }
  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