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 ...
随机推荐
- 关于Ueditor 前后端分离实现文件上传到独立服务器的问题 望大神们赐教
最近,由于网站实现多台服务器负载均衡,导致编辑器上传文件需要同步,可是使用同步软件太慢,不太现实,所以想到实现编辑器上传文件直接上传到独立文件服务器.可是没想到遇到坑了. 1.在本地IIS 中添加网站 ...
- linux服务器开发二(系统编程)--进程相关
进程相关的概念 程序与进程 程序,是指编译好的二进制文件,在磁盘上,不占用系统资源(CPU.内存.打开的文件.设备.锁等等). 进程,是一个抽象的概念,与操作系统原理联系紧密.进程是活跃的程序,占用系 ...
- [HMLY]12.iOS中的Protocol
最近工作中遇到一个比较迷惑的事情,在我利用runtime获取类的属性的时候,由于类实现了一个自定义协议,导致遍历出来的属性中包含了NSObject协议中的property.查来查去,只是知道和prot ...
- es5 数组
一. 数组申明 var a = new Array(5) 会生成一个长度为5 每个元素都是undifined的数组 var a = new Array(1,2,3) 跟 var a = [1,2,3 ...
- dev gridcontrol把event事件转换成命令
可以通过继承gridcontrol的形式来实现 定义DataGridDoubleClickCommand依赖属性 class MyGridControl : GridControl { ...
- html5的navigator调用手机震动
navigator.vibrate(s) 或 navigator.webkitVibrate(s),不过该属性只在安卓系统有效.
- AForge.net 使用之录像拍照功能实现
连接摄像头设备,这里需要引入 AForge.Video; AForge.Video.DirectShow; AForge.Video.FFMPEG; 还需要添加引用,aforge.dll,aforge ...
- NGINX----源码阅读---sources配置脚本
/auto/sources 本文件主要用于对源文件环境变量进行初始化,主要包括:.c和.h文件. 对应变量类型有*_MODULES.*_INCS.*_DEPS等. # Copyright (C) Ig ...
- HTTP请求返回状态详解
当用户试图通过 HTTP 访问一台正在运行 Internet 信息服务 (IIS) 的服务器上的内容时,IIS 返回一个表示该请求的状态的数字代码.状态代码可以指明具体请求是否已成功,还可以揭示请求失 ...
- CodeForces 671A Recycling Bottles
暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...