#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. TCP/IP以及Socket对象基本

    1 OSI七层模型概念介绍 物理层:数据以比特的方式进行传递,典型的设备是集线器.该层主要规定了设备的电压或者端口等等一些列物理层面上的规定     数据链路层:该层数据以帧的方式进行传递,主要是两个 ...

  2. 关于login/interactive/no-interactive shell和profile/bash_profile/bashrc

    login shell:第一次登录进系统时的shell,一般是指本机启动时的控制台shell或者ssh远程登录时的shell. interactive shell:登录以后,再打开控制台时运行的she ...

  3. Struts2学习第二课 Struts2概述

    Struts2是一个用来开发MVC应用程序的框架,它提供了Web应用程序开发过程中的一些常见问题飞解决方案: -对来自用户的输入数据进行合法性验证 -统一的布局 -可扩展性 -国际化和本地化 -支持A ...

  4. 5.Windows应急响应:挖矿病毒

    0x00 前言 随着虚拟货币的疯狂炒作,挖矿病毒已经成为不法分子利用最为频繁的攻击方式之一.病毒 传播者可以利用个人电脑或服务器进行挖矿,具体现象为电脑CPU占用率高,C盘可使用空间骤降, 电脑温度升 ...

  5. VSS配置

    http://www.cnblogs.com/FreeDong/articles/2193151.html

  6. SQL中的union,except,intersect用法

    限制:所有查询中的列数和列的数序必须相同 union all:完全整合两个结果集查出所有数据 union:查出两个表的数据并且去除重复的数据 except:去重之后只会保留第一个表中的数据,查询a表在 ...

  7. 【Spring-任务调度】

    Spring-任务调度: spring内部有一个task是Spring自带的一个设定时间自动任务调度task使用的时候很方便,但是他能做的东西不如quartz那么的多!可以使用注解和配置两种方式,配置 ...

  8. .NET 中文件嵌套,例如:cshtml文件下面嵌套css和js【机器翻译】

    越来越多的我发现自己在我的一些较大的Web项目中丢失了文件.有很多内容要处理 - HTML视图,几个派生CSS页面,页面级CSS,脚本库,应用程序范围的脚本和页面特定的脚本文件等等.幸运的是,我使用R ...

  9. nginx优化配置大全

    由于面试被问到nginx优化做过哪些 后来发现,其实做过的也不少,忘了的更不少,因此写个博客记录一下(里面有一些内容来源于其他作者). 配置文件样例为生产环境样例. 1.nginx基本优化 安装方式有 ...

  10. SMTP服务器配置

    Windows Server 2012/2012 R2:安装和配置 SMTP 服务器 安装 SMTP 服务器 以下是安装 SMTP 服务器功能的步骤: 1. 打开“服务器管理器”:单击键盘上的 Win ...