社区讨论

91求助,玄关

P3884[JLOI2009] 二叉树问题参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mhjug9se
此快照首次捕获于
2025/11/04 08:41
4 个月前
此快照最后确认于
2025/11/04 08:41
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=109;
struct dian{
	LL cnt,le,ri,fa,D,Bool;
};
LL x,y;
LL Dcnt[N];
dian tree[N];
LL dp(LL x,LL y)
{
	if(x==y)return 0;
	if(tree[x].D!=tree[y].D)
		if(tree[x].D<tree[y].D)
			return dp(x,tree[y].fa)+1;
		else
			return dp(tree[x].fa,y)+2;
	else
		return dp(tree[x].fa,tree[y].fa)+3;
}
int main()
{
	LL maxD=1;
	LL n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		LL u,v;
		cin>>u>>v;
		if(tree[u].Bool==0){
			tree[u].Bool=1;
			tree[u].D=1;
		}
		if(tree[u].cnt){
			tree[u].cnt++;
			tree[u].ri=v;
		}
		else{
			tree[u].cnt++;
			tree[u].le=v;
		}
		tree[v].D=tree[u].D+1;
		tree[v].fa=u;
		maxD=max(maxD,tree[v].D);
		tree[v].Bool=1;
	}
	cin>>x>>y;
	for(int i=1;i<=n;i++){
		if(tree[i].Bool)
		Dcnt[tree[i].D]++;
	}
	LL max_cnt=-1;
	for(int i=1;i<=n;i++)
	{
		max_cnt=max(max_cnt,Dcnt[i]);
	}
	cout<<maxD<<'\n'<<max_cnt<<'\n'<<dp(x,y);
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...