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)的更多相关文章

  1. 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 ...

  2. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  3. ZOJ 3949 Edge to the Root

    题意: 在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和. 思路: 我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上, ...

  4. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  5. ZOJ 3626 Treasure Hunt I (树形DP,常规)

    题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...

  6. ZOJ 3805 (树形DP)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...

  7. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  8. ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...

  9. 树dp...吧 ZOJ 3949

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...

随机推荐

  1. Java学习点滴——Integer缓存

    前言 一切从下面这段代码开始 public static void test(String[] agrs){ Integer a = 1; Integer b = 2; System.out.prin ...

  2. .net 笔试面试总结(3)

    什么是Sql注入?如何避免Sql注入? 用户根据系统的程序构造非法的参数从而导致程序执行不是程序期望的恶意Sql语句. 使用参数化的Sql就可以避免Sql注入. 数据库三范式是什么? 第一范式:字段不 ...

  3. 003. 什么是 正向代理 & 反向代理

    正向代理: 客户端的代理: 反向代理: 服务端的代理:

  4. 原生 table css实现操作按钮固定右侧及底部滚动 IE不会卡死

    需求的表格比较复杂(各种合并新增删除),elementUi的table组件无法满足需求,故而写了原生table,且与其他用了table组件的表格保持一致. 贴一下简单的代码,只实现操作按钮固定右侧以及 ...

  5. android - TextView单行显示...或者文字左右滚动(走马灯效果)

    条件 TextView单行显示,文字左右滚动(走马灯效果)实现条件: 实现单行设置固定宽度或者设置权重都行 代码 TextView滚动必须写下面几个属性 android:singleLine=&quo ...

  6. Android常用的四种布局(或者说是五种)

    一.FrameLayout(帧布局): 显示特点:所有的子控件默认显示在FrameLayout的左上角,会重叠在一起显示. 常用属性: layout_gravity(设置给子控件,调整控件在容器内的重 ...

  7. 一起学Android之ToggleButton和Switch

    本文以一个简单的小例子,简述在Android开发中ToggleButton(开关按钮)和Switch(开关)的简单使用,仅供学习分享使用. 概述 ToggleButton是一个有两种状态(checke ...

  8. 荣耀5.0以上手机(亲测有效)激活xposed框架的经验

    对于喜欢搞机的朋友而言,大多时候会使用到xposed框架及其种类繁多功能强悍的模块,对于5.0以下的系统版本,只要手机能获得Root权限,安装和激活xposed框架是非常简便的,但随着系统版本的不断迭 ...

  9. Websocket-Sharp获取客户端IP地址和端口号

    //OnOpen事件 protected override void OnOpen() { string IPAddress = base.Sessions.Sessions.First().Cont ...

  10. TypeScript 之类型判断

    在使用 Angular 做项目的时候,对 TypeScript 的类型判断不太熟练,为了方便查找,特意对 TypeScript 的类型判断做了简单梳理.文章只是 TS 官网的内容摘要,没有高深的知识, ...