Dragon Slayer (the largest sub-tree)

Title Description

  Xiao Ming is the village of A Dragon, he has to defend the peace of the village, in order to not intrusion dragon. And Xiao Ming dragon who also hated, so the dragon decided to organize a collective attack, to beat Xiao Ming, scored A Village. Xiao Ming know, collective attack when the dragon, will create a mysterious link between each other, and are connected to each other such links can dragon's ability to grow, and each has a dragon added to a link, the link in the ability of all dragon will add 1, but only when the fighting capacity greater than Xiao Ming dragon fighting to kill the dragon. Fortunately, Xiao Ming has a one-time Tulong, he can ignore the fighting to kill a dragon, and eliminate all links dragon body. Suppose fighting when not linked to each dragon 1, dragon initially all connected together only N N-1 th link. Bob would like to know how much he must have at least a fighting force to kill all the dragons, and he wondered what he should be killed by Tulong dragons.

Enter a description

  The first line of the input is an integer N (1 <= N <= 40000), N represents a total of only long. The next line N-1 integers a, b (separated by spaces), indicating a link relationship between long.

Output Description

  Output two integers separated by a space. The first integer X, the dragon represents the number of applications Tulong killed. If several dragons can be killed Tulong, output the smallest number that second integer T, Xiao Ming expressed at least some combat.

Examples

Input
. 8
. 1 2
2. 3
15
. 5. 6
. 6. 8
2. 4
. 5. 7
Output
15

Problem-solving ideas

  Examples are given of the simulation, after killing a dragon, which is to remove a node, then the rest of the nodes in neighbor killed node is divided into several collections, the largest collection (up to the number of nodes) the number of nodes plus 1, at least that is the ability to have Dragon Slayer. It requires that all nodes on the essence of all the sub-tree of the largest sub-tree.
  If one node is traversed, the largest sub-tree were obtained, due to the large number of redundant computation, it will be out. Since the subject clearly by initial N nodes N-1 th link edges together, it can be seen as an initial tree, a root node after a specified himself (corresponding to a given direction, the directed graph into a non- the drawing), a deep search can be determined as the number of each node (+1 and all subtrees size) the root node of the subtree contain, as added directionality, there is no way to directly a subtree seek out of the total number of nodes - the root of the tree as a child node size is the size of the rest of the sub-tree, in the process of maintaining a minimum the largest sub-tree recorded on it.

Implementation code

#include <iostream>
#include <vector>
#define NN 40010
using namespace std;
vector<vector<int> > Long(NN,vector<int>());
int n,maxn=NN,index_max=NN;
int getTreeSum(int index,int pre){
	int len=Long[index].size();
	int sum=0,maxv=0,subsum;
	for(int i=0;i<len;i++)
		if(Long[index][i]!=pre){
			subsum=getTreeSum(Long[index][i],index);
			if(subsum>maxv)
				maxv=subsum;
			sum+=subsum;
		}
	sum++;
	int left=maxv;
	if(n-sum>maxv)
		left=n-sum;
	if(maxn>left){
		maxn=left;
		index_max=index;
	}else if(maxn==left&&index_max>index)
		index_max=index;
	return sum;
}
int main(){
	cin>>n;
	int a,b;
	for(int i=1;i<n;i++){
		cin>>a>>b;
		Long[a].push_back(b);
		Long[b].push_back(a);

	}
	getTreeSum(1,-1);
	if(maxn!=0)
		maxn++;
	cout<<index_max<<" "<<maxn<<endl;
	return 0;
}

  Initially all nodes are linked together in this condition will reduce complexity, requires two or deep search.

Published 16 original articles · won praise 0 · Views 319

Guess you like

Origin blog.csdn.net/qq_41922018/article/details/104383175