【题目链接】:http://codeforces.com/problemset/problem/765/E

【题意】



给你一棵树;

可以把一个节点的两条相同长度的链合并成一条链;

且这两条相同长度的链上的点不能有“分叉”;

问你最后是否能形成一条链;

然后让你求链的最短值;

【题解】



dfs;

类似树形DP的东西;

对于每个节点x;

看看它下面的链的长度有多少种->把链的长度存在set里面最后看看set的大小,来确定链的种类;

如果链的种类为0,则这个节点为叶子节点,直接返回0

如果链的种类为1,则这个节点以下的节点都能合并成同一条链,输出这个链的长度就好;

如果链的种类为2,则如果这个节点是根节点的话(没有父亲节点),则这两种链合成一种链;

如果链的种类为2,如果这个节点不是根节点的话,就接近无解了,但我们可以再尝试一下,重新dfs一遍,让这个节点作为根节点,然后再dfs一次;看看这次能不能符合.

如果有3种以上的链,则直接输出无解.



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100; vector <int> G[N];
int n,root; int dfs(int x,int fa)
{
set <int> box;
for (int y:G[x])
{
if (y==fa) continue;
int d = dfs(y,x);
if (d==-1) return -1;
box.insert(d+1);
}
if (box.empty()) return 0;
if (int(box.size())==1) return *box.begin();
if (int(box.size())==2 && !fa) return *box.begin()+*box.rbegin();
if (int(box.size())==2 && fa)
{
root = x;
return -1;
}
if (int(box.size())>=3) return -1;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n-1)
{
int x,y;
rei(x),rei(y);
G[x].ps(y),G[y].ps(x);
}
int temp = dfs(1,0);
if (temp==-1 && root) temp = dfs(root,0);
while (temp%2==0) temp/=2;
printf("%d\n",temp);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 765E】Tree Folding的更多相关文章

  1. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【Codeforces 675D】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路

    首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...

  5. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【13.91%】【codeforces 593D】Happy Tree Party

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 792D】Paths in a Complete Binary Tree

    [题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...

随机推荐

  1. JDK8 函数式接口

    JDK8中为了适应函数式响应编程模式,引入了函数式接口概念以增加Lambda表达式的功能.函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口(Single Abstract M ...

  2. HTTP协议下可拖动时间轴播放FLV的实现(伪流媒体)

    HTTP协议下实现FLV的播放其实并不复杂,当初实现的原理是使用了flowPlayer插件实现的,效果还不错.但仍有两大问题影响着客户的访问情绪: 1.预加载时页面卡死,似乎没有边下边播. 2.偶尔边 ...

  3. 9.12NOIP模拟题

    NOIP 2017 全假模拟冲刺                                               hkd 题目名称 Spfa 走楼梯缩小版 滑稽 题目类型 传统 传统 传统 ...

  4. WCF 通讯标准绑定

    WCF 通讯标准绑定 一.预定义标准绑定 标准绑定 说明 BasicHttpBinding BasicHttpBinding 绑定用于最广泛的互交操作,针对第一代Web服务,所使用的传输协议是HTTP ...

  5. [App Store Connect帮助]三、管理 App 和版本(2.2)输入 App 信息:设置 App 分级

    您必须设置 App 分级,这是一项平台版本信息属性,用于在 App Store 上实施家长控制.App Store Connect 提供了一份内容描述列表,通过该列表,您可以确定相应内容在您 App ...

  6. Akka源码分析-Actor创建

    上一篇博客我们介绍了ActorSystem的创建过程,下面我们就研究一下actor的创建过程. val system = ActorSystem("firstActorSystem" ...

  7. 面向对象之继承-5种JavaScript继承的方法

    今天我们讨论一下常用的几种继承方法:首先我们创建一个动物函数Animal: function Animal () { this.species = '动物' }再写准备名叫猫咪的函数Cat: func ...

  8. HTML--form表单中的label标签

    小伙伴们,你们在前面学习表单各种控件的时候,有没有发现一个标签--label,这一小节就来揭晓它的作用. label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性.如果你在 labe ...

  9. 【洛谷2839/BZOJ2653】middle(主席树)

    题目: 洛谷2839 分析: 记\(s_i\)表示原序列中第\(i\)大的数. 考虑对于任意一个区间\([a,b]\),设它的中位数为\(s_m\),那么这个区间内大于等于\(s_m\)的数和小于\( ...

  10. MAC 中安装和使用express

    其实window系统和mac的操作在大致上其实是想同的,只是一些细节的区别,以下对在mac下安装和使用express做简要介绍,如有不妥之处请各位大神指教. 一.首先要测试node和npm是否已经正确 ...