首页 > 其他分享 >Codeforces Round #362 (Div. 2)-D. Puzzles

Codeforces Round #362 (Div. 2)-D. Puzzles

时间:2023-06-12 17:31:39浏览次数:47  
标签:city Barney int number Puzzles time Div 362 he


原题链接


D. Puzzles



time limit per test



memory limit per test



input



output



Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.



Codeforces Round #362 (Div. 2)-D. Puzzles_#define


Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS

let starting_time be an array of length n
current_time = 0
dfs(v):
	current_time = current_time + 1
	starting_time[v] = current_time
	shuffle children[v] randomly (each permutation with equal possibility)
	// children[v] is vector of children cities of city v
	for u in children[v]:
		dfs(u)

As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).

Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.



Input



The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC.

The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i



Output



In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].

Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.



Examples



input



7
1 2 1 1 4 4



output



1.0 4.0 5.0 3.5 4.5 5.0 5.0



input



12
1 1 2 2 4 4 3 3 1 10 8



output



1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0







d[i]表示以第i个节点为根的树上节点个数

starting_time[i] = ans[i] = ans[j] + (d[j] - d[i] - 1) * 0.5 + 1;(j是i的父节点)


#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;

int d[maxn];
double ans[maxn];
vector<int> v[maxn];
void dfs(int j, int f){
	
	d[j] = 1;
	for(int i = 0; i < v[j].size(); i++){
		if(f == v[j][i])
		 continue;
		dfs(v[j][i], j);
		d[j] += d[v[j][i]];
	}
}
void Dfs(int j, int f){
	
	for(int i = 0; i < v[j].size(); i++){
		if(f == v[j][i])
		 continue;
		int p = v[j][i];
		ans[p] = ans[j] + (d[j] - d[p] - 1) * 0.5 + 1;
		Dfs(p, j);
	}
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, a;
	
	scanf("%d", &n);
	for(int i = 2; i <= n; i++){
		scanf("%d", &a);
		v[a].push_back(i);
	}
	dfs(1, -1);
	ans[1] = 1;
	Dfs(1, -1);
	printf("%.1lf", ans[1]);
	for(int i = 2; i <= n; i++)
	 printf(" %.1lf", ans[i]);
	puts("");
	return 0;
}

标签:city,Barney,int,number,Puzzles,time,Div,362,he
From: https://blog.51cto.com/u_16158872/6464288

相关文章

  • Codeforces Beta Round #29 (Div. 2, Codeforces format)-D. Ant on the Tree
    原题链接D.AntontheTreetimelimitpertestmemorylimitpertestinputoutputConnectedundirectedgraphwithoutcyclesiscalledatree.Treesisaclassofgraphswhic......
  • BestCoder Round #71 (div.2)1001KK's Steel
    题意:中文题思路:其实我们不去考虑N,我们只考虑最优切割策略:     首先肯定是尽量的小即1、2     既要不相等,又不能构成三角形,即每次为当前数列中最大的两项的和     那么,构成的数列为1,2,3,5,8,......     这样我们只要求最接近且小于等于N的......
  • Codeforces Round 877 (Div. 2)
    Preface补题这场补题的时候直接成腐乳了,A题挂两发,B题挂两发,C题挂一发是真的抽象虽然D是个套路题看一眼就秒了,但如果真的比赛时候打真要罚时爆炸了的说后面的EF还是做不来啊岂可修,不过都很有启发性让人感觉神清气爽,不像傻逼蓝桥杯花钱买罪受A.BlackboardList刚开始想错方......
  • Codeforces Round 876 Div2 A-D题解
    CodeforcesRound876Div2A-D题解A.TheGoodArray这个题就是问你对于\(i\leqn\),要求前面后面至少\(ceil(\frac{i}{k})\)个1那我们就贪心的每k个放一个1,或者直接用数学算一下就好了AC代码#include<bits/stdc++.h>usingnamespacestd;constexprintlimit=(......
  • [Codeforces Round 876 (Div. 2)][Codeforces 1839D. Ball Sorting]
    题目链接:D-BallSorting题目大意:需要对一个排列按照指定操作进行排序。操作一:在数字间插入一个特殊点,可执行不超过\(k\)次;操作二:将在特殊点旁的数移动到任意位置。所有操作结束后特殊点会消失,要求对所有\(k\in[1,n]\),求出操作二的最少操作次数。分析题意可以得出,操作一放......
  • 【若归】 【LGR-142-Div.4】洛谷入门赛 #13赛后反思
    比赛链接:【LGR-142-Div.4】洛谷入门赛#13rk288,比前几次差(可能是因为rated?)A十年OI一场空,不开longlong见祖宗#include<bits/stdc++.h>usingnamespacestd;intmain(){ longlongintn; cin>>n; cout<<"8"<<12*(n-2)<<""<<6*(n-......
  • CF1338 Div.1 做题记录
    ACF题面假定用到的最大的数是\(x\),那么一个数最大可以增大\(2^x-1\)。题目只要求不降,所以求出将\(a_i<a_{i-1}\)变成\(a_i=a_{i-1}\)时需要增大的最大值。求出这个数的二进制位数即可。点击查看代码#include<bits/stdc++.h>#defineullunsignedlonglong#definell......
  • Codeforces Round 876 (Div. 2) 题解 A - D
    A.TheGoodArray题目大意给定两个整数\(n\)和\(k\),规定一个\(01\)数列为好的的条件是,对于\(1\simn\)中任意的\(i\),都有:\(a\)的前\(i\)个元素中至少有\(\lceil\frac{i}k\rceil\)个都是\(1\)。\(a\)的后\(i\)个元素中至少有\(\lceil\frac{i}k\rceil\)个都是......
  • Codeforces Round 876 (Div. 2) A-D
    比赛地址A.TheGoodArray题意:定义一个数组是good的要求有:从左往右所有的i,前i个数中至少有[i/k]个数是1从右往左所有的i,前i个数中至少有[i/k]个数是1问good数组对于n而言最少需要多少个1Solution先从左往右填1,直到满足第一个条件,然后从右往左填1,直到满足第二个条件voidso......
  • div元素自适应屏幕大小
    简单介绍一下实现方式(结尾处有代码)1.首先创建一个根元素,将这个跟元素宽高设置为100%,当然,用100vw、100vh也可以,并且将根元素设置为相对定位。2.再创建我们要实现自适应大小的元素,自适应元素我们要给固定的宽高。可以按照常见的屏幕分辨率赋值,1920*1080或者2560*1440。(注:至于为什......