LINK:Expected diameter of a tree

1e5 带根号log 竟然能跑过!

容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) 其中\(D1,D2\)分别为两个联通块内的直径.

\(f_x\)表示 从x出发的最长链.

这样容易想到 枚举一个块的点 然后其实要找到 \(C=max(D1,D2)\) 第一个位置满足\(>C-f_x-1\) 然后就能统计答案了.

排序后扫描 复杂度要高 不如排序后二分.

然后加一个记忆化就过了.

原因是 这样其实本质上是一个根号分治.

对于每次询问的两个块\(x,y\)如果有一个块小于\(\sqrt n\) 那么复杂度为\(\sqrt n \cdot logn\)

如果两个块同时大于\(\sqrt n\) 那么显然对于每个大于\(\sqrt n\)的集合都有\(\sqrt n\)次这样的询问.

求和可以得到复杂度还是为\(n\sqrt n \cdot logn\) 至此可以通过此题.

code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<queue>
#include<deque>
#include<stack>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 1000000001
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define get(x) x=read()
#define gt(x) scanf("%d",&x)
#define gi(x) scanf("%lf",&x)
#define put(x) printf("%d\n",x)
#define putl(x) printf("%lld\n",x)
#define rep(p,n,i) for(RE int i=p;i<=n;++i)
#define go(x) for(int i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define fep(n,p,i) for(RE int i=n;i>=p;--i)
#define vep(p,n,i) for(RE int i=p;i<n;++i)
#define pii pair<int,int>
#define mk make_pair
#define RE register
#define P 1000000007ll
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define EPS 1e-4
#define sq sqrt
#define S second
#define F first
using namespace std;
char *fs,*ft,buf[1<<15];
inline char gc()
{
return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
RE int x=0,f=1;RE char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f; }
const int MAXN=100010;
int n,m,Q,cnt,top,mx;
vector<ll>G[MAXN],c[MAXN];
int g[MAXN],f[MAXN],w[MAXN],s[MAXN],id[MAXN],vis[MAXN];
int lin[MAXN],ver[MAXN<<1],nex[MAXN<<1],len;
map<int,ll>H[MAXN];
inline void add(int x,int y)
{
ver[++len]=y;nex[len]=lin[x];lin[x]=len;
ver[++len]=x;nex[len]=lin[y];lin[y]=len;
}
inline void dfs(int x,int fa)
{
vis[x]=cnt;
go(x)if(tn!=fa)
{
dfs(tn,x);
mx=max(mx,f[x]+f[tn]+1);
if(f[tn]+1>f[x])
{
g[x]=f[x];
f[x]=f[tn]+1;
id[x]=tn;
}
else g[x]=max(g[x],f[tn]+1);
}
}
inline void dp(int x,int fa)
{
s[++top]=f[x];
go(x)if(tn!=fa)
{
if(id[x]!=tn)
{
if(f[x]+1>f[tn])
{
g[tn]=f[tn];
f[tn]=f[x]+1;
id[tn]=x;
}
else g[tn]=max(g[tn],f[x]+1);
}
else
{
if(g[x]+1>f[tn])
{
g[tn]=f[tn];
f[tn]=g[x]+1;
id[tn]=x;
}
else g[tn]=max(g[tn],g[x]+1);
}
dp(tn,x);
}
}
inline int find(int x,int y)
{ int l=0,r=G[x].size()-1;
while(l+1<r)
{
int mid=(l+r)>>1;
if(c[x][mid]<=y)l=mid;
else r=mid;
}
if(c[x][r]<=y)return r;
return l;
}
inline ll calc(int x,int y)
{
if(H[x].find(y)!=H[x].end())return H[x][y];
int cc=max(w[x],w[y]);
ll ans=0;
vep(0,G[x].size(),i)
{
if(c[y][0]>cc-c[x][i]-1)
{
ans+=(ll)(1+c[x][i])*G[y].size()+G[y][G[y].size()-1];
continue;
}
if(c[y][c[y].size()-1]<=cc-c[x][i]-1)
{
ans+=(ll)G[y].size()*cc;
continue;
}
int ww=find(y,cc-c[x][i]-1);
ans+=(ll)(ww+1)*cc+G[y][G[y].size()-1]-G[y][ww]+(1+c[x][i])*(G[y].size()-ww-1);
}
H[x][y]=ans;return ans;
}
int main()
{
//freopen("1.in","r",stdin);
get(n);get(m);get(Q);
rep(1,m,i)add(read(),read());
rep(1,n,i)
{
if(vis[i])continue;
mx=top=0;++cnt;dfs(i,0);dp(i,0);
sort(s+1,s+1+top);w[cnt]=mx;
rep(1,top,j)G[cnt].pb(s[j]),c[cnt].pb(s[j]);
rep(1,top-1,j)G[cnt][j]+=G[cnt][j-1];
}
rep(1,Q,i)
{
int get(x),get(y);
if(vis[x]==vis[y]){puts("-1");continue;}
x=vis[x];y=vis[y];
if(G[x].size()>G[y].size())swap(x,y);
if(G[x].size()==G[y].size()&&x>y)swap(x,y);
printf("%.7lf\n",1.0*calc(x,y)/(G[x].size()*G[y].size()));
}
return 0;
}

