codeforces 220 C. Game on Tree
2024-08-26 11:50:15
题目链接
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;
}
最新文章
- 从NSGA到 NSGA II
- 用MS自带的VS构建joint语句
- Pawn Brotherhood
- javascirpt怎样模仿块级作用域(js高程笔记)
- OOP的方法
- window10 hello 人脸识别无法启动相机的问题
- 安装vue-cli时-4058报错的解决方法
- XBee PRO 900HP远距离无线模块
- Codeforces997D Cycles in product 【FFT】【树形DP】
- Zipkin Server Configuration Using Docker and MySQL[转]
- Maven+Spirng+Mybatis+CXF搭建WebService服务
- SpringBoot集成ActiveMQ
- CSS3 的box-shadow进阶之 - 动画篇 - 制作辐射动画
- A guess 解题报告
- Leetcode 22
- noip2007-4
- Django(完整的登录示例、render字符串替换和redirect跳转)
- nullptr
- 在C++11中实现监听者模式
- 模拟源码深入理解Vue数据驱动原理(2)
热门文章
- [php排错] Forbidden You don&#39;t have permission to access / on this server.
- 【NOIP2013提高组T3】加分二叉树
- Metasploit 一些重要模块使用介绍
- Redis笔记之常用命令
- 重载jquery on方法实现click事件在移动端的快速响应
- Linux轻量级自动运维工具-Ansible浅析【转】
- [写出来才有价值系列:node.js]node.js 02-,learnyounode
- golang-goroutine和channel
- 【hdoj_2566】统计硬币(母函数?)
- MVC – 4.mvc初体验(1)