codeforces 220 C. Game on Tree
题目链接
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;
}
codeforces 220 C. Game on Tree的更多相关文章
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 342E :Xenia and Tree
Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...
- Codeforces Edu3 E. Minimum spanning tree for each edge
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Codeforces 161 D. Distance in Tree (树dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...
随机推荐
- 【BZOJ】3238: [Ahoi2013]差异
[题意]给定长度为n的小写字母字符串,令Ti表示以i开头的后缀,求Σ[Ti+Tj-2*lcp(Ti,Tj)],1<=i<j<=n. [算法]后缀自动机 [题解]Σ(Ti+Tj)只与n ...
- Array和String测试与java.String.split
java.string.split() 存在于java.lang包中,返回值是一个数组. 作用是按指定字符或者正则去切割某个字符串,结果以字符串数组形式返回. 例 String [] toSort = ...
- 【leetcode 简单】第十四题 最后一个单词的长度
给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...
- HDU 1148 Rock-Paper-Scissors Tournament (模拟)
题目链接 Problem Description Rock-Paper-Scissors is game for two players, A and B, who each choose, inde ...
- [005] unique_sub_string
[Description] Given a string, find the largest unique substring. e.g. str[] = "asdfghjkkjhgf&qu ...
- 关于linux系统如何实现fork的研究(一)【转】
转自:http://www.aichengxu.com/linux/4157180.htm 引言 fork函数是用于在linux系统中创建进程所使用,而最近看了看一个fork()调用是怎么从应用到gl ...
- React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)
一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...
- 谁说运维用ELK没用?我就说很有用,只是你之前不会用【转】
1.安装JDK 1)登陆ORACLE官网 (http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html ...
- 设计模式之笔记--解释器模式(Interpreter)
解释器模式(Interpreter) 定义 解释器模式(Interpreter),给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 类图 描述 Expr ...
- vuex实例详解
vuex是一个专门为vue.js设计的集中式状态管理架构.状态?把它理解为在data中的属性需要共享给其他vue组件使用的部分. 简单的说就是data需要共用的属性 一.小demo 已经用Vue脚手架 ...