CF804D Expected diameter of a tree 树的直径 根号分治的更多相关文章

  1. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  2. Codeforces 840D Expected diameter of a tree 分块思想

    Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...

  3. Codeforces 804D Expected diameter of a tree

    D. Expected diameter of a tree time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  4. Codeforces Round #411 (Div. 1) D. Expected diameter of a tree

    题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...

  5. codeforces804D Expected diameter of a tree

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. Codeforces 804D Expected diameter of a tree(树形DP+期望)

    [题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...

  7. CodeForces 805F Expected diameter of a tree 期望

    题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...

  8. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  9. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. 关于ganymed-ssh2版本262和build210的SCPClient类的区别

    ganymed-ssh2是通过java使用ssh连接服务器的工具库,先上两个版本的pom文件配置: <!--ssh连接linux--> <!-- https://mvnreposit ...

  2. 利用docker部署oxidized网络设备备份系统

    随着网络设备的增多,通过人手备份网络设备倍感压力,而且效率低.有编程基础的人可能会通过Python的parimiko 或者netmiko 连接到设备操作 把文件通过ftp 上传到FTP服务器, 在通过 ...

  3. cookie与token

    cookie: 登陆后后端生成一个sessionid放在cookie中返回给客户端,并且服务端一直记录着这个sessionid,客户端以后每次请求都会带上这个sessionid, 服务端通过这个ses ...

  4. requests接口自动化7-Multi/form-data文件上传形式的post请求:files

    Multi/form-data文件上传形式的post请求:用files传参 fiddler里请求响应内容; 代码: import requests from requests_toolbelt imp ...

  5. 关于Haskell计算斐波那契数列的思考

    背景 众所周知,Haskell语言是一门函数式编程语言.函数式编程语言的一大特点就是数值和对象都是不可变的,而这与经常需要对状态目前的值进行修改的动态规划算法似乎有些"格格不入", ...

  6. 用matplotlib画简单折线图示例

    例1 import numpy as np import matplotlib.pyplot as plt from scipy import stats rx1 = np.array([54.52, ...

  7. 使用pycharm、跳板机连接内网服务器

    使用pycharm.跳板机连接内网服务器 接手实验室服务器后,大部分同学在GPU集群上跑程序都是直接在ssh界面上跑,这里想着通过pycharm通过跳板机来连接服务器. 总体就是实验室服务器仅限内网访 ...

  8. Burp Suite Intruder Module - 攻击模块

    参考链接:https://portswigger.net/burp/documentation/desktop/tools/intruder/using 主要思路:在Intruder模块下设定Targ ...

  9. 集训作业 洛谷P3913 车的攻击

    这个题一开始被我想复杂了,但总体差不多. 脑子清醒后我直接看他占领了几条长,几条宽,比如一个长3宽3的地图. 被占领了一条宽,就可以看成一个长3宽2的地图.这个长3宽2的地图就是出去可以被攻击的点剩下 ...

  10. Qt-可视化数据库操作

    1  简介 参考视频:https://www.bilibili.com/video/BV1XW411x7NU?p=89 说明:Qt可使用QSqlTableModel来进行数据库的可视化操作,将mode ...