HDU 5758 Explorer Bo
思维,树形$dp$。
首先选择一个度不为$0$的节点作为根节点,将树无根转有根。
这题的突破口就是要求瞬间移动的次数最少。
次数最少,必然是一个叶子节点走到另一个叶子节点,然后瞬间移动一次,再从一个叶子节点走到另一个叶子节点,然后瞬间移动一次……
因为叶子节点总数可能是奇数,可能是偶数,那么接下来要分类讨论一下了。
叶子节点总数为偶数:
这种情况下,叶子节点可以两两配对。如果 下面叶子节点个数是偶数,那么$<u,v>$这条边通过的次数就是$2$;如果$v$下面叶子节点个数是偶数,那么$<u,v>$这条边通过的次数就是$1$次(画图就可以知道)。知道了每条边的通过次数,那么答案就是每条边通过次数的总和。
叶子节点总数为奇数:
这种情况下,与上面偶数的情况唯一不同的就是:有一个叶子节点无法配对。那么我们只需枚举哪一个叶子节点不配对即可。枚举从$root$开始,$dfs$一次就可以了。
这种题目虽然涉及到的知识量极少,但是思维量极高,对于这种题目弱渣表示暂时完全不能独立想出来。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=+;
int T,n,ans;
struct Edge{int u,v,nx;}e[*maxn];
int root,h[maxn],sz;
int f[maxn],r[maxn],d[maxn]; void add(int u,int v)
{
e[sz].u=u; e[sz].v=v; e[sz].nx=h[u]; h[u]=sz++;
} void dfs(int x,int fa)
{
int sum=; f[x]=d[x]=;
for(int i=h[x];i!=-;i=e[i].nx)
{
if(fa==e[i].v) continue;
sum++; dfs(e[i].v,x); f[x]=f[x]+f[e[i].v];
if(f[e[i].v]%==) d[x]=d[x]+; else d[x]=d[x]+;
}
if(sum==) f[x]=;
} void Find(int x,int fa,int Ans)
{
for(int i=h[x];i!=-;i=e[i].nx)
{
if(fa==e[i].v) continue;
if(f[e[i].v]==) { ans=min(ans,Ans); continue; }
else
{
if(f[e[i].v]%==) Find(e[i].v,x,Ans-);
else Find(e[i].v,x,Ans+);
}
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(h,-,sizeof h); memset(r,sz=,sizeof r); for(int i=;i<=n-;i++)
{
int u,v; scanf("%d%d",&u,&v);
r[u]++; r[v]++; add(u,v); add(v,u);
}
if(n==) { printf("1\n"); continue; }
if(n==) { printf("0\n"); continue; } for(int i=;i<=n;i++) if(r[i]!=) { root=i; break; } dfs(root,); ans=;
for(int i=;i<=n;i++)
{
if(i==root) continue;
if(f[i]%==) ans=ans+; else ans=ans+;
} if(f[root]%==) printf("%d\n",ans);
else { Find(root,,ans); printf("%d\n",ans); }
}
return ;
}
HDU 5758 Explorer Bo的更多相关文章
- HDU 5758 Explorer Bo(树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...
- HDU 5752 Sqrt Bo (数论)
Sqrt Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 5762 Teacher Bo (暴力)
Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
- HDU 5752 Sqrt Bo【枚举,大水题】
Sqrt Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- hdu 5761 Rower Bo 物理题
Rower Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 Description There is a river on the Ca ...
- hdu 5762 Teacher Bo 暴力
Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
- hdu 5753 Permutation Bo 水题
Permutation Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- hdu 5752 Sqrt Bo 水题
Sqrt Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...
随机推荐
- Python中Generators教程
转载至:https://www.bytelang.com/article/content/NQbmUaRIXyA= 要想创建一个iterator,必须实现一个有__iter__()和__next__( ...
- 在Node.js中使用RabbitMQ系列一 Hello world
在前一篇文章中可伸缩架构简短系列中提到过关于异步的问题.当时推荐使用RabbitMQ来做任务队列的实现方案.本篇文章以Node.js为例子,来实际操作如何和RabbitMQ进行交互. 介绍 Rabbi ...
- SQL注入(四)
参数绑定(预编译语句) 虽然数据库自带的过滤是个不错的实现,但是我们还是处在“用户输入被当成 SQL语句的一部分 ”这么个圈子里,其实要跳出这个圈子还有一个实现,就是参数绑定.基本上所有的主流数据库都 ...
- CodeForces 300C Beautiful Numbers
枚举,组合数,逆元. 枚举$a$用了$i$个,那么$b$就用了$n-i$个,这个时候和$sum=a*i+b*(n-i)$,判断$sum$是否满足条件,如果满足,那么答案加上$C(n,i)$. #inc ...
- 遍历HashMap的最佳方式
public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { ...
- C# 语言规范_版本5.0 (第19章 附录A_文档注释)
A. 文档注释 C# 提供一种机制,使程序员可以使用含有 XML 文本的特殊注释语法为他们的代码编写文档.在源代码文件中,可以使用特定形式的注释来指导工具从这些注释及其后的源代码元素生成 XML.使用 ...
- 字符函数库 - cctype 和 climits 中的符号常量
一. C++从C语言中继承一个与字符相关的.非常方便的函数软件包,他可以简化诸如确定字符是否为大写字母‘数字.标点符号等工作,这些函数的原型在头文件cctype(老式的为ctype.h)中定义的.例如 ...
- 最近一段OI学习计划
1.在寒假的时间里尽量吧图论和DP的基础先学一下: 图论:数,二叉树,DFS.BFS遍历,然后最短路径(Floyd.dijkstra.SPFA),然后再最小生成树吧,如果还有时间的话(kruskal( ...
- urllib模块 | Python 2.7.11
官方文档: https://docs.python.org/2/library/urllib.html 某博客对官方文档较全的翻译: http://h2byte.com/post/tech/relat ...
- trove,测试,db小解析
# Copyright 2014 Tesora Inc.# All Rights Reserved.## Licensed under the Apache License, Version 2.0 ...