地址:http://codeforces.com/contest/765/problem/E

题目:

E. Tree Folding
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = va1, ..., ak, and b0 = vb1, ..., bk. Additionally, vertices a1, ..., akb1, ..., bk must not have any neighbours in the tree other than adjacent vertices of corresponding paths. After that, one of the paths may be merged into the other, that is, the vertices b1, ..., bk can be effectively erased:

Help Vanya determine if it possible to make the tree into a path via a sequence of described operations, and if the answer is positive, also determine the shortest length of such path.

Input

The first line of input contains the number of vertices n (2 ≤ n ≤ 2·105).

Next n - 1 lines describe edges of the tree. Each of these lines contains two space-separated integers u and v (1 ≤ u, v ≤ nu ≠ v) — indices of endpoints of the corresponding edge. It is guaranteed that the given graph is a tree.

Output

If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path.

Examples
input
6
1 2
2 3
2 4
4 5
1 6
output
3
input
7
1 2
1 3
3 4
1 5
5 6
6 7
output
-1
Note

In the first sample case, a path of three edges is obtained after merging paths 2 - 1 - 6 and 2 - 4 - 5.

It is impossible to perform any operation in the second sample case. For example, it is impossible to merge paths 1 - 3 - 4 and 1 - 5 - 6, since vertex 6 additionally has a neighbour 7 that is not present in the corresponding path.

思路:自己做的时候傻傻的以为把度数最大的点做为根,然后dfs一遍就可以了,然后wa的不能自理。

  后来参考了http://blog.csdn.net/liangzhaoyang1/article/details/55803877的做法。

  发现树形dp基本一样,不过人家是两遍dfs。

  具体可以看他的博文,讲的很详细。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=2e5+;
const int mod=1e9+; int n,mx;
vector<int>mp[K]; int dfs(int x,int f)
{
int a[],num=;
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=f)
{
int v=mp[x][i];
a[]=dfs(v,x);
if(a[]==-)
return -;
if(num==)
a[]=a[],num++;
else if(num==)
{
if(a[]!=a[])
num++,a[]=a[];
}
else
{
if(a[]!=a[]&&a[]!=a[])
return -;
}
}
if(f!=)
{
if(num==)
return ;
else if(num==)
return a[]+;
mx=x;
return -;
}
if(num==)
return a[]+a[];
else
return a[]; }
int main(void)
{
cin>>n;
for(int i=,u,v;i<n;i++)
scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
int ans=dfs(,);
if(ans==- && mx)ans=dfs(mx,);
while(ans%==)ans>>=;
printf("%d\n",ans);
return ;
}

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding的更多相关文章

  1. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set

    F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...

  3. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造

    D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...

  4. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题

    C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...

  5. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题

    B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...

  6. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题

    A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2

    地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seco ...

随机推荐

  1. Android学习15--使用(Drawable)资源

    1.图片资源 图片资源是最简单的Drawable资源.仅仅要把*.png.*.jpg*..gif等格式的图片放入/res/drawable-XXX文件夹下,Android SDK就会在编译应用自己主动 ...

  2. jquery添加!important样式

    $("span.colorFlag").parents("th").css("cssText","background-color ...

  3. ARM汇编(2)(指令)

    一,ARM汇编语言立即数的表示方法 十六进制:前缀:0x 十进制:无前缀 二制:前缀:0b 二,常用的ARM指令(标准的ARM语法,GNU的ARM语法) 1.@M开头系列 MOV R0, #12 @R ...

  4. Codeforces 193A. Cutting Figure

    看起来非常神,但仅仅有三种情况 -1 , 1 ,2..... A. Cutting Figure time limit per test 2 seconds memory limit per test ...

  5. .NET中的枚举用法浅析

    本文简单分析了.NET中的枚举用法.分享给大家供大家参考.具体分析如下: 我理解的枚举就是编程中约定的一个“可选值”:例如QQ的在线状态,分别有    在线,Q我吧,隐身,忙碌等等...我觉得这就是一 ...

  6. 离散化——化不可能为可能(STL)

    所谓离散,就是化连续为不连续,使得我们某种枚举的方法得以实现. 当然,离散还能够帮助我们将某些数据范围很大达到2^16,但是这些数据并不多(例如才1000+),我们可以把数据进行离散,保持他们之间的相 ...

  7. Java模拟并发

    =========================one============================= public class Bingfa { public static void m ...

  8. confirm() event.target.getAttribute('id')

    w <?php $wecho = '<form id="del' . $wid . '" method="POST" action="&q ...

  9. Spring MVC 根据请求方法限定:请求方法(四)

    请求方法 说明 GET 使用GET方法检索一个表述(representation)——也就是对资源的描述.多次执行同一GET请求,不会对系统造成影响,GET方法具有幂等性[指多个相同请求返回相同的结果 ...

  10. xshell上传下载文件

    上传文件到服务器rz 从服务器下载文件sz