Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + Div. 2 combined) E. Tree folding

Source: Internet
Author: User

Address: Http://codeforces.com/contest/765/problem/E

Topic:

E. Tree foldingTime limit per test2 secondsmemory limit per testMegabytesinputStandard InputOutputStandard Output

Vanya wants to minimize a tree. He can perform the following operation multiple Times:choose a vertexv, and both disjoint (except forv) Paths of equal lengtha0?=?v,a1,...,ak, andb0?=? v ,   b 1, ...,  b K . Additionally, Vertices  a 1, ...,   a K ,   b 1, ...,   b K  must not There are any neighbours in the tree and other than adjacent vertices of corresponding paths. After this, one of the paths may merged into the other, which is, the Vertices  b 1, ...,   b K   Can be effectively erased:

Help Vanya determine if it possible 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, space-separated integers u and v (1?≤? U,? v≤? n, u? ≠? V)-indices of endpoints of the corresponding edge. It is guaranteed, 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.

Examplesinput
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 was 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's impossible to merge paths 1?-? 3?-? 4 and 1?-? 5?-? 6, since Vertex 6 additionally have a neighbour 7 That's not present in the corresponding path.

Thinking: When I do the silly thought of the largest point of the degree as the root, and then Dfs once again, and then WA can not take care of themselves.

Later reference was made to the practice of http://blog.csdn.net/liangzhaoyang1/article/details/55803877.

Found the tree DP basically the same, but the others are two times Dfs.

You can read his blog, and tell it in detail.

1#include <bits/stdc++.h>2 3 using namespacestd;4 5 #defineMP Make_pair6 #definePB push_back7typedefLong LongLL;8typedef pair<int,int>PII;9 Const Doubleeps=1e-8;Ten Const DoublePi=acos (-1.0); One Const intk=2e5+7; A Const intmod=1e9+7; -  - intn,mx; thevector<int>Mp[k]; -  - intDfsintXintf) - { +     inta[3],num=0; -      for(intI=0; I<mp[x].size (); i++) +     if(mp[x][i]!=f) A     { at         intv=Mp[x][i]; -a[2]=DFS (v,x); -         if(a[2]==-1) -             return-1; -         if(num==0) -a[0]=a[2],num++; in         Else if(num==1) -         { to             if(a[0]!=a[2]) +num++,a[1]=a[2]; -         } the         Else *         { $             if(a[0]!=a[2]&&a[1]!=a[2])Panax Notoginseng                 return-1; -         } the     } +     if(f!=0) A     { the         if(num==0) +             return 1; -         Else if(num==1) $             returna[0]+1; $mx=x; -         return-1; -     } the     if(num==2) -         returna[0]+a[1];Wuyi     Else the         returna[0]; -  Wu } - intMainvoid) About { $Cin>>N; -      for(intI=1, u,v;i<n;i++) -scanf"%d%d",&u,&v), Mp[u]. PB (v), Mp[v]. PB (u); -     intAns=dfs (1,0); A     if(ans==-1&& mx) ans=dfs (MX,0); +      while(ans%2==0) ans>>=1; theprintf"%d\n", ans); -     return 0; $}

Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + Div. 2 combined) E. Tree folding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.