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 ...
随机推荐
- [413D][搜索]D - Field expansion
http://codeforces.com/contest/799/problem/D 解题关键:因为3^11>100000,所以若只把2单独拿出,最多只需要暴力2^11次,故只需要dfs一下即 ...
- Impala介绍
Impala介绍 Impala支持的文件格式 Impala可以对Hadoop中大多数格式的文件进行查询.它能通过create table和insert的方式将一部分格式的数据加载到table中,但值得 ...
- 介绍一款网站前台图片滚动插件之"switchable"
一.简单介绍:jQuery.Switchable是一款整合了Tabs.Slide.Scrollable等常见UI组件的jQuery插件,在这里,简答说说他的Slide.像Tabs,在Jquery-UI ...
- C习题练习
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h> //比较俩个数的大小 //int max(int a, int b) {// int t ...
- select下拉框如何与接口配合动态生成option选项
前几天在做任务时考虑到这个问题,具体任务用例如下: HTML: JS:
- bootstrap初学者模板
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> ...
- oracle数据库rownum讲解(转)
https://blog.csdn.net/qq_40794266/article/details/78698321
- Hibernate单表映射学习笔记之一——hibernalnate开发环境配置
1.什么是ORM? Object/Relationship Mapping:对象/关系映射 2.写SQL语句不好之处: (1)不同数据库使用的SQL语法不同(PL/SQL.T/SQL) (2)同样的功 ...
- 洛谷P1072 Hankson 的趣味题
P1072 Hankson 的趣味题 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一 ...
- pod 安装
cocoapods 简介: CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods本身所 ...