题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714

Tree2cycle

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 400    Accepted Submission(s): 78

Problem Description
A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of cost respectively. The nodes are labeled from 1 to N. Your job is to transform the tree to a cycle(without superfluous edges) using minimal cost.

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.

 
Input
The 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).
 
Output
For 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
 
题目大意:已知一棵树,切边与连边都需要1的cost。现在要把这棵树分割连接成为一个环,求最小cost。
 
解题思路:将整棵树拆成N个单枝(所有点的度小于2),所需的cost即为2*N-1(拆成N个单枝需 N-1 cost,合成一个环需要 N cost)。
可用树形DP求出N最小的情况,用dp[i][j]表示以i为根的可用度为j的最小单枝数。
状态转移方程:dp[root][0]=min(dp[root][0]+dp[son][0],dp[root][1]+dp[son][1]-1)
       dp[root][1]=min(dp[root][1]+dp[son][0],dp[root][2]+dp[son][1]-1)
       dp[root][2]=dp[root][2]+dp[son][0]
P.S. 该题的数据量较大,要加#pragma comment(linker,"/STACK:1024000000,1024000000")语句,而且要用C++编译器,不能用G++。
 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
#define N 1000005
#define M 2000010
int vis[N],all,first[N],next[M],v[M],e,degree[N],dp[N][];
void addedge(int x,int y)
{
v[e]=y;
next[e]=first[x];
first[x]=e++;
}
void dfs(int root)
{
int i,k;
dp[root][]=dp[root][]=dp[root][]=;
vis[root]=;
for(i=first[root];i!=-;i=next[i])
{
k=v[i];
if(!vis[k])
{
dfs(k);
dp[root][]=min(dp[root][]+dp[k][],dp[root][]+dp[k][]-);
dp[root][]=min(dp[root][]+dp[k][],dp[root][]+dp[k][]-);
dp[root][]=dp[root][]+dp[k][];
}
}
}
int main()
{
int t,i,x,y;
scanf("%d",&t);
while(t--)
{
memset(dp,,sizeof(dp));
memset(first,-,sizeof(first));
e=;
all=;
int n;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
memset(vis,,sizeof(vis));
dfs();
all=min(dp[][],min(dp[][],dp[][]));
// cout<<all<<endl;
printf("%d\n",all*-);
}
return ;
}

本人入ACM时间尚短,写的不好的地方请多见谅。

HDU 4714 Tree2cycle DP 2013杭电热身赛 1009的更多相关文章

  1. hdu 4714 Tree2cycle dp

    用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...

  2. hdu 4714 Tree2cycle 树形经典问题

    发现今天没怎么做题,于是随便写了今天杭电热身赛的一题. 题目:给出一棵树,删边和添边的费用都是1,问如何删掉一些树边添加一些树边,使得树变成一个环. 分析:统计树的分支数.大概有两种做法: 1.直接d ...

  3. HDU 4704 Sum (2013多校10,1009题)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  4. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

  5. HDU 4714 Tree2cycle (树形DP)

    Tree2cycle Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Tot ...

  6. HDU 4714 Tree2cycle (树形DP)

    题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...

  7. HDU 4714 Tree2cycle(树型DP)

    解题思路: 将一棵树变成一个环.假设一个结点的分叉数目大于等于2.则将它与父节点断开.而且断开子结点数目sum - 2条边,并再次连接sum-2个儿子形成一条直链然后这条游离链与还有一条游离链相连,共 ...

  8. HDU 4714 Tree2cycle

    Tree2cycle dfs 不是根节点:如果边数大于等于2,则删除与父节点的边.并且是一条环,那么每个点的度数是2,则还要删除num(每个节点儿子数)-2,只留两个儿子.当然删除边的儿子也要连到环上 ...

  9. HDU 4714 Tree2cycle:贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...

随机推荐

  1. You have new mail in /var/spool/mail/root 烦不烦你(转)

    转自(http://blog.csdn.net/yx_l128125/article/details/7425182) 有时在进入系统的时候经常提示You have new mail in /var/ ...

  2. linux-信号。

    信号 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的. 信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到 ...

  3. PHP小记录

    正的framework(大量使用)      thinkphp(部分使用)      cakephpyii(极少使用) [一]函数    1:函数的声明:每个函数的第一行都是函数开头,有声明函数的关键 ...

  4. php内存申请和销毁

    内存申请 ZendMM使用自身heap层申请内存追踪结果: ZEND_ASSIGN_SPEC_CV_CONST_HANDLER (......) -> ALLOC_ZVAL(......) -& ...

  5. 已经安装php后,再增加扩展模块(不重新编辑php)

    下面以安装curl为例,介绍具体安装步骤. 1.安装crul wget http://curl.haxx.se/download/curl-7.19.6.tar.gz tar -zxvf curl-7 ...

  6. Remark of BLENDFUNCTION from MSDN

    Remarks When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, ...

  7. 重复数据插入unique列时,锁加在哪?

    1.测试目的 当插入重复数据到有unique索引的表中时,采用何种加锁机制. 2.测试思路 利用10046确定是什么操作导致加锁阻塞了进程: dump锁定前最近一次操作的块结构来分析加锁机制. 3.测 ...

  8. 贴板子系列_1-exgcd

    exgcd ll exgcd(ll a,ll b,ll &x,ll &y) { ) { x=;y=;return a; } ll r=exgcd(b,a%b,x,y); ll t=x; ...

  9. javascript第二遍基础学习笔记(一)

    1.兼容xhtml方法: <script> //<![CDATA[ ... ... //]]> </script> 2.文档模式: IE5.5引入,最初包含2种:混 ...

  10. VMware配置回环地址用于测试

           我们在开发过程中,很可能需要一台服务器用于测试,在这种环境下,我们很可能需要用到vmware来构建这样的开发环境.但如果当前处在一个离线,或是不在网内的环境下,我们所搭建的环境有可能无法 ...