题目链接

codeforces 220 C. Game on Tree

题解

对于 1节点一定要选的

发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一

代码

#include<cstdio>
#include<algorithm> const int maxn = 1000007;
struct node {
int u,v,next;
} edge[maxn << 1] ;
int head[maxn],num = 0;
inline void add_edge(int u,int v) {
edge[++ num].v = v;edge[num].next = head[u] ;head[u] = num;
}
int n;
double ans = 0;
double dep[maxn];
void dfs(int x,int fa) {
ans += (1 / dep[x]);
for(int i = head[x]; i; i = edge[i].next) {
int v = edge[i].v;
if(v == fa)continue;
dep[v] = dep[x] + 1.0;
dfs(v,x);
}
}
int main() {
scanf("%d",&n);
for(int u,v,i = 1;i < n;++ i){
scanf("%d%d",&u,&v);
add_edge(u,v);add_edge(v,u) ;
}
dep[1] = 1;
dfs(1,0);
printf("%.10lf",ans);
return 0;
}

最新文章

  1. 从NSGA到 NSGA II
  2. 用MS自带的VS构建joint语句
  3. Pawn Brotherhood
  4. javascirpt怎样模仿块级作用域(js高程笔记)
  5. OOP的方法
  6. window10 hello 人脸识别无法启动相机的问题
  7. 安装vue-cli时-4058报错的解决方法
  8. XBee PRO 900HP远距离无线模块
  9. Codeforces997D Cycles in product 【FFT】【树形DP】
  10. Zipkin Server Configuration Using Docker and MySQL[转]
  11. Maven+Spirng+Mybatis+CXF搭建WebService服务
  12. SpringBoot集成ActiveMQ
  13. CSS3 的box-shadow进阶之 - 动画篇 - 制作辐射动画
  14. A guess 解题报告
  15. Leetcode 22
  16. noip2007-4
  17. Django(完整的登录示例、render字符串替换和redirect跳转)
  18. nullptr
  19. 在C++11中实现监听者模式
  20. 模拟源码深入理解Vue数据驱动原理(2)

热门文章

  1. [php排错] Forbidden You don&#39;t have permission to access / on this server.
  2. 【NOIP2013提高组T3】加分二叉树
  3. Metasploit 一些重要模块使用介绍
  4. Redis笔记之常用命令
  5. 重载jquery on方法实现click事件在移动端的快速响应
  6. Linux轻量级自动运维工具-Ansible浅析【转】
  7. [写出来才有价值系列:node.js]node.js 02-,learnyounode
  8. golang-goroutine和channel
  9. 【hdoj_2566】统计硬币(母函数?)
  10. MVC – 4.mvc初体验(1)