题目链接:http://codeforces.com/problemset/problem/629/E


询问这个简单环的期望。考虑将这个环拆成两部分。

令${deep[x]>=deep[y]}$,${size[x]}$表示以$x$为根的子树大小,${sdown[x]}$示以$x$为根的子树的所有节点到$x$的距离和,${sall[x]}$所有点到$x$的距离和。${ne}$表示从${y-->x}$路径上${y}$的儿子。

1.${dis(x,y)}$这是一个环肯定要经过的,算入答案。

2.分情况考虑:

  若是${lca(x,y)!=y}$,会产生${\frac{sdown[x]}{size[x]}+\frac{sdown[y]}{size[y]}}$的贡献。

  若是${lca(x,y)=y}$,会产生${\frac{sdown[x]}{size[x]}+\frac{sdall[y]-sdown[ne]-size[ne]}{n-size[ne]}}$的贡献。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 200010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,T;
llg sall[maxn],sdown[maxn],deep[maxn],f[maxn][],size[maxn];
vector<llg>a[maxn]; inline llg getint()
{
llg w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void dfs1(llg x,llg fa)
{
llg w=a[x].size(),v;
deep[x]=deep[fa]+; f[x][]=fa;
size[x]=;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v==fa) continue;
dfs1(v,x);
size[x]+=size[v];
sdown[x]+=sdown[v]+size[v];
}
} void dfs2(llg x,llg fa)
{
llg w=a[x].size(),v;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v==fa) continue;
sall[v]=sall[x]+n-*size[v];
dfs2(v,x);
}
} void init()
{
llg x,y;
cin>>n>>T;
for (llg i=;i<n;i++)
{
x=getint(),y=getint();
a[x].push_back(y),a[y].push_back(x);
}
deep[]=;
dfs1(,);
sall[]=sdown[];
dfs2(,);
for (llg j=;j<=;j++)
for (llg i=;i<=n;i++)
f[i][j]=f[f[i][j-]][j-];
} llg find(llg x,llg y)
{
for (llg i=;i>=;i--)
if (deep[f[x][i]]>=deep[y])
x=f[x][i];
if (x==y) return x;
for (llg i=;i>=;i--)
if (f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][];
} llg dis(llg x,llg y)
{
return deep[x]+deep[y]-deep[find(x,y)]*;
} llg up(llg x,llg res)
{
for (llg i=;i>=;i--)
if (res>=(<<i))
{
x=f[x][i];
res-=(<<i);
}
return x;
} int main()
{
yyj("tree");
init();
while (T--)
{
llg x,y;
x=getint(),y=getint();
if (deep[x]<deep[y]) swap(x,y);
double ans=dis(x,y)+;
if (find(x,y)!=y)
{
ans+=(double)sdown[x]/(double)size[x]+(double)sdown[y]/(double)size[y];
}
else
{
llg ne=up(x,deep[x]-deep[y]-);
ans+=(double)sdown[x]/(double)size[x]+(double)(sall[y]-sdown[ne]-size[ne])/(double)(n-size[ne]);
}
printf("%.9lf\n",ans);
}
return ;
}

Codeforces 629 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

    题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ...

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

  4. codeforces E. Famil Door and Roads 期望

    一棵树,n个节点,边长为1,有q个询问,每个询问给出u,v(u != v),问在树上等概率加一条边,如果使得u,v在一个环内,则这种加边方式是合法的,此时的值为环的长度,所有合法的加边方式出现的概率相 ...

  5. Codeforces 908F New Year and Rainbow Roads

    New Year and Rainbow Roads 思路:我们考虑两个绿点之间的红点和蓝点, 首先把这些红点和蓝点接到绿点上面绝对不会超过绿点距离的两倍. 然后我们先把两个绿点连上, 再把绿点经过蓝 ...

  6. Codeforces 629 B. Far Relative’s Problem

      B. Far Relative’s Problem   time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces 629 A. Far Relative’s Birthday Cake

      A. Far Relative’s Birthday Cake   time limit per test 1 second memory limit per test 256 megabytes ...

  8. 【23.24%】【codeforces 629C】Famil Door and Brackets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Codeforces 908F - New Year and Rainbow Roads 【脑洞】

    题意:在一条数轴上有若干'R','B','G'点,连接两个点的代价是位置差,要求使用最小代价使得除去所有'R'点后剩下的所有点联通,除去所有'B'点后剩下的所有点联通. 还以为会是什么最小生成树,结果 ...

随机推荐

  1. Python智能检测编码并转码

    #安装包工具 $pip3 install chardet #直接打开文件,中文显示乱码 >>> import chardet >>> f = open('test. ...

  2. Navicat连接MySQL8.0亲测有效

    今天下了个 MySQL8.0,发现Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0的不一样,连接会报错. 试了很多种方法,终于找到一种可以实现的: 更 ...

  3. redis安装--转

    第一部分:安装redis 希望将redis安装到此目录 1 /usr/local/redis 希望将安装包下载到此目录 1 /usr/local/src 那么安装过程指令如下: 1 2 3 4 5 6 ...

  4. javascript 与 PHP 通信加密,使用AES 128 CBC no padding,以及ios,java,c#文章例子

    运行环境 php7.0 不适用于 php7.0以上版本,因为mcrypt_encrypt()函数已删除 为何要采用 no padding 这种形式: AES加密如果原输入数据不够16字节的整数位,就要 ...

  5. Golang实现冒泡排序法

    关于冒泡排序的原理请看本博客这篇文章冒泡排序法原理讲解及PHP代码示例 //代码 package main import ( "fmt" ) func main() { //定义一 ...

  6. Cookie&Session与自定义session

    cookie与session的区别? cookie是保存在浏览器端的键值对 session是保存在服务器端的键值对 session依赖于cookie 摘自: http://bubkoo.com/201 ...

  7. ==和equals的区别。

    1.java中equals和==的区别 值类型是存储在内存中的堆栈(简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中. 2.==操作比较的是两个变量的值是否相等,对于 ...

  8. 零基础Python爬虫实现(百度贴吧)

    提示:本学习来自Ehco前辈的文章, 经过实现得出的笔记. 目标 http://tieba.baidu.com/f?kw=linux&ie=utf-8 网站结构 学习目标 由于是第一个实验性质 ...

  9. 20145208 蔡野 《网络对抗》Exp8 Web基础

    20145208 蔡野 <网络对抗>Exp8 Web基础 本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POS ...

  10. 计算多数的乘积(Python实现)

    1 # -*- coding: utf-8 -*- # sum_of_products.py # @author 0yst3r # @description 两数之积及多数之积 # @created ...