题目链接: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. linux /etc/shadow--passwd/pam.d/system-auth文件详解

     在linux操作系统中, /etc/passwd文件中的每个用户都有一个对应的记录行,记录着这个用户的一下基本属性.该文件对所有用户可读.   而/etc/shadow文件正如他的名字一样,他是pa ...

  2. hiho一下 第145周

    题目1 : 智力竞赛 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队参加一个智力竞赛.竞赛采用过关制,共计N个关卡.在第i ...

  3. selenium使用location定位元素坐标偏差

    python+selenium+Chromedriver使用location定位元素坐标偏差使用xpath定位元素,用.location获取坐标值,截取网页截图的一部分出现偏差. 之所以会出现这个坐标 ...

  4. Golang利用select和普通函数分别实现斐波那契数列

    //斐波那契数列 //1 1 2 3 5 8 //观察规律 //第一轮:前两个数是1,1,相加等于2 //第二轮:第二个数和第三个数是1,2,相加等于3 //第三轮:第三个数和第四个数是2,3,相加等 ...

  5. Python3 hasattr()、getattr()、setattr()函数简介

    Python3 hasattr().getattr().setattr()函数简介 一.hasattr(object, name) 判断object对象中是否存在name属性,当然对于python的对 ...

  6. Huffman Implementation with Python

    Huffman Implementation with Python 码表 Token Frequency a 10 e 15 i 12 s 3 t 4 space 13 n 1 生成 Huffman ...

  7. 【RMAN】使用RMAN的 Compressed Backupsets备份压缩技术 (转载)

    1.Oracle参考文档中关于RMAN备份压缩的描述1)关于如何通过调整RMAN参数启用取消备份压缩功能http://download.oracle.com/docs/cd/B19306_01/bac ...

  8. ldap集成x-pack

    ldap配置支持x-pack有两种格式: 1.  User Search Mode 2. User DN Templates Mode 由于第一种方式需要明文填入ldap管理员账号信息,我这边采用第二 ...

  9. 20155201 李卓雯 《网络对抗技术》实验一 逆向及Bof基础

    20155201 李卓雯 <网络对抗技术>实验一 逆向及Bof基础 一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,f ...

  10. 【Python028--引入文件】

    一.打开文件 1.open()函数 打开模式 执行操作 ‘r’ 以只读方式打开文件(默认) ‘w’ 以写入的方式打开文件,会覆盖已存在的文件 ‘x’     如果文件已经存在,使用此模式打开将引发异常 ...