求树的重心

Description

Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T. 
For example, consider the tree: 

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two. 

For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number. 

Input

The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.

Output

For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.

Sample Input

1
7
2 6
1 2
1 4
4 5
3 7
3 1

Sample Output

1 2
//Serene
//紫书p281 树的重心 
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=200000+10,INF=1<<30;
int T,n,ans,size;

int aa;char c;
int read() {
	aa=0;c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') aa=aa*10+c-'0',c=getchar();
	return aa;
}

int to[2*maxn],nxt[2*maxn],fir[maxn],e=0;
void add(int u,int v) {
	to[++e]=v;nxt[e]=fir[u];fir[u]=e;
	to[++e]=u;nxt[e]=fir[v];fir[v]=e;
}

bool vis[maxn];
int sum[maxn];
void dfs(int pos) {
	vis[pos]=1;
	sum[pos]=0;
	int maxsum=0,x,y;
	for(x=fir[pos];x;x=nxt[x]) {
		y=to[x];
		if(!vis[y]) {
			dfs(y);
			maxsum=max(maxsum,sum[y]+1);
			sum[pos]+=sum[y]+1;
		}
	}
	maxsum=max(maxsum,n-sum[pos]-1);
	if(maxsum<size||(maxsum==size&&pos<ans)) {
		size=maxsum; ans=pos;
	}
}

int main() {
	int x,y;
	T=read();
	while(T--) {
		n=read();e=0;size=INF;
		memset(nxt,0,sizeof(nxt));
		memset(fir,0,sizeof(fir));
		memset(vis,0,sizeof(vis));
		for(int i=1;i<n;++i) {
			x=read();y=read();
			add(x,y);
		}
		dfs(1);
		printf("%d %d\n",ans,size);
	}
	return 0;
} 

在C++中,树的重心通常涉及到对二叉树的遍历和一些数据结构的理解。树的重心节点是指从该节点到其两个子树的最近公共祖先(LCA)的距离相等。以下是计算重心节点的一种常见算法,基于路径压缩的Tarjan算法: 1. 首先,需要为每个节点维护两个值:`sz` 表示当前节点及其所有子孙节点的数量,`depth` 表示节点到根的距离。 2. 对于任意节点 `u`,我们首先找到其左孩子的深度 `l` 和右孩子的深度 `r`,然后更新 `u` 的深度为它们的最大值加一。同时递归地计算左右孩子的`sz`。 3. 计算整个树的直径(最长路径长度),即最大深度 `maxDepth`。这可以通过遍历一次树并存储每个节点的深度得到。 4. 初始化 `root` 为中心节点,因为直径的中点就是重心。接着,对于每个非叶子节点,比较它与上一层的节点在直径中所处的位置。如果当前节点距离直径中心更近,则更新重心为当前节点;否则更新为它的兄弟节点(因为在直径两侧,两者必有一者离中心更远)。 5. 当遍历完所有节点后,返回 `root` 就是树的重心。 下面是一个简单的伪代码示例: ```cpp struct Node { int val; Node* left, *right; int sz = 0, depth = 0; }; Node* findCenter(Node* root) { // ... (上述步骤) while (true) { if (!left || (right && maxDepth - left->depth == right->depth)) { return root; } if (maxDepth - root->depth > right->depth) { root = root->left; } else { root = root->right; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值