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明显可以方便很多. 我们先把边权转化为点权, ...
随机推荐
- 栈应用之 背包问题(Python 版)
栈应用之 背包问题 背包问题描述:一个背包里可以放入重量为weight的物品,现有n件物品的集合s,其中物品的重量为别为w0,w1,...,wn-1.问题是能否从中选出若干件物品,其重量之和正好等于w ...
- Mysql Limit操作
oracle : || mysql: contact contact_ws 拼接 Font Size: Large | Medium | Small select * from tabl ...
- Ninject中如果在抽象类中使用了属性注入,则属性必须设置为protected或public
Ninject中如果在抽象类中使用了属性注入,则属性必须设置为protected或public 不能使用private,否则无法注入成功,会报null异常
- oracle数据库11g(11.2.0.1)安装报错:提示ins_ctx.mk编译错误。
https://blog.csdn.net/weixin_42967330/article/details/81668404
- bzoj 1271
思路:因为被占奇数次的点只有一个, 那么我们可以将数轴分成两部分,奇数次点之前的前缀和为偶数,之后的前缀和为奇数, 然后就可以二分了. #include<bits/stdc++.h> #d ...
- jenkins发布maven项目
(1)环境介绍 (2)配置ssh配置:系统管理--->系统设置 做这样的配置是方便打包之后war包或jar包复制到/tomcat/update目录下 (3)安装git 1丶不要使用1.8版本以下 ...
- R语言实战(八)广义线性模型
本文对应<R语言实战>第13章:广义线性模型 广义线性模型扩展了线性模型的框架,包含了非正态因变量的分析. 两种流行模型:Logistic回归(因变量为类别型)和泊松回归(因变量为计数型) ...
- maven设计思想
20171108 maven设计思想? archetype 插件 学习插件.
- Windows 中打开 shell
按住 Shift,单机鼠标右键"在当前目录打开命令窗口"
- Python时间日期格式化之time与datetime模块总结
1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...