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

地址: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

61 22 32 44 51 6

output

3

input

71 21 33 41 55 66 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。

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

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=2e5+7;
12 const int mod=1e9+7;
13
14 int n,mx;
15 vector<int>mp[K];
16
17 int dfs(int x,int f)
18 {
19     int a[3],num=0;
20     for(int i=0;i<mp[x].size();i++)
21     if(mp[x][i]!=f)
22     {
23         int v=mp[x][i];
24         a[2]=dfs(v,x);
25         if(a[2]==-1)
26             return -1;
27         if(num==0)
28             a[0]=a[2],num++;
29         else if(num==1)
30         {
31             if(a[0]!=a[2])
32                 num++,a[1]=a[2];
33         }
34         else
35         {
36             if(a[0]!=a[2]&&a[1]!=a[2])
37                 return -1;
38         }
39     }
40     if(f!=0)
41     {
42         if(num==0)
43             return 1;
44         else if(num==1)
45             return a[0]+1;
46         mx=x;
47         return -1;
48     }
49     if(num==2)
50         return a[0]+a[1];
51     else
52         return a[0];
53
54 }
55 int main(void)
56 {
57     cin>>n;
58     for(int i=1,u,v;i<n;i++)
59         scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
60     int ans=dfs(1,0);
61     if(ans==-1 && mx)ans=dfs(mx,0);
62     while(ans%2==0)ans>>=1;
63     printf("%d\n",ans);
64     return 0;
65 }
时间: 2024-08-08 03:14:17

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

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 standard input output standard output There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (f

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

传送门:http://codeforces.com/contest/765 A题:给你家的名字,以及n张机票的起点和终点,Jinotega一开始在家,你要根据这些机票的起点和终点判断Jinotega最后是不是在家.直接记录起点和终点家出现的次数,如果相等说明Jinotega在家,否则不在家. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include &

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B

Description Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest. To obfuscate th

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 seconds memory limit per test 512 megabytes input standard input output standard output Misha and Vanya have played several table tennis sets. Each set co

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 seconds memory limit per test 512 megabytes input standard input output standard output Artsem has a friend Saunders from University of Chicago. Saunders

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C

Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is r

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A

Description There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artse

Codeforces Round #397 (Div. 2)

A - Neverending competitions 没有任何价值..... 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 const int maxn = 100 + 5; 5 typedef long long LL; 6 typedef pair<int, int>pii; 7 8 char home[5]; 9 int main() 10 { 11 in

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/