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集合(1)

    java集合类存放于java.util包里,只能存放对象,存放的是对象的引用,可以是不同类型,不限数量的数据类型. 顶层接口:Iterator(迭代器),Map Iterator:核心方法hasNex ...

  2. 微信H5页面 会被软键盘顶起来

    问题描述:H5页面在微信中打开,input输入框获取焦点时,页面被软键盘顶上去:关闭软键盘时,页面不会自动下来(恢复初始状态) H5页面在微信中初始状态如下图: input输入框获取焦点时,页面被软键 ...

  3. python 练习 后台返回当前时间

    新建一个 current_time.html 文件, !cur_time! 用来替换 <!DOCTYPE html> <html lang="en"> &l ...

  4. SAP MM 采购ERP顾问咨询费限制总金额的框架协议实现方案

    SAP MM 采购ERP顾问咨询费限制总金额的框架协议实现方案 [业务场景] 采购部门与ERP咨询公司签订了一个框架协议,只规定不同级别顾问的人天费用,不限定这些不同级别咨询顾问的具体采购的人天数,但 ...

  5. Django项目结构介绍

    官网下载网址:https://www.djangoproject.com/download/ 安装(安装最新LTS版): pip3 install django==2.0.7 创建一个django项目 ...

  6. TiDB 架构及设计实现

    一. TiDB的核心特性 高度兼容 MySQL 大多数情况下,无需修改代码即可从 MySQL 轻松迁移至 TiDB,分库分表后的 MySQL 集群亦可通过 TiDB 工具进行实时迁移. 水平弹性扩展 ...

  7. Python之爬虫的理解

    #  -*- coding: utf-8 -*-  中文用户一定先用这行来声明编码方式 爬虫: 爬虫是自动访问互联网,并且提取数据的程序  (从网络上获取非结构化的数据,ETL将这些数据转换为结构化数 ...

  8. i春秋misc部分writeup

    i春秋misc部分writeup 一.敲击 方方格格,然后看到下面的格式,猜测出是键盘上的布局,然后看这些字母形成的形状想那些字母,就是flag了 2.滴滴滴 放到ctfcack里解密,发现时栅栏密码 ...

  9. June 28th. 2018, Week 26th. Thursday

    You cannot change the circumstances but you can change yourself. 既然改变不了环境,那就改变自己. From Jim Rohn. Rec ...

  10. ASP.NET基础知识汇总之WebConfig自定义节点详细介绍

    之前介绍过Webconfig的具体知识ASP.NET基础知识汇总之WebConfig各节点介绍.今天准备封装一个ConfigHelper类,涉及到了自定义节点的东东,平时虽然一直用,但也没有系统的总结 ...