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 ...
随机推荐
- Unity堆内存优化
unity中减少堆内存分配以减少垃圾回收处理:只有局部变量且为值类值的变量是从stack栈中分配内存,其它所有情况都是从heap堆中分配内在.* 缓存获取到的数据.* 频繁被调用的函数中尽量少的分配空 ...
- Luogu 2258 [NOIP2014] 子矩阵
被普及组虐了,感觉
- Entity Framework Code-First(7):Inheritance Strategy
Inheritance Strategy in Code-First: We have seen in the Code First Conventions section that it creat ...
- win7 失去焦点解决方案
将HKEY_CURRENT_USER\Control Panel\Desktop中的ForegroundLockTimeout的选项,改成十进制的200000毫秒或者十六进制30d40. 参考链接: ...
- Python开发【第二篇】: 基本数据类型(一)
1. 整型 整型即整数,用 int 表示,在 Python3 中整型没有长度限制. 1.1 内置函数 1. int(num, base=None) int( ) 函数用于将字符串转换为整型 ...
- 2017乌鲁木齐区域赛I(带权并查集)
#include<bits/stdc++.h>using namespace std;int f[200010];//代表元long long rl[200010];//记rl[i]为结点 ...
- 反射实现增删改查(DAO层)——查询数据
先贴出代码,后续补充自己的思路.配置文件.使用方式: /** * * 数据查询 * */ @Override public List<?> queryObject(List<Map& ...
- 用vue写一个仿简书的轮播图
原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...
- Python的自增运算与Python变量的浅析
一.关于Python的自增运算 学了C/C++后再学习Python,不自觉地就打出了自增运算符++,但是发现Python解释器不认识,查了下资料,发现Python中没有这个运算符.这里暂时不探讨自增运 ...
- JML契约式设计——第三单元学习小结
一.前言 本单元作业都是关于JML(Java Modeling Language),JML是一种契约式设计(Design by Contract)的语言,契约式设计的主要目的是希望程序员能够在设计程序 ...