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;
}
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (中)
- li浮动时ul高度为0,解决ul自适应高度的几种方法
- 使用正则表达式求完整路径中的文件名
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (下)
- Centos下SFTP双机高可用环境部署记录
- as3:Function以及call,apply
- centos6下redis cluster集群部署过程
- centos6下ActiveMQ+Zookeeper消息中间件集群部署记录
- 发布一个轻量级的滑块控件
- as3:sprite作为容器使用时,最好不要指定width,height
- openssl版本升级操作记录
- 清除浮动(clearfix hack)
- Linux下环境变量配置方法梳理(.bash_profile和.bashrc的区别)
- 小程序火爆的因素
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python中id函数运行方式
- CentOS 7如何实现定时执行python脚本
- PHP自动生成缩略图函数的源码示例
- 解决tensorflow 释放图,删除变量问题
- php生成word并下载代码实例
- TensorFlow保存TensorBoard图像操作
- 浅谈PHP SHA1withRSA加密生成签名及验签
- PHP PDO数据库操作预处理与注意事项
- laravel 框架配置404等异常页面
- Django –Xadmin 判断登录者身份实例
- Laravel 队列使用的实现
- keras 两种训练模型方式详解fit和fit_generator(节省内存)
- Keras 中Leaky ReLU等高级激活函数的用法
- Python错误的处理方法
- python3的pip路径在哪