E. Tree Folding

题目连接:

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

Description

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 = v, a1, ..., ak, and b0 = v, b1, ..., bk. Additionally, vertices a1, ..., ak, b1, ..., 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 ≤ n, u ≠ 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.

Sample Input

6

1 2

2 3

2 4

4 5

1 6

Sample Output

3

Hint

题意

给你一棵树,可以在这棵树上进行若干次操作,每次操作可以把两条相同的链,根据一个中点合并在一起,具体看题目的图片(CF上面)

然后问你经过若干次合并之后,最后的最短链长度是多少。

如果不能合并成一条链,输出-1.

题解:

拓扑排序,每次我们从度数为1的点出发bfs。

然后往上合并,如果遇到交叉点,我们就合并链。

我们用一个set来合并就好了,如果两条链长度一样,在set里面自动就合并了。

显然能够bfs的点的set里面只有一个数。

而且只能有一个点的set里面有两个数。

最后扫一遍check一下就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
vector<int> E[maxn];
int n,vis[maxn],len[maxn],de[maxn],d[maxn];
set<int> S[maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
d[a]++;
d[b]++;
}
queue<int> Q;
for(int i=1;i<=n;i++){
if(E[i].size()==1){
Q.push(i);
S[i].insert(0);
}
}
while(!Q.empty()){
int now = Q.front();
vis[now]=1;
Q.pop();
for(int i=0;i<E[now].size();i++){
int v = E[now][i];
if(vis[v])continue;
S[v].insert((*S[now].begin())+1);
d[v]--;
if(d[v]==1&&S[v].size()==1){
Q.push(v);
}
}
}
int ans = 0,num2 = 0;
for(int i=1;i<=n;i++){
if(S[i].size()==0){
cout<<"-1"<<endl;
return 0;
}
if(S[i].size()>2){
cout<<"-1"<<endl;
return 0;
}
if(S[i].size()==1){
ans=max(ans,*S[i].begin());
}
if(S[i].size()==2){
ans=max(ans,*S[i].begin()+*S[i].rbegin());
num2++;
}
}
if(num2>1){
cout<<"-1"<<endl;
return 0;
}
while(ans%2==0){
ans/=2;
}
cout<<ans<<endl;
}

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

    地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...

  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. 2018年10月14日ICPC南京站参赛总结

    这次比赛消耗掉了我们全部的信心和精力 在热身赛上,总体来说还是比较愉快的,这个时候心态就不对 正赛的时候我们遇到了A题签到题 我一开始是读错了题意了,认为这个题是一个裸的SG函数,而且那么多人秒过 W ...

  2. Java SSM框架之MyBatis3(一)MyBatis入门

    MyBatis3介绍 mybatis就是一个封装来jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mybatis是一个不完全的o ...

  3. javascript构造函数模块

    var Person = (function(){ var Constr; Constr = function(){ this.name = 'carl'; } Constr.prototype = ...

  4. Crypto 模块安装

    crypto模块的目的是为了提供通用的加密和哈希算法. AES是一种常用的对称加密算法,加解密都用同一个密钥.crypto模块提供了AES支持,但是需要自己封装好函数,便于使用 方法一: 1,到 ht ...

  5. linux,mac安装sentry

    linux,mac安装sentry 最近需要一个日志监视系统所以选择了sentry.以下是用mac安装,看需求量linux安装类似后面的文章会补充. 安装docker https://download ...

  6. 在VMware上安装Ubuntu软件步骤与遇到的相关问题及解决方案

    图解演示环境版本: 本机系统: WIN10 虚拟机:VMware Workstation 12(中文版) 安装目标:Ubuntu Desktop 12.04 LTS  (请点击这里)先下载好iso镜像 ...

  7. Java 基本语法---流程控制

    Java 基本语法---流程控制 0. 概述 三大流程控制语句:顺序.选择.循环. 选择结构: if 结构,if - else结构: 多重 if - else 语句 ; 嵌套 if - else 语句 ...

  8. TcxGrid 去除<No data to display>

  9. Java编程的逻辑 (7) - 如何从乱码中恢复 (下)?

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  10. java & android 开发规范手册

    阿里巴巴Java开发手册(终极版)https://pan.baidu.com/s/1c1UQM7Q 阿里巴巴Java开发规约插件p3cGitHub:https://github.com/alibaba ...