#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)的更多相关文章

  1. HDU5758 Explorer Bo 思维+树形dp

    题意自己看题目吧,挺短的. 思考过程:昨天感觉一天不做题很对不起自己,于是晚上跑到实验室打开别人树形dp的博客做了上面最后一个HDU的题,也是个多校题..一开始没有头绪了很久,因为起点不固定,所以这1 ...

  2. 【2016多校训练4】Multi-University Training Contest 4

    1001  Another Meaning 题意:字符串A中包含的字符串B可以翻译或不翻译,总共有多少方案. 题解:动规,dp[i]表示A的第i位为止有多少方案. 转移方程: dp[i]=dp[i-1 ...

  3. HDU5758 Explorer Bo 树形dp

    我是参考这一篇写的:http://blog.csdn.net/fsss_7/article/details/52049474 一点感想:dp[i][0]代表以这个点为根的且总叶子数为偶数个叶子的答案 ...

  4. HDU 5732 Subway(2016多校1J,树的重心 + 哈希)

    题目链接  2016多校1 Problem J 题意  给定两棵相同的树,但是编号方案不同.求第一棵树上的每个点对应的第二棵树上的点.输出一种方案即可. 首先确定树的直径的中点.两棵树相等意味着两棵树 ...

  5. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  6. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  7. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

  8. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

  9. 2016多校联合训练contest4 1012Bubble Sort

    Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

随机推荐

  1. java高并发前言

  2. SqlServer2012-创建表、删除表 增加字段 删除字段操作

    新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' nu ...

  3. Web调试利器fiddler介绍

    转载:http://blog.chinaunix.net/uid-27105712-id-3738821.html 最近在使用fiddler,发现这个真是非常最犀利的web调试工具,笔者这里强烈推荐给 ...

  4. Note: Migratory Compression: Coarse-grained Data Reordering to Improve Compressibility

    思路/方法 设计了Migratory Compression. 调整chunk相对位置,将相似chunk排列在一起,通过压缩来减少data store占用的实际存储空间. https://en.wik ...

  5. 如何给自己的开源项目选择和添加 License

    License 的作用:开源 == 为所欲为? 开源并不等于为所欲为! 代码的用途,修改之后的代码有什么要求,开源程序对于原作者的权利和责任等等,都是需要明确的. 开源协议 License 就是这么一 ...

  6. ubuntu 安装 mysql 5.7 简记

    安装: apt-get install mysql-server 会安装最新版本的 mysql ,安装时会提示输入 root 的密码 进入 mysql: mysql -u root -p 进入后会出现 ...

  7. jQuery点击弹出层,弹出模态框,点击模态框消失

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. DNS 网关 路由 交换机 网桥 协议 服务器 这些都是什么关系?

    来源:知乎 服务器:为你提供服务的机器.相当于马路边上的各种店面.虽然理论上任何一户人家都能开店为你提供服务,但是因为各种硬件资源限制而不适合开店.比如:小区道路比较窄(宽带带宽比较窄).家里地方太小 ...

  9. 解读人:李琼,Metabolic profiling of tumors, sera and skeletal muscles from an orthotopic murine model of gastric cancer associated-cachexia(胃癌相关恶病质的原位小鼠模型中肿瘤,血清和骨骼肌的代谢谱分析)

    发表时间:(2019年4月) IF:3.950 单位: 厦门大学 厦门理工大学 物种:小鼠的肿瘤,血清和骨骼肌 技术:核磁共振代谢组学分析 一. 概述:(用精炼的语言描述文章的整体思路及结果) 本研究 ...

  10. IDEA的git密码修改

    问题: 如果你办公的电脑是同事用过,在每次提交git的时候都显示是他的名字.想要修改提交git用户名密码. 但是博客idea 修改Git密码和账号方法所示方法无效.且操作系统是win10.(其他操作系 ...