日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

CF235D-Graph Game【LCA,数学期望】

發(fā)布時(shí)間:2023/12/3 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF235D-Graph Game【LCA,数学期望】 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

正題

題目鏈接:https://www.luogu.com.cn/problem/CF235D


題目大意

給出一棵基環(huán)樹,每次隨機(jī)選擇一個(gè)點(diǎn)讓權(quán)值加上這個(gè)點(diǎn)的連通塊大小然后刪掉這個(gè)點(diǎn)。

求刪光所有點(diǎn)時(shí)期望權(quán)值。

1≤n≤30001\leq n\leq 30001n3000


解題思路

先找到環(huán),然后考慮暴力枚舉點(diǎn)對(duì)(x,y)(x,y)(x,y)計(jì)算貢獻(xiàn),即統(tǒng)計(jì)在xxx刪除時(shí)與yyy連通的概率。

如果他們之間的路徑?jīng)]有經(jīng)過(guò)環(huán),那么顯然這個(gè)概率是1∣p∣\frac{1}{|p|}p1?xxx必須是第一個(gè)刪除的。

如果他們之間的路徑有環(huán),那么這樣就會(huì)產(chǎn)生兩條路徑,概率計(jì)算后要容斥減去即產(chǎn)生貢獻(xiàn)
1∣p1∣+1∣p2∣?1∣p1∪p2∣\frac{1}{|p_1|}+\frac{1}{|p_2|}-\frac{1}{|p_1\cup p_2|}p1?1?+p2?1??p1?p2?1?

寫個(gè)LCALCALCA就好了,時(shí)間復(fù)雜度O(n2log?n)O(n^2\log n)O(n2logn)


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\n",ans);return 0; }

總結(jié)

以上是生活随笔為你收集整理的CF235D-Graph Game【LCA,数学期望】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。