Codeforces Round #633 (Div. 2)D Edge Weight Assignment(構造、樹的權值異或)

Edge Weight Assignment

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

6
1 3
2 3
3 4
4 5
5 6

output

1 4

input

6
1 3
2 3
3 4
4 5
4 6

output

3 3

input

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

output

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.

 思路:最大值和最小值是兩個不同問題,我們分開思考:

最小值:

如果任意兩兩葉子結點不存在奇數長度簡單路徑,那麼所有路徑上隨便填一個值,就可以保證兩兩異或等零了

否則的話,兩個數肯定不行,因爲最後會剩下一條新的路徑,至少要三個數,按照a,b,a,b, c.......的順序填

其中c=a^b

關於葉子結點之間的長度問題,隨便找個根dfs一下就好,然後記錄一下各個葉子結點到根的深度,只要存在奇數和偶數長度,那麼任意兩兩葉子結點就存在奇數長度簡單路徑,否則就沒有

最大值:

可以如下構造:

我們設想如果樹是一條長鏈那麼答案就是n-1,但當它不是長鏈,比如多了一個分支,那麼這條最長鏈還是可以保證填的數都不相同,然後新增出來的分支上填的數就等於某些數的異或和(這個和一定是新的),可能有點難理解,但是想象一下應該還好

接着我們就會發現如果某兩個葉子結點連邊有公共結點,那麼一定是要相同的參考第三個例子的左側的1、2、3結點,1與3連權值1,2與3連權值1,必須要保證相同,那麼我們就很容易想出來,只要x個葉子結點有唯一公共結點,就必須答案減x-1(初始值是n-1,因爲有n-1條邊)

ok,上代碼,希望你懂~

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
    x=0;char ch=getchar();ll f=1;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x個加上val
vector<ll>v[100005],check;
ll d[100005],cntt[100005],depth[100005],vis[100005],ou,ji;
inline void dfs(int x,int h,int fa)
{
	depth[x]=h;
	vis[x]=1;
	for(int i=0;i<v[x].size();i++)
	{
		int k=v[x][i];
		if(!vis[k])
		dfs(k,h+1,x);
		
	}
}
int main() 
{
	cin>>n;
	ll ans1,ans2=n-1;
	for(int i=1;i<n;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b),v[b].push_back(a);
		d[a]++,d[b]++;
	}
	depth[1]=0;
	dfs(1,0,-1);
	ll cnt=0;
	for(int i=1;i<=n;i++)
	{
		
		if(d[i]==1)
		{
			if(depth[i]%2)ji++;
			else ou++;
			cnt++;
			cntt[v[i][0]]++;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(cntt[i]>=2)ans2-=cntt[i]-1;
	}
	if(ji&&ou)ans1=3;
	else ans1=1;
	cout<<ans1<<" "<<ans2<<endl;
    return 0;
    
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章