http://codeforces.com/contest/839/problem/C

【AC】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath> using namespace std;
const int maxn=2e5+;
struct edge
{
int to;
int nxt;
}e[maxn];
int head[maxn];
int tot;
int dep[maxn];
double ans;
int n;
void init()
{
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} void dfs(int u,int pa,double p)
{
// cout<<u<<" "<<p<<" "<<dep[u]<<endl;
int cnt=;
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(v==pa) continue;
cnt++;
}
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(v==pa) continue;
dep[v]=dep[u]+;
dfs(v,u,p/(double)cnt);
}
if(cnt==)
{
// cout<<"叶节点"<<u<<endl;
ans+=(double)dep[u]*p;
} }
int main()
{
while(~scanf("%d",&n))
{
init();
memset(dep,,sizeof(dep));
int u,v;
for(int i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
ans=0.0;
dep[]=;
dfs(,,);
printf("%.15f\n",ans);
}
return ;
}

【注意】

不能先把所有的分枝数乘起来得到cnt,最后到叶节点再计算概率1/cnt,这样cnt会爆的,一开始就是因为这个WA了

【dfs】codeforces Journey的更多相关文章

  1. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  2. 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph

    题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...

  3. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

  4. 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

    暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...

  5. 【DFS】Codeforces Round #398 (Div. 2) C. Garland

    设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...

  6. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  7. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  8. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  9. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. 动手实现 React-redux(二):结合 context 和 store

    既然要把 store 和 context 结合起来,我们就先构建 store.在 src/index.js 加入之前创建的 createStore 函数,并且构建一个 themeReducer 来生成 ...

  2. hash 【模板】

    hash 功能: hash一般用于快速判断两个或多个字符串是否匹配. 实现 :    想一想,如果比较两个数子的话是很方便的很快,那么我们把整个字符串看成一个大数.  它是base进制的len位数.但 ...

  3. 取URL中各个参数的值

    取参数值的方法有很多,个人记录一个方便好用的 //查询参数(参数名)function GetQueryString(name) {    var reg = new RegExp("(^|& ...

  4. [转]合理使用ArrayMap代替HashMap

    合理使用ArrayMap代替HashMap 2016年07月08日 15:34:44 阅读数:5938 转载请标注: 披萨大叔的博客 http://blog.csdn.net/qq_27258799/ ...

  5. CF782B The Meeting Place Cannot Be Changed

    题意: The main road in Bytecity is a straight line from south to north. Conveniently, there are coordi ...

  6. [实用技巧] Mac下面如何通过终端快速打开当前文件夹

    Mac mac里面其实很简单,直接输入 open .,注意是open + 英文句点. Windows windows里面是start .,注意是start  + 英文句点.

  7. 【HEVC帧间预测论文】P1.5 Fast Coding Unit Size Selection for HEVC based on Bayesian Decision Rule

    Fast Coding Unit Size Selection for HEVC based on Bayesian Decision Rule <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  8. 初用emmet

    下载emmet的pspad插件emmet.js.复制到pspad目录下的 script\JScript 文件夹. 输入 ul#nav>li.item$*4>{Item $} 但是没反应. ...

  9. ios之sqllite3简单使用

    SQLite3是嵌入在iOS中的关系型数据库,对于存储大规模的数据很有效.SQLite3使得不必将每个对象都加到内存中. 基本操作: (1)打开或者创建数据库 sqlite3 *database; i ...

  10. Django生成二维码

    1. 安装 pip install qrcode 安装Image包 pip install Image 1.1 在代码中使用 import qrcode img = qrcode.make('输入一个 ...