Journey
来源:互联网 发布:淘宝企业店铺过户 编辑:程序博客网 时间:2024/05/22 08:10
http://codeforces.com/contest/839/problem/C
题目大意:
无向图 n个点 n-1条边 类似与树结构 从1开始到各叶子节点的期望长度 从一个点到其相邻节点的概率相同
分析:
dfs求 到达叶子节点用路长乘以概率就行
AC代码:
#include <bits/stdc++.h> #define gcd(a,b) __gcd(a,b) #define mset(a,x) memset(a,x,sizeof(a)) #define FIN freopen("jogging.in","r",stdin) #define FOUT freopen("jogging.out","w",stdout) const int INF=0x3f3f3f3f; const double PI=acos(-1.0); const int MAX=1e5+10; typedef long long LL; const LL mod=1e9+7; using namespace std; int cnt;int vis[100005]; int head[100005];int inedge[100005];double ans;void init(){ ans=cnt=0;mset(head,-1);mset(inedge,0);mset(vis,0);}struct edge{int next;int to;}edge[200005];void add(int u,int v){edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;}void dfs(int u,int countx,double p){int sum=0;for (int i=head[u];i!=-1;i=edge[i].next)if (!vis[edge[i].to]) sum++;if (!sum){ans+=countx*p;return;}for (int i=head[u];i!=-1;i=edge[i].next){if (!vis[edge[i].to]){vis[edge[i].to]=1;dfs(edge[i].to,countx+1,p/sum);}}}int main(){int n;while (scanf ("%d",&n)!=EOF){init();int x,y;for (int i=1;i<n;i++){scanf ("%d%d",&x,&y);add(x,y);add(y,x);inedge[y]=1;}vis[1]=1;dfs(1,0,1);printf ("%.15lf\n",ans);}return 0; }
阅读全文
0 0
- Journey
- journey beginning
- Tomorrow journey
- Journey Start
- Journey(CDOJ1655)
- UESTC Journey
- Journey Project
- CF374 Journey
- C. Journey
- [CF721C]Journey
- Codeforces839C Journey
- C. Journey
- Happiness Is A Journey
- A new journey begin
- POJ_2488_A Knight's Journey
- Life's Journey
- POJ 1935 Journey
- poj 1935 Journey
- 快速排序&冒泡排序
- Vue实战--v-bind动态绑定Class和style
- 视频录制不清楚、模糊解决办法
- 用RestTemplate调用百度文字识别接口
- 关于队列的操作理解
- Journey
- CentOS 7
- 右键新建excel文档无法打开,提示“文件格式或扩展名无效”
- 《java基础入门》课后习题答案_传智播客版
- 查找tomcat程序中某个进程占用CPU高的方法
- maven学习笔记1--简介
- 2017.11.29 python学习笔记
- 如何让idea文件夹目录结构清晰一些
- centos7 关闭防火墙