[Codeforces 839C] Journey
2024-08-30 18:01:26
[题目链接]
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 ; }
最新文章
- jQuery学习之路(7)- 用原生JavaScript实现jQuery的某些简单功能
- 集合覆盖 顶点覆盖: set cover和vertex cover
- 关于ssh_copy_id脚本解析
- Android 视频播放器进度的处理
- Flash 二进制传图片到后台Java服务器接收
- ubuntu安装cacti错误
- ASP.NET 图片上传工具类 upload image简单好用功能齐全
- 【SGU 104】Little shop of flowers
- BZOJ-1013 球形空间产生器sphere 高斯消元+数论推公式
- annotation(@Retention@Target)详解
- c++标准库中几个常见的数据结构的区别和应用规则
- 李洪强经典iOS面试题11
- Java虚拟机的内存组成以及堆内存介绍
- 如何让图片在div里面剧中显示
- js动弹特效
- List<;String>; 和 ArrayList<;String>;的区别
- 关于Springboot整合mybatis启动的问题
- C++之异常捕获和处理
- dagger2的初次使用
- 防火墙禁ping:虚拟机ping不通主机,但主机可以ping虚拟机