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 ...
随机推荐
- CentOS系统下安装以及卸载mysql
CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 [root@localhost `]$ wget htt ...
- opensuse pptp拨号设置注意事项
防火墙一定要关闭 路由器要映射本地1723和1701端口 tcp协议(个别路由器和网络环境,可以先不设置) pptp配置注意协议的选择:
- 【原创】如何找到Oracle中哪条记录被锁
通常有这种情况,某个表或者准确的说是表的某条记录被锁(TX锁),在业务层面排查之余,一般都会想知道是哪条记录被锁,每次被锁的是否是同一条记录?还是每次都不同?通过记录可以找到这条记录可以在哪个模块.哪 ...
- WCF之绑定
NameSpace+Name作为服务元数据的唯一标示.BindingElement描述Binding的特征. 绑定表示通信信道的配置,定义C/S间的协议. 分为:传输信道(TCP,HTTP…),消息编 ...
- (转)mongoDB 禁用大内存页面 transparent_hugepage=never
最近在学mongoDB,安装倒没什么困难,有yum仓库.不过接入ctl后的一条warning倒挺让人烦心的. 1 2 2015-03-22T09:27:00.222+0800 I CONTROL [ ...
- sicily 1022. Train Problem
本题主要是出栈入栈顺序的问题 Home Problems My Status Standing <-> 1022. Train Problem Total: 2 ...
- MyBatis用嵌套ResultMap实现一对多映射
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3959451.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- adb连接不上手机
昨天电脑重装了系统,今天打开之前的eclips工作环境,点击run as -> android application,一直报各种诡异的错误,总之就是连接不上手机. 其中包括 Adb conne ...
- (转)HTML5游戏如何挣钱?2条经验让你每款赚3万刀
原文作者:Alexander Krug,是世界上最大的HTML5游戏平台的运营商SOFTGAMES的CEO. 现今苹果App Store当中的应用数量可以以海量来形容,最新发布的应用对排行榜的冲击力也 ...
- CSS3制作立体导航
<ul class="nav"> <li><a href="">首页</a></li> <li ...