Codeforces Round #343 (Div. 2) E. Famil Door and Roads
题目链接:
http://www.codeforces.com/contest/629/problem/E
题解:
树形dp。
siz[x]为x这颗子树的节点个数(包括x自己)
dep[x]表示x这个节点的深度,从1开始(其实从什么开始都可以,我们这里用到的只是相对距离)
对于查询u,v,总共有三种情况:
1、u为公共祖先
设x为(u,v)链上u的儿子,则我们知道新边只能从非x子树的点(n-siz[x]连到以v为根的子树上的点(siz[v])
则新边的总条数为(n-siz[x])*siz[v]
现在用树形dp(跑两趟,树形dp的常见用法)可以求出u到(n-siz[x])这些点的距离的和(sd1),以及v到siz[v]这些点的距离的和(sd2)
且(u,v)这条链的长度为Len=dep[u]+dep[v]-2*dep[Lca(u,v)];
组合数学一下,那么答案就是:ans=(sd1*siz[v]+sd2*(n-siz[x])+(n-siz[x])*siz[v]+Len*(n-siz[x])*siz[v])/((n-siz[x])*siz[v])
2、v为公共祖先
同上
3、u,v都不是公共祖先
比上面的更一般化了,把对象(n-siz[x])变成siz[v]就可以了:
ans=(sd1*siz[v]+sd2*siz[u]+siz[u]*siz[v]+Len*siz[u]*siz[v])/(siz[u]*siz[v])
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std; typedef long long LL;
const int maxn = +;
const int maxm = ; int n, m; vector<int> G[maxn]; int siz[maxn], dep[maxn],lca[maxn][maxm];
LL sdown[maxn],sall[maxn];
void dfs(int u,int fa,int d) {
dep[u] = d, siz[u] = , sdown[u] = ;
lca[u][] = fa;
for (int i = ; i < maxm; i++) {
int f = lca[u][i - ];
lca[u][i] = lca[f][i - ];
}
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u, d + );
siz[u] += siz[v];
sdown[u] += siz[v] + sdown[v];
}
}
void dfs2(int u, int fa) {
if (fa == ) sall[u] = sdown[u];
else sall[u] = sall[fa] + n - * siz[u];
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs2(v, u);
}
} inline void up(int &u,int d){
for (int i = maxm - ; i >= ; i--) {
if (dep[lca[u][i]] >= d) u = lca[u][i];
}
} int Lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
up(u, dep[v]);
if (u == v) return u;
for (int i = maxm - ; i >= ; i--) {
if (lca[u][i] != lca[v][i]) {
u = lca[u][i];
v = lca[v][i];
}
}
return lca[u][];
} void query() {
int u, v;
scanf("%d%d", &u, &v);
int anc = Lca(u, v);
//(u,v)链上的边的贡献+新增的边的贡献
double ans = dep[v] + dep[u] - * dep[anc] + ;
if (anc == v) swap(u, v);
if (anc == u) {
//x为(u,v)链上u的儿子
int x = v; up(x, dep[u] + );
//除x所在子树的点外到所有点的距离的和
LL tmp = sall[u] - sdown[x] - siz[x];
//ans+=(tmp*siz[v]+sdown[v]*(n-siz[x]))/((n-siz[x])*siz[v])
ans += 1.0*tmp / (n - siz[x]) + 1.0*sdown[v] / siz[v];
}
else {
//ans+=(sdown[v]*siz[u]+sdown[u]*siz[v])/(siz[v]*siz[u])
ans += 1.0*sdown[v] / siz[v] + 1.0*sdown[u] / siz[u];
}
printf("%.8lf\n", ans);
} void init() {
for (int i = ; i <= n; i++) G[i].clear();
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
init();
for (int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, , );
dfs2(, );
while (m--) query();
}
return ;
}
Codeforces Round #343 (Div. 2) E. Famil Door and Roads的更多相关文章
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp
E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)
Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp
C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Fami ...
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets
题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...
- Codeforces Round #343 (Div. 2)
居然补完了 组合 A - Far Relative’s Birthday Cake import java.util.*; import java.io.*; public class Main { ...
- Codeforces Round #343 (Div. 2) B. Far Relative’s Problem 暴力
B. Far Relative's Problem 题目连接: http://www.codeforces.com/contest/629/problem/B Description Famil Do ...
- Codeforces Round #343 (Div. 2) B
B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake 水题
A. Far Relative's Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/A Description Do ...
- Codeforces Round #343 (Div. 2)-629A. Far Relative’s Birthday Cake 629B. Far Relative’s Problem
A. Far Relative's Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...
随机推荐
- SQL Server 日志清除
在SqlServer中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式. *[DataBaseName]要压缩日志的数据库名称. 设置数据库模式为简单模式 ALTER DATABASE ...
- VHDL MOD和REM(转)
mod(取模)and rem(取余) VHDL has mod and rem. They return the same value if both arguments are positive. ...
- WCF学习笔记(1)——Hello WCF
1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...
- SDWebImage缓存清理
//计算缓存大小 [SDImageCache sharedImageCache] getSize] //清理缓存 SDImageCache *sd = [[SDImageCache alloc]ini ...
- tomcat的OutOfMemoryError(PermGen space)解决方法
修改TOMCAT_HOME/bin/catalina.bat,在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: set J ...
- Codevs 1669 运输装备
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 德国放松对英国的进攻后,把矛头指向了东北—— ...
- java.util.Vector
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, C ...
- 修改eclipse中tomcat的发布路径
当我们在eclipse部署好tomcat的时候,默认这个项目是部署在eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\t ...
- VHDL基本常识
std_logic_vector和integer需要通过signed或unsigned进行间接转换(强制转换) a_std <= std_logic_vector(to_unsigned(a_i ...
- 实现textarea自适应的方法
1.用div来模拟实现textarea自适应 <!doctype html> <html lang="en"> <head> <meta ...