【题目链接】: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. dotnet core 文档链接

    The installation was successful The following were installed at C:\Program Files\dotnet • .NET Core ...

  2. XAML实例教程系列 - 类型转换器(Type Converter)七

    XAML实例教程系列 - 类型转换器(Type Converter) 分类: Windows 8 Silverlight2012-06-25 13:40 961人阅读 评论(0) 收藏 举报 butt ...

  3. bzoj2844

    http://www.lydsy.com/JudgeOnline/problem.php?id=2844 线性基... 先把线性基搞出来,然后不断逼近答案,如果这个基比答案小了,那么说明要加上,同时加 ...

  4. 【高德地图API】VS2012或者VS2013添加高德地图v2.1.1版本SDK失败

    可能由于v2.1.1版本SDK可能是在Win8.1环境下编译[这里有许多的原因,系统升级,安装VS2013等等] 有童鞋在操作正常的情况下添加SDK失败,提示版本不兼容. 如下图: 编辑项目  *.c ...

  5. PCB SQL SERVER 字段模糊匹配个数 实现方法

    今天工程系统给到加投加投组件的数据规则修改,遇到需将一个字段模糊匹配的个数统计 这类需求要平时应该很少遇到了,这里将此方法分享出来, 一.需求如下 例子:itempara字段中的内容是: IVH板 铜 ...

  6. Rails5 Route Document

    创建: 2017/06/29 完成: 2017/06/29 更新: 2017/06/30 最开头的有效路径展示补充网页版 更新: 2017/07/21 修正错别字 更新: 2017/09/02 增加m ...

  7. 路一直都在——That's just life

    分享一首很喜欢的歌,有时候歌词写得就是经历,就是人生... 穿过人潮汹涌灯火栏栅 没有想过回头 一段又一段走不完的旅程 什么时候能走完 我的梦代表什么 又是什么让我们不安 That's just li ...

  8. esp和ebp指针

    gdb调试的时候会出现esp和ebp这两个指针,而这两个指针为我们查看栈的情况提供了方便. 简单点说,esp指向栈顶,而ebp指向栈底.例如一段程序: #include <stdio.h> ...

  9. Java注解Annotation的用法 - 自定义Annotation实现

    Java注解又称Java标注,是Java语言5.0版本开始支持加入源代码的特殊语法元数据. Java语言中的类.方法.变量.参数和包等都可以被标注.和Javadoc不同,Java标注可以通过反射获取标 ...

  10. ACM算法目录

    数据结构 栈,队列,链表 •哈希表,哈希数组 •堆,优先队列 双端队列 可并堆 左偏堆 •二叉查找树 Treap 伸展树 •并查集 集合计数问题 二分图的识别 •平衡二叉树 •二叉排序树 •线段树 一 ...