hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II

直接枚举就好了,当概率极小时贡献值可以忽略。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int maxn = 1e5 + 5;

int N, D[maxn];
vector<int> G[maxn];

void init () {
	for (int i = 1; i <= N; i++) G[i].clear();

	int t;
	for (int i = 2; i <= N; i++) {
		scanf("%d", &t);
		G[t].push_back(i);
	}

	queue<int> Q;
	D[1] = 0;
	Q.push(1);

	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();

		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			D[v] = D[u] + 1;
			Q.push(v);
		}
	}
	sort(D + 1, D + 1 + N);
}

int main () {
	while (scanf("%d", &N) == 1) {
		init ();

		double ans = 0;
		for (int i = 0; i < N && i < 100; i++) {
			int u = N - i;
			double w = 0;
			for (int j = N; j > u; j--)
				w += 1.0 * (D[u] + 1) * (D[j] + 1) / (D[u] + D[j] + 2);
			double t = pow(2, N-u+1) - (N+1) / pow(2, u-1);
			ans += w / t;
		}
		printf("%.6lf\n", ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-23 19:58:09

hdu 5425 Rikka with Tree II(暴力)的相关文章

HDU 5831 Rikka with Parenthesis II(六花与括号II)

HDU 5831 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math ta

HDU 5423:Rikka with Tree Dijkstra算法

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有

hdu 5424 Rikka with Graph II 哈密顿通路

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

hdu 5423 Rikka with Tree 树的性质

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 165    Accepted Submission(s): 85 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s

HDU 5423 Rikka with Tree(水题)

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 149 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

HDU 5424——Rikka with Graph II——————【哈密顿路径】

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1051    Accepted Submission(s): 266 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situati

hdu 5423 Rikka with Tree(dfs)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的连接情况: (1)自环(这里不需要考虑): (2)最后一条边将首和尾连接,这样每个点的度都为2: (3)最后一条边将首和除尾之外的点连接或将尾和出尾之外的点连接,这样相应的首或尾的度最小,度为1: (4)最后一条边将首和尾除外的两个点连接,这样就有两个点的度最小,度都为1 如果所给的图是联通的话,那

hihoCoder #1454 : Rikka with Tree II

Description 一个\(n\)个节点的树,先根遍历为\(1...n\).已知两个数组,一个数组表示是否是叶节点,另一个数组表示十分有右兄弟节点...'?'表示未知,求方案数\(n\leqslant 500\). Solution 区间DP. DFS序的一段表示一个子树. \(f[l][r][0/1]\)表示[l,r]变成几个子树,并不是一个...右边是否还能放的方案数... 为了方便我多写了一个函数A,他表示仅有一个子树的方案数.. Code #include <bits/stdc++.