传送门

题目大意

https://www.luogu.org/problemnew/show/CF235D

分析

我们先考虑它是树的情况

我们设$event(x,y)$表示删除点x是y与x联通这件事对答案贡献的期望

我们设x到y这一条链的长度为$len$,$x$和$y$所属联通块的大小为$n$

则我们可以猜测$event$的值为$\frac{1}{len}$

我们可以用数学归纳法证明

我们知道直接选到$x$的概率为$\frac{1}{n}$

先选到其它点再通过若干步选到x的概率为$\frac{n-len}{n} * \frac{1}{len}$

由此得证

于是我们在考虑它是基环树的情况

我们不难发现对于不经过环的路径没有影响

而其它路径我们把它不经过环的那些距离设为$x$,经过环的两条路分别为$y$和$z$

$event(x,y)$发生的概率实际上就是这两条路中$x$是任意一条路上第一个被删除的结点的概率

我们再容斥一下就可以得到$event(x,y) = \frac{1}{x+y} + \frac{1}{x+z} - \frac{1}{x+y+z}$

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int n,m,dep[],id[],cnt,sum,acc[],lca[][],f[];
int dfn[],low[],ist[],belong[],tot[],maxid;
double Ans;
stack<int>a;
vector<int>v[];
inline void tarjan(int x,int fa){
dfn[x]=low[x]=++cnt;
a.push(x);
ist[x]=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa){
if(!dfn[v[x][i]]){
tarjan(v[x][i],x);
low[x]=min(low[x],low[v[x][i]]);
}else if(ist[v[x][i]]){
low[x]=min(low[x],dfn[v[x][i]]);
}
}
if(low[x]==dfn[x]){
sum++;
while(){
int u=a.top();
a.pop();
ist[u]=;
belong[u]=sum;
tot[sum]++;
if(u==x)break;
}
}
}
inline int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
inline void work(int x,int ac,int fa){
acc[x]=ac;
f[x]=x;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&tot[belong[v[x][i]]]==){
dep[v[x][i]]=dep[x]+;
work(v[x][i],ac,x);
if(sf(v[x][i])!=sf(x))
f[sf(v[x][i])]=sf(x);
}
for(int i=;i<=n;i++)
if(acc[i]==ac)lca[x][i]=lca[i][x]=sf(i);
}
inline void dfs(int x,int fa){
id[x]=id[fa]+,dep[x]=,acc[x]=x;
work(x,x,x);
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&!id[v[x][i]]){
if(tot[belong[v[x][i]]]>)dfs(v[x][i],x);
}
}
int main(){
int i,j,k,x,y;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
x++,y++;
v[x].push_back(y);
v[y].push_back(x);
}
for(i=;i<=n;i++)
if(!dfn[i])tarjan(i,);
for(i=;i<=n;i++)
if(tot[belong[i]]>){
id[i]=;
maxid=tot[belong[i]];
dfs(i,);
break;
}
for(i=;i<=n;i++)
for(j=;j<=n;j++){
if(acc[i]==acc[j])Ans+=1.0/(dep[i]+dep[j]-*dep[lca[i][j]]+);
else {
int x=dep[i]+dep[j],y=abs(id[acc[i]]-id[acc[j]])-,z=maxid-y-;
Ans+=1.0/(x+y)+1.0/(x+z)-1.0/(x+y+z);
}
}
printf("%0.7lf\n",Ans);
return ;
}

235D Graph Game的更多相关文章

  1. Codeforce 水题报告

    最近做了好多CF的题的说,很多cf的题都很有启发性觉得很有必要总结一下,再加上上次写题解因为太简单被老师骂了,所以这次决定总结一下,也发表一下停课一星期的感想= = Codeforces 261E M ...

  2. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  3. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  4. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  5. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  7. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  8. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  9. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

随机推荐

  1. 利用Github免费搭建个人主页(个人博客)

    之前闲着, 利用Github搭了个免费的个人主页. 涉及: Github注册 Github搭建博客 域名选购 绑定域名 更多 一  Github注册 在地址栏输入地址:http://github.co ...

  2. 系列文章--SharePoint 2013 Designer 入门教程

    SharePoint的使用中,SharePoint Designer是非常重要的工具,我们可以通过Designer设计页面.母版页,维护.管理站点,也可以定制列表表单.数据视图,设计工作流等等.下面总 ...

  3. python 函数 hex()

    hex(x)作用:hex() 函数用于将10进制整数转换成16进制整数. x-10进制整数,返回16进制整数 实例: >>>hex(255) '0xff' >>> ...

  4. 第13篇 PSR-2代码样式

    这个规范原文如下: 1. Overview Code MUST follow a "coding style guide" PSR [PSR-1]. Code MUST use 4 ...

  5. 小程序切换账户拉取仓库文件的appid提示

    小程序切换账户拉取仓库文件,拉取后appid会提示项目不是当前appid的项目,因为切换了账户,而每个小程序账户只有一个appid,所以会冲突 去project.config.json里吧appid改 ...

  6. MVC匿名类传值学习

    刚接触MVC+EF框架不久,但一直很困惑的就是控制器能否及如何向视图传递匿名类数据.宝宝表示很讨厌去新建实体类啦,查询稍有不同就去建一个实体类不是很麻烦吗,故趁阳光正好,周末睡到自然醒后起来尝试了之前 ...

  7. selenium定位方式源码的存放位置

    find_element方法源码存在位置 by定位方法

  8. PL/SQL 训练04--事务

    --pl/sql通过SQL和ORACLE数据库紧密的整合在一起--在pl/sql中可以执行任何操作语句(DML语句),包括INSERT,UPDATE,DELETE,MERGE,也包括查询语句--可否执 ...

  9. Window中的内存地址(小知识)

    现在的编辑器大部分工作都是内存管理托管型,所以很少直接对Window的内存地址直接管理了. Window中的内存地址主要是以16进制数字体现的,当操作系统为32位时,那么每个内存地址为2的32次方,也 ...

  10. swift(Object Storage对象存储服务)(单节点)

    # 在部署对象存储服务(swift)之前,你的环境必须包含身份验证服务(keystone); # keystone需要MySQL数据库,Rabbitmq服务,Memcached服务; # 内存:4G ...