結果

問題 No.1752 Up-Down Tree
ユーザー zhicheng
提出日時 2024-06-04 16:04:00
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 2,824 ms
コンパイル使用メモリ 224,380 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-12-23 10:56:19
合計ジャッジ時間 4,799 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
const int N=100010;
int tot,first[N],nnext[N<<1],to[N<<1];
__gnu_pbds::priority_queue<int>q[N];
void add(int x,int y){
	nnext[++tot]=first[x];
	first[x]=tot;
	to[tot]=y;
}
void dfs(int u,int fa){
	int a,b;
	for(int e=first[u];e;e=nnext[e]){
		if(to[e]!=fa){
			dfs(to[e],u);
			q[u].join(q[to[e]]);
		}
	}
	if(q[u].size()==0){
		q[u].push(1);
		return;
	}
	if(q[u].size()==1){
		a=q[u].top();
		q[u].pop();
		q[u].push(a+1);
		return;
	}
	a=q[u].top();
	q[u].pop();
	b=q[u].top();
	q[u].pop();
	if(a%2||b%2){
		q[u].push(a+b+1);
	}
	else{
		q[u].push(a+b);
		q[u].push(1);
	}
}
int main(){
	int n,a,b;
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		scanf("%d%d",&a,&b);
		add(a,b);
		add(b,a);
	}
	dfs(1,0);
	printf("%d",q[1].top());
}
0