[Codeforces 839C] Journey
[题目链接]
http://codeforces.com/contest/839/problem/C
[算法]
概率DP
时间复杂度 : O(N)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 int tot , n;
int head[MAXN];
bool visited[MAXN];
double f[MAXN]; struct edge
{
int to , nxt;
} e[MAXN << ]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline double dp(int u,int fa)
{
int cnt = ;
if (visited[u]) return f[u];
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
cnt++;
}
if (cnt == )
{
visited[u] = true;
return f[u] = ;
}
f[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
f[u] += 1.0 / cnt * dp(v,u);
}
visited[u] = true;
return f[u];
} int main()
{ read(n);
for (int i = ; i < n; i++)
{
int u , v;
read(u); read(v);
addedge(u,v);
addedge(v,u);
}
memset(visited,false,sizeof(visited));
printf("%.10lf\n",dp(,)); return ; }
[Codeforces 839C] Journey的更多相关文章
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- Codeforces 839C Journey - 树形动态规划 - 数学期望
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...
- Codeforces 839C Journey【DFS】
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...
- Journey CodeForces - 839C
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...
- CodeForces 788B--Weird journey
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Little ...
- CodeForces 721C Journey
$dp$,拓扑排序. 记$dp[i][j]$表示走到节点$i$,走过了$j$个点的最小时间,然后就可以递推了.要注意的是节点$1$的入度一开始不一定等于$0$. #pragma comment(lin ...
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- CodeForces 721B Journey (DP)
题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t. 析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊...如果是有向图就好做多了,枚举边,然后打印就好,dp[ ...
- Codeforces 789D Weird journey - 欧拉路 - 图论
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...
随机推荐
- linux shell symbolic link & soft link, symbol link, link
linux shell symbolic link symbolic link https://en.wikipedia.org/wiki/Ln_(Unix) https://stackoverflo ...
- poj2446 Chessboard 【最大匹配】
题目大意:一个n*m的棋盘,某些格子不能用,问用1*2的骨牌能否完全覆盖这个棋盘,当然,骨牌不能有重叠 思路:显然黑白染色后,一个骨牌只能覆盖一个白色格子和一个黑色格子,然后我们间二染色建图,看能否有 ...
- POJ3321Apple Tree【dfs 树状数组】
题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...
- 洛谷P2527 [SHOI2001]Panda的烦恼
题目描述 panda是个数学怪人,他非常喜欢研究跟别人相反的事情.最近他正在研究筛法,众所周知,对一个范围内的整数,经过筛法处理以后,剩下的全部都是质数,不过panda对这些不感兴趣,他只对被筛掉 ...
- 【分段哈希】H. Paint the Wall
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/H [题意] 在一个长为H,宽为W的白墙上选一个矩形区域涂颜色,后涂的颜色会覆盖先 ...
- Android基本动画
Android基本动画 Android Animations动画使用详解 一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XM ...
- SyntaxError: expected expression, got '<'异常错误
引入jQuery文件,浏览器报SyntaxError: expected expression, got '<'错误,但是jQuery文件可以在网络一栏看到,最后检查是springMVC定义的拦 ...
- java基础 3 Object通用方法(1)
Object通用方法(1) clone: 浅复制 被复制对象的所有变量都含有与原对象相同的值,而所有对其他对象的引用仍然指向原来的对象,换言之,浅复制仅仅复 ...
- foobar2000实现用手机远程控制PC命令行版
实现此功能主要是在手机上使用Telnet命令实现下一首播放. 1.安装Telnet守护进程插件:http://www.foobar2000.org/components/view/foo_telnet ...
- 对CSS尺寸单位'em'的长期误解
一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...