2016多校训练3_1007(hdu5758 Explorer Bo)
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <climits>
#include <utility>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <cctype>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <new>
#include <map>
#include <set>
using namespace std; typedef long long LL;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
#define PB push_back
#define FI first
#define SE second
#define gcd(x, y) __gcd(x, y)
#define gcd3(x, y, z) __gcd(__gcd(x, y), z) const double EPS = 1e-;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = + ;
const int MOD = 1e9 + ; int n;
vector<int> G[MAXN];
int dp[MAXN][], deg[MAXN], lf[MAXN]; void dfs(int u, int fa) {
dp[u][] = lf[u] = ;
for (int i = , sz = G[u].size(); i < sz; ++i) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
lf[u] += lf[v];
int d = (lf[v] & ) ? : ;
dp[u][] += dp[v][] + d;
}
dp[u][] = INF;
for (int i = , sz = G[u].size(); i < sz; ++i) {
int v = G[u][i];
if (v == fa) continue;
if (lf[v] == ) dp[u][] = min(dp[u][], dp[u][]);
int d = (lf[v] & ) ? : -;
dp[u][] = min(dp[u][], dp[u][] - dp[v][] + dp[v][] + d);
}
if (G[u].size() == ) lf[u] = ;
} int main() {
int T;
cin >> T;
while (T--) {
scanf("%d", &n);
for (int i = ; i < MAXN; ++i) {
G[i].clear();
}
memset(dp, , sizeof(dp));
memset(deg, , sizeof(deg));
memset(lf, , sizeof(lf));
for (int i = ; i < n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u].PB(v);
G[v].PB(u);
deg[u]++;
deg[v]++;
}
if (n == ) {
puts("");
continue;
}
int leaf = , rt;
for (int i = ; i <= n; ++i) {
if (deg[i] == ) {
leaf++;
} else {
rt = i;
}
}
dfs(rt, -);
printf("%d\n", dp[rt][leaf & ]);
}
return ;
}
2016多校训练3_1007(hdu5758 Explorer Bo)的更多相关文章
- HDU5758 Explorer Bo 思维+树形dp
题意自己看题目吧,挺短的. 思考过程:昨天感觉一天不做题很对不起自己,于是晚上跑到实验室打开别人树形dp的博客做了上面最后一个HDU的题,也是个多校题..一开始没有头绪了很久,因为起点不固定,所以这1 ...
- 【2016多校训练4】Multi-University Training Contest 4
1001 Another Meaning 题意:字符串A中包含的字符串B可以翻译或不翻译,总共有多少方案. 题解:动规,dp[i]表示A的第i位为止有多少方案. 转移方程: dp[i]=dp[i-1 ...
- HDU5758 Explorer Bo 树形dp
我是参考这一篇写的:http://blog.csdn.net/fsss_7/article/details/52049474 一点感想:dp[i][0]代表以这个点为根的且总叶子数为偶数个叶子的答案 ...
- HDU 5732 Subway(2016多校1J,树的重心 + 哈希)
题目链接 2016多校1 Problem J 题意 给定两棵相同的树,但是编号方案不同.求第一棵树上的每个点对应的第二棵树上的点.输出一种方案即可. 首先确定树的直径的中点.两棵树相等意味着两棵树 ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 2016多校联合训练4 F - Substring 后缀数组
Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...
- 2016多校联合训练contest4 1012Bubble Sort
Bubble Sort Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
随机推荐
- error C2872: “flann”: 不明确的符号 --- PCL 与OpenCV2 的flann命名空间冲突问题的解决方法
error C2872: "flann": 不明确的符号 - PCL 与OpenCV2命名空间冲突问题的解决方法 error C2872: "flann" 如果 ...
- pig入门教程(2)
本文可以让刚接触pig的人对一些基础概念有个初步的了解. 本文的大量实例都是作者Darran Zhang(website: codelast.com)在工作.学习中总结的经验或解决的问题,并且添加了较 ...
- window.parent ,window.top,window.self 详解及parent和opener的区别
window.parent ,window.top,window.self 详解 在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层fr ...
- linux学习第一周小结
这几天学习linux课程,安装环境,遇到不会的查询资料,在这个过程中发现了很多有意思的网页,看到了一些不一样的内容,现在对linux的学习兴趣增强了许多.学习解决问题也是很有意思的事情,解决问题的过程 ...
- 【转】webservice
一.WebService概念 Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它 ...
- (JAVA)String类型的逻辑语句编译
项目中遇到了动态配置条件触发相应事件的需求,需要根据String类型的逻辑语句,以及动态获取的数据,计算数据对应的结果,java实现.解决思路及代码实现如下,有需要的同学请自取. 一.需求提取 根据需 ...
- PHP中的继承
<?php class Bar { private $salary = 3000; public $lunch = 1000; // php中关于“可见性”的概念 public function ...
- JAVA之反射(一)
反射(一) ** 注:博主的这篇文章是在学习反射的时间写的如有问题请及时联系博主进行修改 ** 何为反射 这里也不说一些很官方的语言了,官方的说明看着头痛,总之一句话,就是在JAVA的运行状态的时候 ...
- this a sao
我就是来doubi的,顺便分享一下笔记,欢迎( ̄ε(# ̄)
- iOS拼图
#import "ViewController.h" @interface ViewController () @end @implementation ViewContro ...