結果

問題 No.1928 Make a Binary Tree
ユーザー harurun
提出日時 2021-12-09 11:03:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,018 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 85,332 KB
実行使用メモリ 721,424 KB
最終ジャッジ日時 2024-11-06 04:23:28
合計ジャッジ時間 8,406 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33 MLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;

vector<int> Graph[200010];
int checked[200010];
vector<int> vec[200010];

int dfs(int i,int parent){
  checked[i]=1;
  for(int to:Graph[i]){
    if(!checked[to]){
      int p=dfs(to,i);
      vec[i].emplace_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].emplace_back(vec[i][j]);
      }
      vec[i].resize(0);
    }
  }
  return res;
}

int N;
int x,y;
int ans;
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cin>>N;
  for(int i=0; i<N-1; i++){
    cin>>x>>y;
    Graph[x-1].emplace_back(y-1);
    Graph[y-1].emplace_back(x-1);
  }
  ans=dfs(0,-1);
  cout<<ans<<"\n";
}
0