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设计模式之访问者模式

    博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...

  2. 【译】.NET Core 3.0 中的新变化

    .NET Core 3.0 是 .NET Core 平台的下一主要版本.本文回顾了 .Net Core 发展历史,并展示了它是如何从基本支持 Web 和数据工作负载的版本 1,发展成为能够运行 Web ...

  3. nginx 启动错误

    场景 在Windows下 启动nginx报错: nginx: [error] ReadFile() : Incorrect function) 解决 因为 nginx.conf 中存在 /* 被认为是 ...

  4. 禁用事件event默认行为

    在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页. js中return false作用一般是 ...

  5. Snapde电子表格支持的文件格式

    Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件:它运行的速度非常快,反应非常灵敏.那么它支持哪些文件格式呢? 1.CSV文件格式,是一种以逗号分隔列.以回车分隔行的文本文 ...

  6. (四) Keras Dropout和正则化的使用

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 使用drop ...

  7. 章节十、6-CSS---用CSS 定位子节点

    以该网址为例(https://learn.letskodeit.com/p/practice) 一.通过子节点定位元素 1.例如我们需要定位这个table表格 2.当我们通过table标签直接定位时, ...

  8. Django的URL调度

    1.URLconf (URL configuration):(Django版本1.11.20,其它版本可能各有差异.) 在Django中Python后端与前端URL进行交互,是通过一个名为urlcon ...

  9. Jenkins 使用Tfs 插件出现 MappingConflictException 错误问题解决

    FATAL: com.microsoft.tfs.core.clients.versioncontrol.exceptions.MappingConflictException: The path C ...

  10. Docker容器镜像删除

    好吧,本来认为删除镜像是一件很容易的事情,但刚开始上手,还是有点百思不得其解.删着删着,发现果然很容易.分享下本人的心得: 分两种情况:那么要删除镜像,首先得删除容器,删除容器时,确保容器已停止运行: ...