题目链接: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的更多相关文章

  1. 【codeforces 765E】Tree Folding

    [题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...

  2. 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 ...

  3. 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 ...

  4. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  5. Codeforces 765E. Tree Folding [dfs][树形dp]

    题解:先从节点1开始dfs.对于每一个节点,用一个set记录:以该点为根的子树的深度. a) 如果此节点的某个子节点打出了GG,则此节点直接打出GG. b) 若set的元素个数<=1,那么,以该 ...

  6. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  7. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

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

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

  9. codeforces 765 D. Artsem and Saunder(数学题)

    题目链接:http://codeforces.com/contest/765/problem/D 题意:题目中给出你两个公式,g(h(x))==x,h(g(x))==f(x).现给你f(x) 让你求符 ...

随机推荐

  1. vue去掉严格开发,即去掉vue-cli安装时的eslint

    vue去掉严格开发,即去掉vue-cli安装时的eslint : 1.vue-cli书写规范(主要是js规范) a.逗号.冒号后面要加空格 b.不能使用双引号,一律使用单引号 webpack的语法检查 ...

  2. vue生产环境清除console.log

    npm run build 后的生产环境的代码,会有很多开发时留下的console.log(),不可能每个页面不停地删除 在build/webpack.prod.conf.js文件里加上这样一段代码即 ...

  3. Java中高级面试必问之多线程TOP50(含答案)

    以下为大家整理了今年一线大厂面试被问频率较高的多线程面试题,由于本人的见识局限性,所以可能不是很全面,也欢迎大家在后面留言补充,谢谢. 1.什么是线程? 2.什么是线程安全和线程不安全? 3.什么是自 ...

  4. 给web项目整合富文本编辑器

    给jsp页面整合富文本编辑器下载——删除多余的组件——加入到项目中——参照案例来完成整合步骤:1. 解压zip文件,将所有文件复制到Tomcat的webapps/kindeditor目录下. 2. 将 ...

  5. IntelliJ IDEA. Debug模式

    资料收集: https://www.bilibili.com/video/av6749471/?p=16 eclipse debug模式. 基础 Intellij Idea--Debug使用 Inte ...

  6. 合并ts到mp4

    这个比较好用. copy /b d:\xxx\download_ts\*   d:\xxx\download_ts\new.mp4 用python ffmpeg也可以,不过我合出来有卡顿或者掉声问题, ...

  7. 解决mySQL占用内存超大问题

    为了装mysql环境测试,装上后发现启动后mysql占用了很大的虚拟内存,达8百多兆.网上搜索了一下,得到高人指点my.ini.再也没见再详细的了..只好打开my.ini逐行的啃,虽然英文差了点,不过 ...

  8. ubuntu文件名乱码convmv和iconv

    sudo apt install convmv sudo convmv -f gbk -t utf- -r --notest /home/pm/Desktop/p Linux下两个工具convmv和i ...

  9. 3 字节的 UTF-8 序列的字节 3 无效 解决

    参考下列应该可以解决,笔者为3. 1.https://blog.csdn.net/hostel_2/article/details/51517361 2.https://blog.csdn.net/u ...

  10. php mysqli 的使用方法

    原文链接:https://blog.csdn.net/solly793755670/article/details/52217456 Mysqli是php5之后才有的功能 需要修改php.ini的配置 ...