地址: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. pl/sql 实例精解 07

    这章主要讨论 oracle11g 新特性, continue, continue when 语句 continue 的作用同其他编程语言一样. continue when condition 只是当条 ...

  2. 使用jmeter实现对jar包的调用

    一.前言 在我们测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据(如有时需要对参数的输入值使用第三方jar包进行加密操作),涉及到这种的情况,普遍做法是:手动调用jar包获得需要的值 ...

  3. .NET学习笔记(1)

    把 DataSet 绑定到 Repeater 控件 Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件.Repeater 控件可被绑定到数据库表.XML 文件或者其他项目列表. 获取 ...

  4. 【python】获取网页中中文内容并分词

    # -*- coding: utf-8 -*- import urllib2 import re import time import jieba url="http://www.baidu ...

  5. poj 3204(最小割)

    题目链接:http://poj.org/problem?id=3204 思路:显然只有增大那最小割边集上的边才能增加最大流,因此,我们可以先跑一遍最大流,然后对于那些满足条件的边u->v,当且仅 ...

  6. plsql参数

    PL/SQL中对out,in out参数使用的? 默认形参会复制一份实参的副本,然后在内部传递,修改等,发生异常,不会赋值给实参,控制权交还调用环境,而实参值不变,还是调用前的值.而使用了NOCOPY ...

  7. PHP编程经常容易记乱的知识

    PHP经常容易记乱的知识 1.echo和print的区别 PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.echo输出后没有返回值,但print有返回值,当其执行失败 ...

  8. PAXOS: libevent_paxos

    PAXOS实现 -- libevent_paxos 该文章是项目的一部分.主要讲PAXOS算法的实现.                                                  ...

  9. iOS-项目archive错误Code Sign error: No matching provisioning profile found: Your build settings UUID:

    解决办法: 1.关闭项目 2.打开xprojiect右键 打开包文件 3.搜索:PROVISIONING_PROFILE 相关删除了

  10. 网络虚拟化之FlowVisor:网络虚拟层(中)

    上一篇博客网络虚拟化之FlowVisor:网络虚拟层(上)主要对比了计算机虚拟化和网络虚拟化,引出了FLowVisor网络虚拟层,介绍了其一些特性,这篇博文深入讲解FLowVisor的技术. 一. 概 ...