【codeforces 765E】Tree Folding
【题目链接】: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的更多相关文章
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 675D】Tree Construction
[链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.91%】【codeforces 593D】Happy Tree Party
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 792D】Paths in a Complete Binary Tree
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
随机推荐
- IDEA Spark Streaming 操作(RDD队列流)
import org.apache.spark.SparkConf import org.apache.spark.rdd.RDD import org.apache.spark.streaming. ...
- mvn scope (转)
策略一: 对于容器提供的(如:servlet-api-2.3等)和测试需要时的(如:junit-3.81等),可以直接在pom.xml中去掉. maven的dependency中有一个tag是< ...
- RegisterAttached 两种绑定方式
RegisterAttached 含义:使用指定的属性名称.属性类型和所有者类型注册附加属性 绑定方式:C#绑定.WPF绑定 例:需求DataViewModel为DataView的VM层,在DataV ...
- PCB InCAM 获取 JOB STEP 实现外挂脚本调试功能实现
PCB CAM自动化基于Incam 打造,在测试时经常遇到调试障碍,每次自行对功能测试时,生成了exe脚本后,再到Incam里面运行,发现问题,再回来修改代码,非常不爽, 参考Genesis调试运行模 ...
- 【转】深入理解Java多态原理
之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...
- xcode常用的快捷键
按键 描述 command+[ 左移代码块 command+] 右移代码块 Tab键 接受代码自动完成提示 Esc键 显示代码提示菜单 command+方向left键 移动光标到本行行首 comman ...
- Django用户认证系统
一. 认证系统概要 create_user 创建用户 authenticate 验证登录 login 记住用户的登录状态 logout 退出登录 is_authenticated 判断用户是否登录 l ...
- HTML和CSS网页开发基础
一 HTML文档结构 HTML文档结构:<html>.<head>.<title>.<body>构成HTML页面中最基本的元素. HTML常用标记:1. ...
- PDO访问其他数据库操作及作用
PDO的作用 PDO<!--数据访问抽象层--><!--1.可以访问其他数据库--><!--2.具有事务功能--><!--3.带有预处理语句功能(防止SQL注 ...
- day01-编程与计算机组成原理
什么是编程 编程语言:是人与计算机沟通交流的介质,通过标准化的规则传递信息 编程:就是为了使计算机能够理解人的意图,通过编程语言写出一个个文件,这堆文件完成相应的目的 编程的目的:用计算机取代人完成工 ...