HDU 4714 Tree2cycle (树形DP)

时间:2022-08-26 22:27:12

Tree2cycle

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

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
Hint

In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.

 
Source
 
Recommend
liuyiding
 

简单树形DP来一发

用dp1表示形成一颗链,而且该点在端点。

dp2表示形成一颗链需要的最少步数

 /* *******************************************
Author : kuangbin
Created Time : 2013年09月08日 星期日 12时00分01秒
File Name : 1009.cpp
******************************************* */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
vector<int>vec[MAXN];
int dp1[MAXN];
int dp2[MAXN];
void dfs(int u,int pre)
{
int sz = vec[u].size();
int sum1 = ;
int maxn = , maxid = -;
int smaxn = , smaxid = -;
for(int i = ;i < sz;i++)
{
int v = vec[u][i];
if(v == pre)continue;
dfs(v,u);
sum1 += dp2[v]+;
int tmp = dp1[v] - (dp2[v] + );
tmp = -tmp;
if(tmp > smaxn)
{
smaxn = tmp;
smaxid = v;
if(smaxn > maxn)
{
swap(smaxn,maxn);
swap(smaxid,maxid);
}
}
}
dp1[u] = sum1 - maxn;
dp2[u] = sum1 - maxn - smaxn; }
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int u,v;
for(int i = ;i <= n;i++)
vec[i].clear();
for(int i = ;i < n;i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(,-);
cout<<dp2[]+<<endl;
}
return ;
}

HDU 4714 Tree2cycle (树形DP)的更多相关文章

  1. HDU 4714 Tree2cycle &lpar;树形DP&rpar;

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

  2. hdu 4714 Tree2cycle 树形经典问题

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

  3. hdu 4717 Tree2cycle&lpar;树形DP&rpar;

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

  4. HDU 4714 Tree2cycle DP 2013杭电热身赛 1009

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...

  5. hdu 4714 Tree2cycle dp

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

  6. HDU 2196&period;Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  8. hdu 6201 【树形dp&vert;&vert;SPFA最长路】

    http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...

  9. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

随机推荐

  1. Android新组件RecyclerView介绍,其效率更好

    今天我们首先来说为什么要介绍这个新组件RecyclerView,因为前几天我发布了一个常用面试题ListView的复用及如何优化的文章,介绍给一些开发者,但是我看到有关的反馈说:现在都不再用listv ...

  2. wget 增加单个文件下载限制大小

    增加了参数 -M --limit-size 使用方法 -M 5m 或者 -M 500k 或者 --limit-size=5m 或者 --limit-size=500k 下载地址 http://pan. ...

  3. FZU 1061 矩阵连乘

    用栈来算一算就可以了. #include<iostream> #include<algorithm> #include<cstdio> #include<cs ...

  4. Codeforces&period;547D&period;Mike and Fish&lpar;思路 欧拉回路&rpar;

    题目链接 \(Description\) 给定平面上n个点,将这些点染成红or蓝色,要求每行.每列红色点与蓝色点数量的差的绝对值<=1.输出方案(保证有解). \(Solution\) 参考这 ...

  5. C&num;编程(六十)----------LINQ的概述

    LINQ的概述 LINQ的全名为语言继承查询,是VS2008个.NET3.5版中一款突破性的创新,他再对象领域和数据领域之间架起了一座桥梁.使用LINQ能大大加快对于对象数据等等的查询,加快效率. 由 ...

  6. table边框和td的width失效

    table元素有一个属性border,可设置table的边框.这个边框对内部元素有效. 不同于style:border,这个仅仅是外边框. table{ width:60%; border-colla ...

  7. Cocos2dx源码赏析&lpar;3&rpar;之事件分发

    Cocos2dx源码赏析(3)之事件分发 这篇,继续从源码的角度赏析下Cocos2dx引擎的另一模块事件分发处理机制.引擎的版本是3.14.同时,也是学习总结的过程,希望通过这种方式来加深对Cocos ...

  8. PHP与ASP&period;NET的优劣比较

    PHP与ASP.NET的比较 表 1 PHP 4 PHP5 ASP.NET 软件价格 免费 免费 免费 平台价格 免费 免费 $$ 速度 强 强 弱 效率 强 强 弱 安全性 强 强 强 平台 强 强 ...

  9. spring boot容器加载完后执行特定操作

    有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...

  10. BZOJ4070 &lbrack;Apio2015&rsqb;雅加达的摩天楼 【分块 &plus; 最短路】

    题目链接 BZOJ4070 题解 考虑暴力建图,将每个\(B_i\)向其能到的点连边,复杂度\(O(\sum \frac{n}{p_i})\),当\(p\)比较小时不适用 考虑优化建图,每个\(dog ...