题意

给出 \(n\) 个点的树,求 \(\sum_{i=1}^n{\sum_{j=i}^n{\lceil \frac{dis(i,j)}{2} \rceil}}\) .

\(n\leq 2 \times 10^5\) .

分析

  • 点分治SBT.考虑更快速的做法。

  • 如果直接统计总的贡献唯一的问题在于奇数路径统计时上取整的问题。

  • 实际答案加上奇数长路径条数就可以解决问题。

  • 一条路径可以写成: \({dis}_u+{dis}_v-2*{dis}_{lca}\), \(2*{dis}_{lca}\) 是偶数,路径长为奇数当且仅当 \({dis}_u\) ,\({dis}_v\) 一奇一偶。

  • 总时间复杂度为 \(O(n)\)。

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=2e5 + 7;
int n,c0,c1,edc;
int dep[N],son[N],head[N];
LL sum[N],ans;
struct edge{
int last,to;
edge(){}edge(int last,int to):last(last),to(to){}
}e[N*2];
void Add(int a,int b){
e[++edc]=edge(head[a],b),head[a]=edc;
e[++edc]=edge(head[b],a),head[b]=edc;
}
void dfs(int u,int fa){
if(dep[u]&1) c1++;
else c0++;
son[u]=1;
go(u)if(v^fa){
dep[v]=dep[u]+1,dfs(v,u);
ans+=(sum[v]+son[v])*son[u]+sum[u]*son[v];
son[u]+=son[v],sum[u]+=sum[v]+son[v];
}
}
int main(){
n=gi();
rep(i,1,n-1) Add(gi(),gi());
dfs(1,0);
printf("%lld\n",(ans+1ll*c0*c1)/2);
return 0;
}

[CF1060E]Sergey and Subway[树dp]的更多相关文章

  1. cf1060E. Sergey and Subway(树形dp)

    题意 题目链接 Sol 很套路的题 直接考虑每个边的贡献,最后再把奇数点的贡献算上 #include<bits/stdc++.h> #define Pair pair<int, in ...

  2. CF1060E Sergey and Subway(点分治)

    给出一颗$N$个节点的树,现在我们**在原图中**每个不直接连边但是中间只间隔一个点的两个点之间连一条边. 比如**在原图中**$u$与$v$连边,$v$与$w$连边,但是$u$与$w$不连边,这时候 ...

  3. CF1060E Sergey and Subway 思维

    分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...

  4. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

  5. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  6. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  7. HDU4276 The Ghost Blows Light SPFA&&树dp

    题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...

  8. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  9. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. 试试SQLServer 2014的内存优化表(转载)

    SQL Server2014存储引擎:行存储引擎,列存储引擎,内存引擎 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据 ...

  2. CPU IO MEM NETWork 监控命令

    性能优化中CPU.内存.磁盘IO.网络性能的依赖(上) 性能优化中CPU.内存.磁盘IO.网络性能的依赖(下)

  3. jbosscache

    JBossCache 讲解说明 是什么? 一个树形结构.支持集群.支持事务的缓存技术. 有什么作用? JBoss Cache是针对Java应用的企业级集群解决方案,其目的是通过缓存需要频繁访问的Jav ...

  4. heckboxlist详细用法、checkboxlist用法、checkboxlist

    heckboxlist详细用法.checkboxlist用法.checkboxlist for (int i = 0; i < CheckBoxList1.Items.Count; i++) { ...

  5. Django之MVC与MTV

    MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写 模型 - 视图 - 控制器是一种通常用于开发用户界面的 ...

  6. load file within a jar

    String examplejsPrefix = "example"; String examplejsSuffix = "js"; String exampl ...

  7. android下载 sdk 的两个代理 ,解决下载sdk慢的问题

    mirrors.opencas.cn mirrors.neusoft.edu.cn   设置教程:http://blog.csdn.net/mociml/article/details/1633125 ...

  8. 2018.09.01 09:08 Genesis

    Nothing to think about, I don't know where to start, the mastery of learning is not an easy task, yo ...

  9. Spring IOC容器创建bean过程浅析

    1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...

  10. IOS和安卓不同浏览器常见bug

    一.IOS自带safari浏览器 1.safari不支持fixed.input输入框 iOS下的 Fixed + Input 调用键盘的时候fixed无效问题 拖动页面时 header 和 foote ...