結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 kmjpkmjp
提出日時 2022-05-06 22:18:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 3,000 ms
コード長 1,481 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 209,564 KB
実行使用メモリ 52,436 KB
最終ジャッジ日時 2023-09-20 03:14:16
合計ジャッジ時間 10,963 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
17,888 KB
testcase_01 AC 9 ms
17,696 KB
testcase_02 AC 8 ms
17,576 KB
testcase_03 AC 8 ms
17,580 KB
testcase_04 AC 9 ms
17,576 KB
testcase_05 AC 9 ms
17,628 KB
testcase_06 AC 8 ms
17,580 KB
testcase_07 AC 8 ms
17,576 KB
testcase_08 AC 9 ms
17,580 KB
testcase_09 AC 8 ms
17,656 KB
testcase_10 AC 9 ms
17,568 KB
testcase_11 AC 8 ms
17,572 KB
testcase_12 AC 8 ms
17,572 KB
testcase_13 AC 9 ms
17,660 KB
testcase_14 AC 9 ms
17,564 KB
testcase_15 AC 15 ms
18,660 KB
testcase_16 AC 13 ms
18,308 KB
testcase_17 AC 12 ms
18,324 KB
testcase_18 AC 14 ms
18,460 KB
testcase_19 AC 9 ms
17,944 KB
testcase_20 AC 11 ms
18,096 KB
testcase_21 AC 11 ms
18,112 KB
testcase_22 AC 13 ms
18,276 KB
testcase_23 AC 12 ms
18,000 KB
testcase_24 AC 14 ms
18,380 KB
testcase_25 AC 15 ms
18,804 KB
testcase_26 AC 203 ms
36,224 KB
testcase_27 AC 115 ms
28,664 KB
testcase_28 AC 126 ms
29,596 KB
testcase_29 AC 236 ms
38,836 KB
testcase_30 AC 106 ms
27,772 KB
testcase_31 AC 145 ms
31,668 KB
testcase_32 AC 225 ms
37,904 KB
testcase_33 AC 114 ms
28,928 KB
testcase_34 AC 150 ms
32,076 KB
testcase_35 AC 69 ms
24,208 KB
testcase_36 AC 151 ms
45,724 KB
testcase_37 AC 163 ms
52,436 KB
testcase_38 AC 8 ms
17,840 KB
testcase_39 AC 148 ms
43,496 KB
testcase_40 AC 153 ms
43,888 KB
testcase_41 AC 156 ms
43,492 KB
testcase_42 AC 151 ms
43,592 KB
testcase_43 AC 151 ms
43,320 KB
testcase_44 AC 152 ms
43,488 KB
testcase_45 AC 152 ms
43,496 KB
testcase_46 AC 147 ms
43,464 KB
testcase_47 AC 153 ms
43,648 KB
testcase_48 AC 152 ms
43,340 KB
testcase_49 AC 88 ms
48,892 KB
testcase_50 AC 194 ms
28,512 KB
testcase_51 AC 189 ms
28,576 KB
testcase_52 AC 195 ms
28,580 KB
testcase_53 AC 192 ms
28,776 KB
testcase_54 AC 190 ms
28,512 KB
testcase_55 AC 90 ms
41,012 KB
testcase_56 AC 247 ms
39,684 KB
testcase_57 AC 249 ms
39,536 KB
testcase_58 AC 252 ms
39,988 KB
testcase_59 AC 218 ms
43,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
vector<int> E[202020];
multiset<int> M[202020];

void dfs(int cur,int pre) {
	FORR(e,E[cur]) if(e!=pre) {
		dfs(e,cur);
		
		if(M[cur].size()<M[e].size()) swap(M[cur],M[e]);
		FORR(v,M[e]) M[cur].insert(v);
	}
	
	if(M[cur].empty()) {
		M[cur].insert({1});
	}
	else if(M[cur].size()==1) {
		int x=*M[cur].begin();
		M[cur]={x+1};
	}
	else {
		int x=*M[cur].rbegin();
		M[cur].erase(M[cur].find(x));
		int y=*M[cur].rbegin();
		M[cur].erase(M[cur].find(y));
		M[cur].insert(x+y+1);
	}
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	
	dfs(0,0);
	cout<<*M[0].rbegin()<<endl;
	
		
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0