刷题总结——Tree2cycle(hdu4714 树形dp)
题目:
A cycle of n nodes is defined as follows: (1)a graph with n nodes and n edges (2)the degree of every node is 2 (3) each node can reach every other node with these N edges.InputThe first line contains the number of test cases T( T<=10 ). Following lines are the scenarios of each test case.
In the first line of each test case, there is a single integer N( 3<=N<=1000000 ) - the number of nodes in the tree. The following N-1 lines describe the N-1 edges of the tree. Each line has a pair of integer U, V ( 1<=U,V<=N ), describing a bidirectional edge (U, V).
OutputFor each test case, please output one integer representing minimal cost to transform the tree to a cycle.
Sample Input
1
4
1 2
2 3
2 4
Sample Output
3
Hint
In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.
题目大意:给定一棵树··问将这棵树变成环的最小价值
题解:
原本以为是道很难的题··结果发现自己智障了···
首先将树剖成链···对于每个节点··如果有2个以上的儿子··需要将儿子节点断开到只剩2个··再将该节点与父亲节点断开···否则直接与父亲节点相连··每剖一次代价加1··最后合并链的时候代价*2+1(+1是因为首位链要合并)··
递归求解答案即可··
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e6+;
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
int n,ans=,first[N],nxt[N*],go[N*],tot,T;
inline void pre()
{
memset(first,,sizeof(first));ans=tot=;
}
inline void comb(int a,int b)
{
nxt[++tot]=first[a],first[a]=tot,go[tot]=b;
nxt[++tot]=first[b],first[b]=tot,go[tot]=a;
}
inline int dfs(int u,int fa)
{
int cnt=;
for(int e=first[u];e;e=nxt[e]) {int v=go[e];if(v==fa) continue;cnt+=dfs(v,u);}
if(cnt>=){ans+=cnt-+(u==?:);return ;}
else return ;
}
int main()
{
// freopen("a.in","r",stdin);
T=R();
while(T--)
{
pre();n=R();int a,b;
for(int i=;i<n;i++) {a=R(),b=R();comb(a,b);}
dfs(,);cout<<ans*+<<endl;
}
return ;
}
刷题总结——Tree2cycle(hdu4714 树形dp)的更多相关文章
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- HDU 4714 Tree2cycle (树形DP)
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp
题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...
- hdu4714树形DP+贪心(乱搞)
Tree2cycle A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- poj3417 Network 树形Dp+LCA
题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...
- 『战略游戏 最大利润 树形DP』
通过两道简单的例题,我们来重新认识树形DP. 战略游戏(luoguP1026) Description Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要 ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- 【HDU - 4340】Capturing a country(树形DP)
BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...
- cf161d 求距离为k的点对(点分治,树形dp)
点分治裸题,但是用树形dp也能做 /* dp[u][k]表示在u下距离k的点数量 */ #include<bits/stdc++.h> using namespace std; ]; ], ...
随机推荐
- CF Gym 100187E Two Labyrinths (迷宫问题)
题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...
- ECshop安装提示cls_image::gd_version() 和不支持JPEG
ecshop版本:ECShop_V2.7.3_UTF8_release1106php 版本 5.5--------------------------------------------------- ...
- JAVA并发编程:相关概念及VOLATILE关键字解析
一.内存模型的相关概念 由于计算机在执行程序时都是在CPU中运行,临时数据存在主存即物理内存,数据的读取和写入都要和内存交互,CPU的运行速度远远快于内存,会大大降低程序执行的速度,于是就有了高速缓存 ...
- 1066: 输入n个数和输出调整后的n个数
1066: 输入n个数和输出调整后的n个数 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2739 Solved: 1578[Submit][Stat ...
- ovs的学习
本来编辑好了的, 结果忘了保存, 坑爹,直接把人家的网址贴上来吧 http://blog.chinaunix.net/uid-20737871-id-4333314.html 昨天遇到一个问题(虚拟机 ...
- python queue - 同步队列类
参考 官网 queue 模块 queue 模块实现多生产者,多消费者队列. 当必须在 ==多个线程之间安全地交换信息== 时,它在线程编程中特别有用. 此模块中的Queue类实现了所有必需的锁定语义. ...
- Jquery之 Ajax /json
前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...
- 二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...
- destoon 短信发送函数及短信接口修改
// $DT在common.inc.php中定义, $CACHE = cache_read('module.php'); $DT = $CACHE['dt']; 从缓存里读取网站配置信息. //$d ...
- Liunx将私密代理添加到环境变量
.bash_profile文件存在于用户主目录下,绝对路径为/home/$name/.bash_profile.bash_profile文件是隐藏文件,里面包含的是用户的用户的环境变量. 注意: 这个 ...