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. 

题目大意:一棵有n个点的树,删边需要1的费用,增边需要1的费用,问最少需要多少费用才能得到一个环,不能用多余的边(即总共n条边)。

思路:首先我们可以这样考虑:我们先删掉x条边,那么之后再加上x+1条边,形成一个环。我们删掉x条边后,所有的点的度都不能大于2,那么就会出现多条链,再用x+1条边把这些链首尾相接就可以形成一个环。现在问题就转化成了给一棵树,问最少删掉多少条边,使得每个点的度不大于2。然后就是树状DP,用dp[i][0]表示,第i个点,连0个或1个子节点(度小于2)的最小费用。用dp[i][1]表示,第i个点,连0个或1个或2个子节点(度小于等于2)的最小费用。这样对每个点选择是不连或者连一个子节点,还是连两个子节点。然后随便搞,时间复杂度为O(n)。

PS:100W个点我看到好多人栈溢出了所以大家还是写非递归吧(实际上会不会溢出我不知道我没试过我一开始就写非递归)……我极少写非递归可能写得比较挫……

代码(2703MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = MAXN * ; int head[MAXN];
int stk[MAXN], stkp[MAXN], top;
int next[MAXE], to[MAXE];
int ecnt, n, T; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} int dp[MAXN][];
//0:连0 or 1个子节点,1:连两个子节点
int solve() {
top = ;
stk[top] = ; stkp[top] = head[];
while(top > ) {
int &p = stkp[top], u = stk[top];
if(to[p] == stk[top - ]) p = next[p];
if(p) {
int &v = to[p];
++top; stk[top] = v; stkp[top] = head[v];
p = next[p];
}
else {
int min1 = MAXN, min2 = MAXN;
dp[u][] = ;
for(int q = head[u]; q; q = next[q]) {
int &v = to[q];
if(v == stk[top - ]) continue;
++dp[u][];
dp[u][] += min(dp[v][], dp[v][]);
int x = dp[v][] - min(dp[v][], dp[v][]);
if(x < min1) {
min2 = min1;
min1 = x;
}
else min2 = min(min2, x);
}
int best = dp[u][];
dp[u][] = min(dp[u][], best - + min1);
dp[u][] = min(dp[u][], best - + min1 + min2);
--top;
}
}
return * min(dp[][], dp[][]) + ;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init();
int u, v;
for(int i = ; i < n; ++i) {
scanf("%d%d", &u, &v);
add_edge(u, v);
}
printf("%d\n", solve());
}
}

HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)的更多相关文章

  1. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  2. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  3. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  6. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  7. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  8. HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description N soldiers from the famous "*FFF* army" is standing in a line, from left to ri ...

  9. HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)

    Problem Description   Zero and One are good friends who always have fun with each other. This time, ...

随机推荐

  1. Mysql jar包

    密码cngb https://pan.baidu.com/share/init?surl=bSGA6T-LTwjx-qaNAiipCA

  2. Plupload使用API

    Plupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统的<input type=”file” />.Plupload会自动侦测当前 ...

  3. Js操作DOM及获取浏览器高度以及宽度

    1.获取网页可见区域的宽度:document.body.clientWidth ; 2.获取网页可见区域的高度:document.body.clientHeight; 3.获取 网页可见区域宽:doc ...

  4. 小a和uim之大逃离(dp)

    题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...

  5. SpringBoot配置全局自定义异常

    不同于传统集中时Springmvc 全局异常,具体查看前面的章节https://www.cnblogs.com/zwdx/p/8963311.html 对于springboot框架来讲,这里我就介绍一 ...

  6. 使用virtual安装Windows系列操作系统总结

    最近在安装Windows操作系统的过程中,发现总是报错,无法安装成功,后来经过不断地摸索,发现根本的问题在于镜像,所以在以后的大文件传输下载后,一定要校验其MD5值是否与源文件一致,需要的朋友可以联系 ...

  7. SSH远程登录和端口转发详解

     SSH远程登录和端口转发详解   介绍 SSH 是创建在应用层和传输层基础上的安全协议,为计算机上的 Shell(壳层)提供安全的传输和使用环境. SSH 只是协议,有多种实现方式,本文基于其开源实 ...

  8. MariaDB数据库服务

    一.初始化mariaDB服务程序: yum install mariadb mariadb-server           //安装mariaDB systemctl start mariadb   ...

  9. 多线程编程之Apue3rd_Chapter15.10之posix信号量

    看了APUE的chapter15,只重点看了15.10,学习了posix信号量.Posix信号量比起xsi信号量的优点是性能更好,在Linux3.2.0平台上性能提升很大.其中命名信号量使用方法如下. ...

  10. 小程序开发-7-访问api数据与ES6在小程序中的应用

    访问API数据与ES6在小程序中的应用 看待组件的两种观点 组件复用 代码分离-(特别重要) 不能在一个页面写所有的代码,代码分离具有很强的可读性.可维护性 Blink Api 介绍与测试API ur ...