长沙注册公司核名网站,wordpress列表显示标签,黄页网站数据来源,我的三次做网站的经历正题
题目链接:https://www.luogu.com.cn/problem/CF235D 题目大意
给出一棵基环树#xff0c;每次随机选择一个点让权值加上这个点的连通块大小然后删掉这个点。
求删光所有点时期望权值。 1≤n≤30001\leq n\leq 30001≤n≤3000 解题思路
先找到环#xff0c;然后考虑暴…正题
题目链接:https://www.luogu.com.cn/problem/CF235D 题目大意
给出一棵基环树每次随机选择一个点让权值加上这个点的连通块大小然后删掉这个点。
求删光所有点时期望权值。
1≤n≤30001\leq n\leq 30001≤n≤3000 解题思路
先找到环然后考虑暴力枚举点对(x,y)(x,y)(x,y)计算贡献即统计在xxx删除时与yyy连通的概率。
如果他们之间的路径没有经过环那么显然这个概率是1∣p∣\frac{1}{|p|}∣p∣1即xxx必须是第一个删除的。
如果他们之间的路径有环那么这样就会产生两条路径概率计算后要容斥减去即产生贡献 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
写个LCALCALCA就好了时间复杂度O(n2logn)O(n^2\log n)O(n2logn) code
#includecstdio
#includecstring
#includealgorithm
using namespace std;
const int N3100,T12;
struct node{int to,next;
}a[N1];
int n,tot,cnt,ls[N],cir[N],root[N],dep[N],f[N][T1];
bool v[N];double ans;
void addl(int x,int y){a[tot].toy;a[tot].nextls[x];ls[x]tot;return;
}
int dfs(int x,int fa){v[x]1;for(int ils[x];i;ia[i].next){int ya[i].to;if(yfa)continue;if(v[y]){root[cnt]x;cir[x]cnt;return y;}int zdfs(y,x);if(z-1)return -1;if(z){root[cnt]x;cir[x]cnt;if(zx)return -1;return z;}}return 0;
}
void Dfs(int x){for(int ils[x];i;ia[i].next){int ya[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 iT;i0;i--)if(dep[f[x][i]]dep[y])xf[x][i];if(xy)return x;for(int iT;i0;i--)if(f[x][i]!f[y][i])xf[x][i],yf[y][i];return f[x][0];
}
int main()
{scanf(%d,n);for(int i1;in;i){int x,y;scanf(%d%d,x,y);x;y;addl(x,y);addl(y,x);}dfs(1,0);for(int i1;icnt;i)dep[root[i]]1,Dfs(root[i]);for(int j1;jT;j)for(int i1;in;i)f[i][j]f[f[i][j-1]][j-1];for(int x1;xn;x)for(int y1;yn;y){if(cir[x]cir[y]){int lcaLCA(x,y);ans1/(1.0*(dep[x]dep[y]-dep[lca]*21));}else{int len3dep[x]dep[y];int len1abs(cir[x]-cir[y]);int len2cnt-len1;len1len3-1;len2len3-1;len3cnt-2;ans1.0/(double)len11.0/(double)len2-1.0/(double)len3;}}printf(%.12lf\n,ans);return 0;
}