Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E
$DFS子$树进行$DP$
大概分以下几种情况:
1.为叶子,直接返回。
2.长度不同的路径长度只有一条,显然可以合并成这一条的长度。
3.长度不同的路径长度有两条,并且这个点为根,显然可以合并成这两条的长度和。
4.其他的都不合法。
注意:事实上如果第一次$DP$之后不合法要考虑换根再做一次
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<set>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1001000
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,root;
vector<llg>a[maxn]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void init()
{
llg x,y;
cin>>n;
for (llg i=;i<n;i++)
{
x=getint(),y=getint();
a[x].push_back(y),a[y].push_back(x);
}
} llg dfs(llg x,llg fa)
{
set<llg>s;
llg w=a[x].size(),v;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v==fa) continue;
llg val=dfs(v,x);
if (val==-) return val;
s.insert(val+);
}
if (s.size()==) return *s.begin();
if (s.size()==) return ;
if (s.size()==)
{
if (fa) {root=x; return -;}
return *s.begin()+*s.rbegin();
}
return -;
} int main()
{
yyj("tree");
init();
llg ans=dfs(,);
if (ans==- && root) ans=dfs(root,);
while (ans%==) ans/=;
cout<<ans;
return ;
}
Codeforces 765 E. Tree Folding的更多相关文章
- 【codeforces 765E】Tree Folding
[题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- 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 ...
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces 765E. Tree Folding [dfs][树形dp]
题解:先从节点1开始dfs.对于每一个节点,用一个set记录:以该点为根的子树的深度. a) 如果此节点的某个子节点打出了GG,则此节点直接打出GG. b) 若set的元素个数<=1,那么,以该 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 765 D. Artsem and Saunder(数学题)
题目链接:http://codeforces.com/contest/765/problem/D 题意:题目中给出你两个公式,g(h(x))==x,h(g(x))==f(x).现给你f(x) 让你求符 ...
随机推荐
- いっしょ / Be Together (暴力枚举)
题目链接:http://abc043.contest.atcoder.jp/tasks/arc059_a Time limit : 2sec / Memory limit : 256MB Score ...
- 5、CentOS 6.5系统安装配置Nginx-1.2.7+PHP-5.3.22环境
一,操作系统 以最小服务器形式安装系统,并添加开发工具库,便于后期编译使用. 此处基本都是下一步,下一步,不再废话. 安装完成,进入系统,调通网络,关闭防火墙或打开相应的WEB端口. 以下安装操作默认 ...
- JS中常见原生DOM操作API
摘自:https://blog.csdn.net/hj7jay/article/details/53389522 几种对象 Node Node是一个接口,中文叫节点,很多类型的DOM元素都是继承于它, ...
- weblogic11,linux字符页面安装
1. 安装前 1 确定weblogic版本被认证 首先请确认您要安装的Weblogic版本所在的平台已通过了BEA的认证,完整的认证平台列表请参考http://e-docs.bea.com/wls ...
- \r\n回车换行\r回车\n换行的区别
在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33,Linux/Unix下的tty概念也来自于此)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候 ...
- Python小项目四:实现简单的web服务器
https://blog.csdn.net/u010103202/article/details/74002538 本博客是整理在学习实验楼的课程过程中记录下的笔记形成的,参考:https://www ...
- Solr创建核的方法
Solr创建核的方法,简单粗暴 就是进入到solrhome中进行复制粘贴这个collection2 然后进入到conf中,修改一下name 然后从新启动tomcat
- foreve结束
import asyncio from threading import Thread import time print('main start:',time.time()) async def s ...
- 作为phper既然了解共享内存函数shmop的使用方法,那么就必须要了解一下信号量是什么,以及信号量使用的代码案例
在单独的一个PHP进程中读写.创建.删除共享内存方面上你应该没有问题了.但是实际运行中不可能只是一个PHP进程在运行中.如果在多个进程的情况下你还是沿用单个进程的处理方法,你一定会碰到问题--著名的并 ...
- 关于PRD、MRD、BRD文档
笔者现在所在的公司有专职的产品经理,在职业生涯最长的那家公司,并没有专门的产品经理,虽然在创业公司期间有产品经理,但是似乎产品经理更多的是和客户沟通需求,反馈,并不输出正规意义上产品经理该有的输出.从 ...