B. Edge Weight Assignment

98 篇文章 3 订阅
8 篇文章 0 订阅

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.

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).

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<bi≤n1≤ai<bi≤n) — it means there is an edge between aiai and bibi. It is guaranteed that given graph forms tree of nn vertices.

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.

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.

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.


给一棵树的每个边填一个数字,使得每个叶子节点之间的简单路径的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<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
vector<LL>g[maxn];
LL col[maxn],fa[maxn],deg[maxn];
void dfs(LL u,LL colour){
	col[u]=colour;
	for(LL i=0;i<g[u].size();i++){
		LL v=g[u][i];
		if(col[v]==1||col[v]==-1 ) continue;
		dfs(v,-colour);
	}
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<n;i++){
  	LL x,y;cin>>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<<mi<<" "<<ans<<endl;
return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值