HDU 4714 Tree2cycle (树形DP)
题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环。
析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定要把它从父结点上剪下来是最优的,
因为如果这样剪下来再粘上花费是2(先不管另一端),如果分别剪下来再拼起来,肯定是多花了,因为多了拼起来这一步,知道这,就好做了。先到叶子结点,
然后再回来计算到底要花多少。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int next, to;
};
Edge edge[maxn<<1];
int cnt, ans;
int head[maxn]; void add(int u, int v){
edge[cnt].to =v;
edge[cnt].next = head[u];
head[u] = cnt++;
} int dfs(int u, int fa){
int tmp = 0;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
tmp += dfs(v, u);
} if(tmp >= 2){
if(1 == u) ans += 2 * (tmp - 2);
else ans += 2 * (tmp - 1);
return 0;
}
return 1;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
int u, v;
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 1; i < n; ++i){
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
ans = 1;
dfs(1, -1);
printf("%d\n", ans);
}
return 0;
}
HDU 4714 Tree2cycle (树形DP)的更多相关文章
- 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)
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明显可以方便很多. 我们先把边权转化为点权, ...
随机推荐
- Hibernate 一对一、一对多、多对多注解cascade属性的总结
作用:是否级联被注解字段里面的对象.可选值:javax.persistence.CascadeType.PERSIST, MERGE, REMOVE, REFRESH, DETACH, ALL.可选其 ...
- PyQt 5的基本功能
PyQt5常用的模块 PyQt5的类别分为几个模块,包括: QtCore:包含非核心的GUI功能,此模块用于处理时间.文件和目录.各种数据类型.流.URL.MIME类型.线程或进程 QtGui:包括窗 ...
- Collection集合学习(二)———List接口与具体实现
二.List接口: 一个可以包含重复元素的Collection,List中的元素不会自动排序,元素顺序由添加时的顺序决定. 具体实现类包括Vector(线程安全的),ArrayList,LinkedL ...
- Oracle 10g RAC TAF
Oracle RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会影 ...
- 【转】C#中Func与Action的理解
原文地址:https://www.cnblogs.com/ultimateWorld/p/5608122.html Action 与 Func是.NET类库中增加的内置委托,以便更加简洁方便的使用委托 ...
- Tkinter LabelFrame
Tkinter LabelFrame: 在一个labelframe一个简单的容器构件.其主要目的是作为一个间隔或复杂的窗口布局容器. 在一个labelframe一个简单的容器构件.其主要目的是作 ...
- django第一课大概了解
项目目录介绍: -------- manage.py : Django项目里面的工具,通过它可以调用django shell和数据库等. mysite/ 主要是项目配置的信息,通常一次生成后不需要其他 ...
- C#内存流示例----->用内存流来读取图片
背景:一个窗体.一个pictureBox.一个lable[没有选择图片,lable的text为"图片未选择"],在pictureBox1的Click事件中添加如下代码: priva ...
- 143. Reorder List(List)
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...
- 原生JS的使用,包括jquery和原生JS获取节点、jquery和原生JS修改属性的比较
一.前言 相比于JS这条直达终点.满是荆棘的小路,jquery无疑是康庄大道了,足够的简洁.易用给了它辉煌的地位.然而,毕竟是绕着道的插件,当小路走着走着变成大路的时候,曾经的大路也就失去了他自身的优 ...