HDU 4714 Tree2cycle (树形DP)
Tree2cycle
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 324 Accepted Submission(s): 54
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.
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).
4
1 2
2 3
2 4
In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.
简单树形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)的更多相关文章
- HDU 4714 Tree2cycle (树形DP)
题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...
- hdu 4714 Tree2cycle 树形经典问题
发现今天没怎么做题,于是随便写了今天杭电热身赛的一题. 题目:给出一棵树,删边和添边的费用都是1,问如何删掉一些树边添加一些树边,使得树变成一个环. 分析:统计树的分支数.大概有两种做法: 1.直接d ...
- hdu 4717 Tree2cycle(树形DP)
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- HDU 4714 Tree2cycle DP 2013杭电热身赛 1009
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...
- hdu 4714 Tree2cycle dp
用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 6201 【树形dp||SPFA最长路】
http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
随机推荐
- 洛谷P2261余数求和
传送门啦 再一次见证了分块的神奇用法,在数论里用分块思想. 我们要求 $ ans = \sum\limits ^{n} _{i=1} (k % i) $ ,如果我没看错,这个题的暴力有 $ 60 $ ...
- ROS数据可视化工具Rviz和三维物理引擎机器人仿真工具V-rep Morse Gazebo Webots USARSimRos等概述
ROS数据可视化工具Rviz和三维物理引擎机器人仿真工具V-rep Morse Gazebo Webots USARSimRos等概述 Rviz Rviz是ROS数据可视化工具,可以将类似字符串文本等 ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- c语言循环链表的问题
今天,老师说了一道题,大意是,有一群小朋友10个人,但是老师只有一个苹果,只能给一个小朋友,于是老师就决定让小朋友们做成一圈,从第一个小朋友开始,每隔一个小朋友就没有机会得到苹果,最后剩下的一个人可以 ...
- Pyhton核心编程-Chap2习题-DIY
在学Python,在看<Python核心编程>的pdf,做了Chap2的题目,答案为DIY # Filename: 2-11.py # Author: ChrisZZ mylist = [ ...
- mysql千万级表关联优化
MYSQL一次千万级连表查询优化(一) 概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差 ...
- Centos 常用命令[持续积累中...]
CentOS常用到的查看系统命令 uname -a cat /etc/issue /sbin/ifconfig # 查看内核/操作系统/CPU信息 head -n 1 /etc/issue # 查看操 ...
- CRLF LF CR
The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without ...
- pip-django-cms
pip install django-el_pagination pip install django-ckeditor
- 移动端css单位之 “vh” & “vw”
一.前言: 响应式web设计离不开百分比.但是,CSS百分比并不是所有的问题的最佳解决方案.CSS的宽度是相对于包含它的最近的父元素的宽度的.但是如果你就想用视口(viewpoint)的宽度或者高度, ...