235D Graph Game
题目大意
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的更多相关文章
- Codeforce 水题报告
最近做了好多CF的题的说,很多cf的题都很有启发性觉得很有必要总结一下,再加上上次写题解因为太简单被老师骂了,所以这次决定总结一下,也发表一下停课一星期的感想= = Codeforces 261E M ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [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), ...
- [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), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
随机推荐
- 计算机信息类ComputerInfo
using System; using System.Management; using System.Net; using System.Net.Sockets; using System.Text ...
- Instruments检测解决内存泄露以及进行性能测试
1.启动Xcode自带的Instruments.这里有两种方法启动. 方法一: 方法二: 2.选择Leaks选项.(该选项用来进行内存泄漏检测) 说明: Leaks:找到引发内存泄漏的起点. Time ...
- sqlalchemy在pythonweb中开发的使用(基于tornado的基础上)
一.关于SQLAlchemy的安装pip install SQLAlchemy安装如果上面的方式安装不成功的情况可以使用下面的方法 百度下载window或者linux下面对应的sqlalchemy的版 ...
- git超速掌握之一(基本使用)
前言: 无论你是运维.开发还是IT爱好者,都会听说github了吧?动不动哪位大神就说在github上有什么什么项目,我的github地址是xxxxx,甚至有自己个github在找新工作时都能给自己加 ...
- 机器人操作系统(ROS)教程22:ROS的3D可视化工具—rviz
rviz是ROS中的一个3D可视化工具,有了它就可以把你用代码建的机器人模型转化为可视的3D模型. 首先需要安装: rosdep install rviz 然后编译rviz: rosmake rviz ...
- Sql server 2008 R2 正在关闭[0x80041033]
1. 事件起因, 昨天还访问的好好的, 然后系统一更新, 今天访问的时候, 就报什么 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是 ...
- 七 Kafka Streams VS Consumer API
1 kafka Streams: 概念: 处理和分析储存在Kafka中的数据,并把处理结果写回Kafka或发送到外部系统的最终输出点,它建立在一些很重要的概念上,比如事件时间和消息时间的准确区分, ...
- java成神之——properties,lambda表达式,序列化
Properties 加载defaults.properties文件 写Properties到xml文件 读Properties从xml文件 Lambda表达式 自定义 内置 sort方法中使用Lam ...
- Python 列表学习笔录
列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类 ...
- table td 实现细边框的样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org ...