B. Edge Weight Assignment -结论 树

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

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.

=========================================================================

是个结论题,首先看最小值,显然是1,满足条件无非是每个叶子节点之间的距离都是偶数,比如样例1,3。那么万一存在一个奇数的,如果最少是2的话,必定是奇数个a与偶数个b异或,结果一定不是0,所以最少应该是3。

这一验证如何快速进行呢?随便找一个节点作为根节点显然是不行的,至于lCA等算法肯定大材小用,考虑“ 一个叶子结点到全部叶子结点距离是偶数”就能保证全部叶子结点之间距离都是偶数。

证明如下,另一个叶子结点为偶数,在满足其余全部叶子结点距离都是偶数的时候,这些叶子结点之间 势必要么是子树关系,要么是并列关系,子树关系很显然,偶数减偶数一定是偶数,并列关系必定有LCA,那么必定会有重合地方,也就是 x+y=偶数=x+z ,可以证明 x为偶数时,y,z为偶数,x为奇数时,y,z为奇数,奇偶性相同,其和必为偶数

再看最大值,首先最大必定是n-1个,然后看一看样例的反例就是 两个叶子结点同父的情况,减去这些情况即可。 

# include<bits/stdc++.h>

using namespace std;
# define mod 1000000007
typedef long long int ll;

vector<int>v[100000+10];
int du[100000+10];
int flag;

void dfs(int now,int pre,int dep)
{

    if(du[now]==1&&dep%2==1)
    {
        flag=1;
    }


    for(auto it:v[now])
    {
        if(it==pre)
            continue;

        dfs(it,now,dep+1);

    }
}
int main ()
{


    int n;
    cin>>n;

    for(int i=1; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        du[x]++;
        du[y]++;
    }

    for(int i=1; i<=n; i++)
    {
        if(du[i]==1)
        {

            dfs(i,0,0);

            break;
        }
    }


    if(flag)
    {
        cout<<3<<" ";
    }
    else
    {
        cout<<1<<" ";
    }

    int ans=n-1;

    for(int i=1; i<=n; i++)
    {

        if(du[i]==1)
            continue;
        int cnt=0;
        for(auto it:v[i])
        {
            if(du[it]==1)
            {
                cnt++;
            }
        }
        if(cnt==0)
            continue;
        ans-=(cnt-1);

    }

    cout<<ans;


    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值