Codeforces 629 E. Famil Door and Roads
题目链接: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的更多相关文章
- 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
题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ...
- 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 E. Famil Door and Roads 期望
一棵树,n个节点,边长为1,有q个询问,每个询问给出u,v(u != v),问在树上等概率加一条边,如果使得u,v在一个环内,则这种加边方式是合法的,此时的值为环的长度,所有合法的加边方式出现的概率相 ...
- Codeforces 908F New Year and Rainbow Roads
New Year and Rainbow Roads 思路:我们考虑两个绿点之间的红点和蓝点, 首先把这些红点和蓝点接到绿点上面绝对不会超过绿点距离的两倍. 然后我们先把两个绿点连上, 再把绿点经过蓝 ...
- 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 ...
- 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 ...
- 【23.24%】【codeforces 629C】Famil Door and Brackets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 908F - New Year and Rainbow Roads 【脑洞】
题意:在一条数轴上有若干'R','B','G'点,连接两个点的代价是位置差,要求使用最小代价使得除去所有'R'点后剩下的所有点联通,除去所有'B'点后剩下的所有点联通. 还以为会是什么最小生成树,结果 ...
随机推荐
- Codeforce 294A - Shaass and Oskols (模拟)
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to ...
- Step7:SQL Server 多实例下的复制
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 搭建步骤(Procedure) 注意事项(Attention) 二.背景(Contexts) ...
- Django之MVC和MTV
一. MVC MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式: Model(模型)表示应用程序核心(比如数据库记录列表). ...
- MySQL5.7 的新特点
1.安全性 MySQL 5.7 的目标是成为发布以来最安全的 MySQL 服务器,其在 SSL/TLS 和全面安全开发方面有一些重要的改变. mysql.user表结构升级 MySQL5.7用户表my ...
- word2010自定义的多级列表编号变成黑块的解决办法
首先,看图说话 是的,当我保存Word再打开之后,我辛辛苦苦(没错,小白有罪,调来调去真辛苦)搞得多级列表编号变成了黑块,默默无言,只有泪千行,还好有万能的Google. 解决办法: 将光标移到黑块的 ...
- 利用cgi编程实现web版man手册
董老师前几天给我们布置了3道作业,第三道作业是cgi程序设计. 题目: web服务器cgi接口功能的实现 要求: 能调用cgi程序并得到返回结果: cgi程序能接受参数并得到返回结果: 使用两种或以上 ...
- 深入理解 Vue 组件
深入理解 Vue 组件 组件使用中的细节点 使用 is 属性,解决组件使用中的bug问题 <!DOCTYPE html> <html lang="en"> ...
- 自动发现实现url+响应时间监控
url自动发现脚本: [root@jenkins scripts]# cat urlDiscovery.py #!/usr/bin/env python #coding:utf-8 import o ...
- libcurl 设置代理,通过Fiddler可以进行抓包
转载:https://blog.csdn.net/jaryguo/article/details/53021923 转载:https://www.cnblogs.com/miantest/p/7289 ...
- 从输入URL到页面显示发生了什么
阅读目录 1.输入地址 2.浏览器查找域名的 IP 地址 3.浏览器向 web 服务器发送一个 HTTP 请求 4.服务器的永久重定向响应 5.浏览器跟踪重定向地址 6.服务器处理请求 7.服务器返回 ...