HDU 4714 Tree2cycle(树型DP)
解题思路:
将一棵树变成一个环。假设一个结点的分叉数目大于等于2。则将它与父节点断开。而且断开子结点数目sum - 2条边,并再次连接sum-2个儿子形成一条直链然后这条游离链与还有一条游离链相连,共须要2*(sum-1)个操作,假设该结点为根结点,则一共须要2 * (sum - 2)种操作。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
using namespace std;
const int MAXN = 1000000 + 10;
int ans;
vector<int>G[MAXN];
int n;
int dfs(int u, int pre)
{
int sz = G[u].size();
int sum = 0;
for(int i=0;i<sz;i++)
{
int v = G[u][i];
if(v == pre) continue;
sum += dfs(v, u);
}
if(sum >= 2)
{
if(u == 1) ans += 2 * (sum - 2);
else ans += 2 * (sum - 1);
return 0;
}
return 1;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=0;i<=n;i++) G[i].clear();
ans = 0;
int u, v;
for(int i=1;i<n;i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1);
printf("%d\n", ans + 1);
}
return 0;
}
HDU 4714 Tree2cycle(树型DP)的更多相关文章
- HDU 4714 Tree2cycle (树形DP)
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- HDU 树型dp
HDU 4123 Bob's Race 题意:定义每个点的值为它到树上最远点的距离,每次询问q,回答最长的极值差小于等于q且编号连续的一段点的长度. 题解:求距离两次dp,求极值ST表+尺取法. HD ...
- HDU 5905 Black White Tree(树型DP)
题目链接 Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- HDU_1561_The more, The Better_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (J ...
- HDU_1011_Starship Troopers_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...
- HDU_1520_Anniversary party_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- POJ 3342 - Party at Hali-Bula 树型DP+最优解唯一性判断
好久没写树型dp了...以前都是先找到叶子节点.用队列维护来做的...这次学着vector动态数组+DFS回朔的方法..感觉思路更加的清晰... 关于题目的第一问...能邀请到的最多人数..so ea ...
随机推荐
- 集合(set)的基本操作
集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 集合中的元素必须是不可变类型 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 a = se ...
- mariadb数据库的链接查询和表格设计
链接查询 练习准备: --创建学生表 create table students ( id int unsigned not null auto_increment primary key, name ...
- mycat读写分离+垂直切分+水平切分+er分片+全局表 测试
原文http://blog.163.com/bigoceanwu@126/blog/static/172718064201683031639683/ 读写分离:利用最基础的mysql主从复制,事务性的 ...
- gps 地图
http://www.cnblogs.com/sylvanas2012/p/5342530.html http://blog.csdn.net/ma969070578/article/details/ ...
- COGS——T 826. [Tyvj Feb11] GF打dota
http://www.cogs.pro/cogs/problem/problem.php?pid=826 ★★☆ 输入文件:dota.in 输出文件:dota.out 简单对比时间限制:1 ...
- "pom.xml" could not be activated because it does not exist.
"pom.xml" could not be activated because it does not exist. 在sts中使用maven build,输入package然后 ...
- MySQL具体解释(9)----------索引具体解释
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点. 考虑例如以下情况.假设数据库中一个表有10^6条记录,DBMS的页面大小为4K.并存储100条记录.假设没有索引, ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
- 介绍Oracle自带的一些ASM维护工具 (kfod/kfed/amdu)
1.前言 ASM(Automatic Storage Management)是Oracle主推的一种面向Oracle的存储解决方式,它是一个管理卷组或者文件系统的软件.眼下已经被RAC环境广泛使用,可 ...
- Majority Element:主元素
Given an array of size n, find the majority element. The majority element is the element that appear ...