zoukankan      html  css  js  c++  java
  • CF235D-Graph Game【LCA,数学期望】

    正题

    题目链接:https://www.luogu.com.cn/problem/CF235D


    题目大意

    给出一棵基环树,每次随机选择一个点让权值加上这个点的连通块大小然后删掉这个点。

    求删光所有点时期望权值。

    (1leq nleq 3000)


    解题思路

    先找到环,然后考虑暴力枚举点对((x,y))计算贡献,即统计在(x)删除时与(y)连通的概率。

    如果他们之间的路径没有经过环,那么显然这个概率是(frac{1}{|p|})(x)必须是第一个删除的。

    如果他们之间的路径有环,那么这样就会产生两条路径,概率计算后要容斥减去即产生贡献

    [frac{1}{|p_1|}+frac{1}{|p_2|}-frac{1}{|p_1cup p_2|} ]

    写个(LCA)就好了,时间复杂度(O(n^2log n))


    code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=3100,T=12;
    struct node{
    	int to,next;
    }a[N<<1];
    int n,tot,cnt,ls[N],cir[N],root[N],dep[N],f[N][T+1];
    bool v[N];double ans;
    void addl(int x,int y){
    	a[++tot].to=y;
    	a[tot].next=ls[x];
    	ls[x]=tot;return;
    }
    int dfs(int x,int fa){
    	v[x]=1;
    	for(int i=ls[x];i;i=a[i].next){
    		int y=a[i].to;
    		if(y==fa)continue;
    		if(v[y]){
    			root[++cnt]=x;cir[x]=cnt;
    			return y;
    		}
    		int z=dfs(y,x);
    		if(z==-1)return -1;
    		if(z){
    			root[++cnt]=x;cir[x]=cnt;
    			if(z==x)return -1;
    			return z;
    		}
    	}
    	return 0;
    }
    void Dfs(int x){
    	for(int i=ls[x];i;i=a[i].next){
    		int y=a[i].to;
    		if(cir[y])continue;
    		cir[y]=cir[x];
    		dep[y]=dep[x]+1;
    		f[y][0]=x;Dfs(y);
    	}
    	return;
    }
    int LCA(int x,int y){
    	if(dep[y]>dep[x])swap(x,y);
    	for(int i=T;i>=0;i--)
    		if(dep[f[x][i]]>=dep[y])x=f[x][i];
    	if(x==y)return x;
    	for(int i=T;i>=0;i--)
    		if(f[x][i]!=f[y][i])
    			x=f[x][i],y=f[y][i];
    	return f[x][0];
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++){
    		int x,y;
    		scanf("%d%d",&x,&y);x++;y++;
    		addl(x,y);addl(y,x);
    	}
    	dfs(1,0);
    	for(int i=1;i<=cnt;i++)
    		dep[root[i]]=1,Dfs(root[i]);
    	for(int j=1;j<=T;j++)
    		for(int i=1;i<=n;i++)
    			f[i][j]=f[f[i][j-1]][j-1];
    	for(int x=1;x<=n;x++)
    		for(int y=1;y<=n;y++){
    			if(cir[x]==cir[y]){
    				int lca=LCA(x,y);
    				ans+=1/(1.0*(dep[x]+dep[y]-dep[lca]*2+1));
    			}
    			else{
    				int len3=dep[x]+dep[y];
    				int len1=abs(cir[x]-cir[y]);
    				int len2=cnt-len1;
    				len1+=len3-1;len2+=len3-1;len3+=cnt-2;
    				ans+=1.0/(double)len1+1.0/(double)len2-1.0/(double)len3;
    			}
    		}
    	printf("%.12lf
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    css滚动条设置
    动态背景插件three.min.ts
    富文本编辑器ckeditor使用(angular中)
    angular接口传参
    angular组件图标无法显示的问题
    angular项目搭建
    使用Flume
    centos7 安装Flume
    centos7 安装kubernetes
    Nginx的rewrite对地址进行重写
  • 原文地址:https://www.cnblogs.com/QuantAsk/p/15169683.html
Copyright © 2011-2022 走看看