B. Edge Weight Assignment

https://codeforces.com/problemset/problem/1338/B

You have unweighted tree of nn vertices. You have to assign a positive weight to each edge so that the following condition would hold:

  • For every two different leaves v1v1 and v2v2 of this tree, bitwise XOR of weights of all edges on the simple path between v1v1 and v2v2 has to be equal to 00.

Note that you can put very large positive integers (like 10(1010)10(1010)).

It's guaranteed that such assignment always exists under given constraints. Now let's define ff as the number of distinct weights in assignment.

B. Edge Weight Assignment_第1张图片In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is 00. ff value is 22 here, because there are 22 distinct edge weights(44 and 55).

B. Edge Weight Assignment_第2张图片In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex 11 and vertex 66 (3,4,5,43,4,5,4) is not 00.

What are the minimum and the maximum possible values of ff for the given tree? Find and print both.

Input

The first line contains integer nn (3≤n≤1053≤n≤105) — the number of vertices in given tree.

The ii-th of the next n−1n−1 lines contains two integers aiai and bibi (1≤ai

Output

Print two integers — the minimum and maximum possible value of ff can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.

Examples

input

Copy

6
1 3
2 3
3 4
4 5
5 6

output

Copy

1 4

input

Copy

6
1 3
2 3
3 4
4 5
4 6

output

Copy

3 3

input

Copy

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

output

Copy

1 6

Note

In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

B. Edge Weight Assignment_第3张图片

In the second example, possible assignments for each minimum and maximum are described in picture below. The ff value of valid assignment of this tree is always 33.

B. Edge Weight Assignment_第4张图片

In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

B. Edge Weight Assignment_第5张图片


给一棵树的每个边填一个数字,使得每个叶子节点之间的简单路径的xor为零,要你求出为满足这种条件的使用的不同数字的个数的最大值和最小值。

自己最开始的思路是朝着每个节点的距离是偶数还是奇数去思考的。

首先是偶数的话,如果每个叶子节点之间都是偶数,那么直接n个1就可以XOR成 0,满足条件。

那如果出现了奇数怎么办?根据XOR的结合律,先结合前面n-1个数字,(偶数个)出来肯定是0,然后通过最后一个1变成0.

能成立吗?题目给的条件是正整数,所以前面n-1个数全放1是不能成立的。所以要放至少两个不同的数,结果变成1,最后再和一个1变成0.

那么最大怎么想?这道题的启发是树的构造题可以尝试往根 ,lca,父节点去思考。从父节点思考,每个父亲节点的直连节点里如果有叶子节点的话,那么这两个叶子节点必然要相同权值。

那先令最大值为n-1.碰到相同权值的时候减去(叶子个数-1)。剩下的边在无穷大数里可以随意放,每个不同父节点里直连的叶子节点和其他父节点里是不同的权值。这样合起来是最大的。

代码上:对于判断每个节点直接是不是偶数距离,可以采用染色法。如果都是同色就是偶数距离,不然就是奇数。

父节点可以不用dfs开桶直接统计直连叶子节点数量。

可以尝试开deg[]度数代替g[i].size()方便思考

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout<<#a<<"="<g[maxn];
LL col[maxn],fa[maxn],deg[maxn];
void dfs(LL u,LL colour){
	col[u]=colour;
	for(LL i=0;i>n;
  for(LL i=1;i>x>>y;
  	g[x].push_back(y);
  	g[y].push_back(x);
  	deg[x]++;deg[y]++;
  }
  for(LL i=1;i<=n;i++){
  	if(deg[i]==1){
  		fa[g[i][0]]++;	
	}
  }
  LL ans=n-1;
  for(LL i=1;i<=n;i++){
  	ans-=max((LL)0,fa[i]-1);
  }
  dfs(1,1);
  LL f1=0;LL f2=0;
  for(LL i=1;i<=n;i++){
  	if(deg[i]==1){
  		if(col[i]==1) f1=1;
		if(col[i]==-1) f2=1;  	
	}
  }
  LL mi=0;
  if(f1&f2){
  	mi=3;
  }
  else mi=1;
  cout<

 

你可能感兴趣的:(奇数偶数性,构造,思维)