ZOJ 3949 Edge to the Root( 树形dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949
题解:树dp真的很直觉,或者说dp真的很直觉。就上周末比赛时其实前一半的观察我都找到了,也感觉就是O(N)的,但就是不会转移。观察其实就是说这条边会使得x这个子树的距离变少,以及从1到x这条链的中点+1的这个点的子树的距离变少。那么怎么从fa(x)转移过来呢。对于dep[x]/2+1的子树,因为x相对于fa(x)变深了,所以子树内的每一个都要+1,然而对于x的子树中的每一个点深度相对于从fa(x)变浅了所以-1,那么也就等价一个+1,一个-2。然后记录答案的那个数组要开ll。PS:为什么会有第一发超时,第二发AC的操作,也许有点小卡常。。。这好像是我第一次在ZOJ上交题诶。代码不长,想了很久。
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls (x<<1)
#define rs (x<<1|1)
#define all(x) x.begin(),x.end()
#define ll long long
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forr(i,a,b) for(int i=a;i>=b;i--)
#define Fr(i,a,b) for(int i=a;i<b;i++)
#define Frr(i,a,b) for(int i=a;i>b;i--)
#define pll pair<ll,ll>
using namespace std;
const int maxn=2e5+100;
const ll inf=0x3f3f3f3f;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int T;
int n;
int siz[maxn],depth[maxn];
int d[maxn];
ll ans[maxn];
vector<int>vec[maxn];
void dfs(int x,int fa)
{
siz[x]=1;depth[x]=depth[fa]+1;
for(int i=0;i<vec[x].size();i++){
int tt=vec[x][i];
if(tt!=fa){
dfs(tt,x);
siz[x]+=siz[tt];
}
}
}
void dfs1(int x,int fa)
{
d[depth[x]]=x;
int tt=depth[x]/2+1;
if(fa==0||fa==1){
ans[x]=ans[1];
}
else
{
ans[x]=ans[fa]+siz[d[tt]]-2*siz[x];
}
for(int i=0;i<vec[x].size();i++)
{
int tk=vec[x][i];
if(tk!=fa)dfs1(tk,x);
}
}
int main()
{
int p,q;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)vec[i].clear();
for(int i=1;i<n;i++)
{
scanf("%d",&p);scanf("%d",&q);
vec[p].pb(q);vec[q].pb(p);
}
depth[0]=-1;
dfs(1,0);
ans[1]=0;
for(int i=2;i<=n;i++)ans[i]=1e18;
for(int i=1;i<=n;i++)ans[1]+=depth[i];
//cout<<ans[1]<<"\n";
dfs1(1,0);
ll res=ans[1];
for(int i=2;i<=n;i++)res=min(res,ans[i]);
printf("%lld\n",res);
}
}
ZOJ 3949 Edge to the Root( 树形dp)的更多相关文章
- ZOJ 3949 Edge to the Root(想法)(倍增)
Edge to the Root Time Limit: 1 Second Memory Limit: 131072 KB Given a tree with n vertices, we ...
- ZOJ 3949 Edge to the Root(树形DP)
[题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...
- ZOJ 3949 Edge to the Root
题意: 在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和. 思路: 我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上, ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...
- ZOJ 3805 (树形DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...
- ZOJ 3201 树形dp+背包(简单题)
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...
- ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...
- 树dp...吧 ZOJ 3949
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...
随机推荐
- Java SimpleDateFormat处理日期与字符串的转换
1.为什么要使用SimpleDateFormat? 在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出: import java.util.D ...
- 如何将workerman部署到windows服务器上面
一直以来对php的即时通讯都很好奇,其实是不知道应该怎么来实现,后来了解到了swoole和workerman这两个神器,他们都可以实现即时通信的功能,其中swoole是C语言编写的php扩展,其配置起 ...
- 一起学Android之ViewPager
本文以一个简单的小例子,简述在Android开发中ViewPager的常见用法,仅供学习分享使用. 概述 ViewPager是一个支持使用者左右滑动的布局管理控件,可以通过一个实现的(适配器)Page ...
- maven pom 属性介绍
maven pom属性 内置属性(预定义,可直接使用) ${basedir} 表示项目根目录,即包含pom.xml文件的目录; ${version} 表示项目版本; ${project.basedir ...
- 简单易懂的单元测试框架-gtest(二)
简介 事件机制用于在案例运行前后添加一些操作(相当于挂钩函数).目前,gtest提供了三种等级的事件,分别: 全局级,所有案例执行的前后 TestSuite级,某一个案例集的前后 TestCa ...
- There is already an object named '#xxxx' in the database.
这个案例是前几天同事遇到的一个案例,在存储过程中"删除"了一个临时表,然后重新创建这个临时表时遇到"There is already an object named 'x ...
- Copula函数
Copula函数 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. Copula介绍 Copula函数把边缘分布函数与联合分布函数联系起来,是研究变 ...
- EasyUI之DataGird动态组合列
Dojo.ExtJS.Jquery(EasyUI.jQgrid.ligerui.DWZ).还有asp.net中的服务器控件.当然也少不了HTML 标签之table标签了.其中dojo.ExtJS.Jq ...
- 开发环境---->服务器(数据库迁移Migration)
1.查找服务器环境迁移记录表的最近一次迁移名称 SELECT * FROM __efmigrationshistory; 最后一次:20190225075007_UpdateSocialApplyCo ...
- python从开始到放弃的途中一直很菜的day13
一直很菜的今天又来了两个新的概念,一个是装饰器,一个是推导式,装饰器好比女生出门前需要化妆的准备,推导式也称为生成式.先说装饰器吧,装饰器其实本质也是一个函数,并用@+函数名装饰到其他函数上,当这个其 ...