結果
| 問題 |
No.1928 Make a Binary Tree
|
| コンテスト | |
| ユーザー |
harurun
|
| 提出日時 | 2021-12-01 11:37:50 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 903 bytes |
| コンパイル時間 | 4,261 ms |
| コンパイル使用メモリ | 121,876 KB |
| 最終ジャッジ日時 | 2025-01-26 02:38:51 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 MLE * 1 |
| other | AC * 37 TLE * 11 MLE * 9 |
ソースコード
#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;
}
harurun