結果

問題 No.1928 Make a Binary Tree
ユーザー 沙耶花沙耶花
提出日時 2022-05-06 21:35:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 311 ms / 3,000 ms
コード長 1,073 bytes
コンパイル時間 4,685 ms
コンパイル使用メモリ 267,140 KB
実行使用メモリ 55,328 KB
最終ジャッジ日時 2023-09-20 02:28:24
合計ジャッジ時間 15,427 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 10 ms
4,376 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 10 ms
4,380 KB
testcase_25 AC 10 ms
4,416 KB
testcase_26 AC 256 ms
24,320 KB
testcase_27 AC 147 ms
16,604 KB
testcase_28 AC 162 ms
17,492 KB
testcase_29 AC 293 ms
27,336 KB
testcase_30 AC 136 ms
15,376 KB
testcase_31 AC 187 ms
19,388 KB
testcase_32 AC 276 ms
26,444 KB
testcase_33 AC 152 ms
16,668 KB
testcase_34 AC 193 ms
19,480 KB
testcase_35 AC 164 ms
27,828 KB
testcase_36 AC 178 ms
39,552 KB
testcase_37 AC 179 ms
27,120 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 235 ms
41,556 KB
testcase_40 AC 232 ms
41,432 KB
testcase_41 AC 237 ms
41,428 KB
testcase_42 AC 234 ms
41,412 KB
testcase_43 AC 231 ms
41,528 KB
testcase_44 AC 232 ms
41,496 KB
testcase_45 AC 232 ms
41,500 KB
testcase_46 AC 234 ms
41,624 KB
testcase_47 AC 234 ms
41,304 KB
testcase_48 AC 230 ms
41,376 KB
testcase_49 AC 186 ms
55,328 KB
testcase_50 AC 283 ms
25,728 KB
testcase_51 AC 281 ms
25,728 KB
testcase_52 AC 284 ms
25,792 KB
testcase_53 AC 289 ms
25,976 KB
testcase_54 AC 282 ms
25,784 KB
testcase_55 AC 174 ms
41,484 KB
testcase_56 AC 311 ms
27,444 KB
testcase_57 AC 295 ms
27,104 KB
testcase_58 AC 303 ms
27,256 KB
testcase_59 AC 180 ms
24,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

int n;
vector<vector<int>> E;
vector<int> dp;
vector<priority_queue<int>> Q;

void dfs(int cur,int p){
	//cout<<cur<<endl;
	dp[cur] = 1;
	vector<int> t;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur);
		t.push_back(to);
		Q[cur].push(dp[to]);
	}
	if(t.size()==0)return;
	sort(t.begin(),t.end(),[&](int a,int b){
		return Q[a].size()<Q[b].size();
	});
	swap(Q[cur],Q[t.back()]);
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		while(Q[to].size()!=0){
			Q[cur].push(Q[to].top());
			Q[to].pop();
		}
	}
	rep(i,2){
		if(Q[cur].size()==0)break;
		dp[cur] += Q[cur].top();
		Q[cur].pop();
	}
}
	

int main(){
	
	cin>>n;
	E.resize(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u--,v--;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	
	dp.resize(n);
	Q.resize(n);
	
	dfs(0,-1);
	cout<<dp[0]<<endl;
	
    return 0;
}
0