【dfs】codeforces Journey
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的更多相关文章
- 【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...
- 【贪心】【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< ...
- 【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 ...
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- 【题解】Codeforces 961G Partitions
[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))
度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- sql server 2012 从删库到跑路
问题: 向sql server 2012单个数据库中导入1500万+条数据的时候,报错: 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 O ...
- 5款好用的mysql客户端
1. EMS SQL Manager for MySQL 是一款高性能MySQL数据库服务器系统的管理和开发工具.它支持从MySQL 3.23到6.0的任一版本,并支持最新版本的MySQL的特点,包括 ...
- Openrisc的or1200
1 什么是OpenRISC OpenRISC 是硬件开源社区opencores开发的RISC指令集处理器架构,包括32 bits 的Openrisc1000 和64 bitsOpenrisc 2000 ...
- sqlserver2012 offset
/* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public Lic ...
- COGS 942. [東方S3] 比那名居天子
Problem 1 比那名居天子(tenshi.cpp/c/pas) 题目描述 在幻想乡,比那名居天子是管理着『要石』的天人.『要石』是能够引发和镇压地震的存在,当然也可以用来改变地形.因为在幻想乡引 ...
- dropuser - 删除一个 PostgreSQL 用户帐户
SYNOPSIS dropuser [ option...] [ username] DESCRIPTION 描述 dropuser 删除一个现有 PostgreSQL 用户 和 该用户所有的数据库. ...
- c++如何使用全局变量
在xxxx.h文件中使用extern声明变量: extern int i; 在xxxx.cpp文件中定义变量: int i; 声明和定义都只需一次.
- Mathematics-基础:1+2+3+……+n
设Sn=1+2+3+……+n-1+n则有Sn=n+n-1+……+3+2+1两式相加得2Sn=(n+1)+(n+1)+……+(n+1)2Sn=n×(n+1)Sn=n×(n+1)/2
- TUM好用的工具
https://vision.in.tum.de/data/datasets/rgbd-dataset/tools?tdsourcetag=s_pctim_aiomsg
- process data
# version 1.0def connect_mysql(sql, oper_type="select", data_l=None): conn = pymysql.conne ...