【Codeforces Round #411 (Div. 1)】Codeforces 804D
来源:互联网 发布:js开发工具中文版 编辑:程序博客网 时间:2024/05/23 14:02
先预处理出每棵树的直径
然后这个题就做完了,复杂度是
单次询问的复杂度可以看成是
#include<cstdio>#include<vector>#include<map>#include<algorithm>using namespace std;const int maxn=200010;int down[maxn],from[maxn],down2[maxn],up[maxn],bel[maxn],dia[maxn],size[maxn],n,m,q,tot;double s[maxn];vector<int> a[maxn],to[maxn];vector<double> sum[maxn];map<pair<int,int>,double> ans; void dfs1(int u,int f){ bel[u]=tot; size[tot]++; for (vector<int>::iterator it=to[u].begin();it!=to[u].end();++it) if ((*it)!=f) { dfs1(*it,u); if (down[*it]+1>=down[u]) { down2[u]=down[u]; down[u]=down[*it]+1; from[u]=*it; } else down2[u]=max(down2[u],down[*it]+1); }}void dfs2(int u,int f){ int x; if (f!=-1) { up[u]=up[f]+1; if (from[f]!=u) up[u]=max(up[u],down[f]+1); else up[u]=max(up[u],down2[f]+1); } x=max(up[u],down[u]); dia[tot]=max(x,dia[tot]); a[tot].push_back(x); for (vector<int>::iterator it=to[u].begin();it!=to[u].end();++it) if ((*it)!=f) dfs2(*it,u);}int main(){ //freopen("in.txt","r",stdin); int u,v,x; double res,d,now; pair<int,int> pr; vector<int>::iterator i2; scanf("%d%d%d",&n,&m,&q); for (int i=1;i<=m;i++) { scanf("%d%d",&u,&v); to[u].push_back(v); to[v].push_back(u); } for (int i=1;i<=n;i++) if (!bel[i]) { tot++; dfs1(i,-1); dfs2(i,-1); sort(a[tot].begin(),a[tot].end()); sum[tot].push_back(now=0); for (vector<int>::iterator it=a[tot].begin();it!=a[tot].end();++it) sum[tot].push_back(now+=(*it)); s[tot]=now; } while (q--) { scanf("%d%d",&u,&v); u=bel[u]; v=bel[v]; if (u==v) { printf("-1\n"); continue; } if (size[u]>size[v]||(size[u]==size[v]&&u>v)) swap(u,v); pr=make_pair(u,v); if (ans.count(pr)) { printf("%.10f\n",ans[pr]); continue; } res=0; d=max(dia[u],dia[v]); for (vector<int>::iterator it=a[u].begin();it!=a[u].end();++it) { x=upper_bound(a[v].begin(),a[v].end(),d-(*it)-1)-a[v].begin(); res+=d*x+((*it)+1.0)*(size[v]-x)+s[v]-sum[v][x]; } res/=(double)size[u]*size[v]; printf("%.10f\n",ans[pr]=res); }}
0 0
- 【Codeforces Round #411 (Div. 1)】Codeforces 804D
- Codeforces Round #202 (Div. 1) D. Turtles
- Codeforces Round #253 (Div. 1)D题
- Codeforces Round #225 (Div. 1) D. Antimatter
- Codeforces Round #276 (Div. 1) D. Kindergarten
- Codeforces Round #286 (Div. 1) C、D
- Codeforces Round #434 Div.1 D graph
- Codeforces Round #411 (Div. 1)(A~D)题解
- [树的直径] Codeforces 804D Round #411 (Div. 1) D. Expected diameter of a tree
- 【Codeforces Round #200 (Div. 1)】Codeforces 343D Water Tree
- 【Codeforces Round #122 (Div. 1)】Codeforces 193D Two Segments
- 【Codeforces Round #196 (Div. 1)】Codeforces 338D GCD Table
- 【Codeforces Round #411 (Div. 1)】Codeforces 804A Find Amir
- Codeforces Round #250 (Div. 2) && (Div. 1) D题
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D
- Codeforces Round #237 (Div. 2) D. Minesweeper 1D
- Codeforces Round #237 (Div. 2) D Minesweeper 1D
- 【 D3.js 入门系列 --- 0 】 简介和安装
- Spring MVC + Spring + Hibernate集成
- 【bzoj3262】陌上花开
- Caffe安装以及GPU服务器设置并行
- JavaScript中canvas绘制太极图案
- 【Codeforces Round #411 (Div. 1)】Codeforces 804D
- 超酷的 Vim 搜索技巧
- Vuforia核心特征视频教程
- 《Effective java 第2版》读书笔记--泛型与枚举
- 抓取某网站数据小结
- 二分贪心练习题U21
- ICP算法理解
- javaScript & jquery完美判断图片是否加载完毕
- poj 2387 Til the Cows Come Home