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 ...
随机推荐
- Java之循环语句练习1
最近在猛复习Java,猛刷题目ing.这个做题目的过程其实也就像搬砖一样,一点一点把最基础的巩固好,一块一块.整整齐齐地砌才能砌好一面墙.好了,不说了,我要去搬砖了. 其实不瞒你们说,我是比较喜欢数学 ...
- Android平台下OpenGL初步
Android OpenGL ES 开发教程 从入门到精通 http://blog.csdn.net/zhoudailiang/article/details/50176143 http://blog ...
- apache Internal Server Error 的几个问题
Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...
- 什么叫wipe,安卓用户如何去wipe?
一.wipe是什么意思 wipe从英文单词的字面意思来理解就是:揩,擦;揩干,擦净的意思,从刷机爱好者的专业角度来理解可以认为是一种对手机数据擦除的操作.关于wipe是什么意思比较专业的解答为:wip ...
- Linux中RM快速删除大量文件/文件夹方法
昨天遇到一个问题,在Linux中有一个文件夹里面含有大量的Cache文件(夹),数量级可能在百万级别,使用rm -rf ./* 删除时间慢到不可接受.Google了一下,查到了一种方法,试用了下确实比 ...
- Dicom格式文件解析器
转自:http://www.cnblogs.com/assassinx/archive/2013/01/09/dicomViewer.html Dicom全称是医学数字图像与通讯,这里讲的暂不涉及通讯 ...
- 20140912-.NET平台技术思维导图
前段时间在网上看到的一张图,忘记出处了.
- 通过Driver获取数据库连接
先看一下文件,在当前包下有一个properties配置文件,在根目录下有一个lib文件夹,里面放的是mySql的驱动jar包 Driver :是一个接口,数据库厂商必须提供实现的接口,能从其中获取数据 ...
- 更新win7资源管理器
更新exeplorer.exe: 1.方法1: void RefreshExplorer() { ]; SHELLEXECUTEINFOA shellExeInfo={}; shellExeInfo. ...
- MySql安装时在Start Service处失败
安装MySql时在最后Start Service时失败: 首先先卸载掉MySql. 查看MySql服务有没有启动,若启动,则先停止MySql服务. 打开注册表,到HKEY_LOCAL_MACHIN ...