题目链接:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets

    题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...

  5. Codeforces Round #343 (Div. 2)

    居然补完了 组合 A - Far Relative’s Birthday Cake import java.util.*; import java.io.*; public class Main { ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ASP.NET C# 文件下载

    1.文件下载到客户端 //WriteFile实现下载 protected void Download_Click(object sender, EventArgs e) { string fileNa ...

  2. UI2_ScrollView&UIPageControl

    // // ViewController.h // UI2_ScrollView&UIPageControl // // Created by zhangxueming on 15/7/10. ...

  3. StreamWriter和StremReader简单的用法

    string str = "中国";//写入的内容 string path = @"e:\1.txt";//文件路径 StreamWriter sw = new ...

  4. QT 多线程程序设计【转】

    QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...

  5. 纯css3 开关按钮

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. web前端炫酷实用的HTML5应用和jQuery插件

    又开始了新的一天,我们也将继续为大家分享许多优秀的HTML5应用和jQuery插件,作为前端开发者来说,这些资源可以帮助你在项目开发上派上用场.下面一起来看看这些炫酷而实用的HTML5应用和jQuer ...

  7. 数值积分NIntegrate中的具体算法

    数值积分方法很多,Mathematica中至提供了NIntegrate.具体算法可参照官方帮助. http://reference.wolfram.com/language/tutorial/NInt ...

  8. Fedora 19 vim c语言开发环境

    1. Fedora 19 居然没有自带 gcc 和 g++: sudo yum -y install gcc gcc-c++ 2. 安装 vim 和 cvim 插件: sudo yum -y vim ...

  9. spring4.0源码导入

    一个面试,让我知道了自己的不足,一天不进步就是倒退. spring源码导入eclipse 本人的环境 (我导入的是最新的spring 4.0 所以要用jdk1.8) 1 安装git (mac上自带了g ...

  10. CustomMessageBox使用总结

    开发过程中难免要使用到消息框,然而系统提供的MessageBox却难以满足许多需求.一.MessageBox的背景颜色无法更改,这就无法满足需求要求的消息框颜色.二.MessageBox的提示形式过于 ...