【Codeforces 696B】Another Sith Tournament &【JZOJ 4647】寻找

Description

Codeforces

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.

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 to search in the cities. A pseudo code of this algorithm is as follows:

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(1n105) — the number of cities in USC.

The second line contains n - 1 integers p2,p3,...,pn(1pi<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 in USC.

JZOJ

Bob和Alice出去度蜜月,但Alice不慎走失,Bob在伤心过后,决定前去寻找Alice。
他们度蜜月的地方是一棵树,共有N个节点,Bob会使用下列DFS算法对该树进行遍历。

starting_time是一个容量为n的数组
current_time = 0
dfs(v):        
       current_time = current_time + 1        
       starting_time[v] = current_time        
       将children[v]的顺序随机排列 (每个排列的概率相同)        
       // children[v]v的直接儿子组成的数组        
       for u in children[v]:               

1是这棵树的根,Bob会从1出发,即运行dfs(1),现在他想知道每个点starting_time的期望值。

Solution

期望值=概率*权值,

设每个子树的总点数为 sii ,祖先f有h个儿子,对于f的每一个儿子s,每一轮走到它的概率都是 1h (就是在走它之前无论做了多少个其他点),
第一轮走就直接走,
第二轮走,那么在走它之前的增加的期望步数就是 siqsish1
第三轮就是把除了点s以外的所有的 sison 的两两组合,也就是 =C1h2C2h1(siqsis)=2h1(siqsis) ,(把原公式拆开来分开来写即可发现),
第四轮同理,为: =C2h2C3h1(siqsis)=3h1(siqsis)
…..
所以,对于每个点s,走到它之前增加的期望值就是:

(siqsis)i=1h1in

化简一下:
siqsis2

最后预处理以后直接扫一遍即可。
复杂度: O(n)

Code

#include<cstdio> 
#include<cstdlib>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define efo(i,q) for(int i=A[q];i;i=B[i][0])
#define iff() if(B[i][1]!=fa)
using namespace std;
typedef double db;
const int N=100500;
int read(int &n)
{
    char ch=' ';int q=0,w=1;
    for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
    if(ch=='-')w=-1,ch=getchar();
    for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int n;
int B[N][2],A[N],B0;
int b[N],b1[N];
db ans[N];
void link(int q,int w){B[++B0][0]=A[q],A[q]=B0,B[B0][1]=w;}
int dfs1(int q,int fa)
{
    b[q]=1;
    efo(i,q)iff()b[q]+=dfs1(B[i][1],q),b1[q]++;
    return b[q];
}
void dfs2(int q,db s,int fa)
{
    ans[q]=++s;db s1=0.5;
    efo(i,q)iff()dfs2(B[i][1],s+(b[q]-b[B[i][1]]-1)*s1,q);
}
int main()
{
    int q,w;
    read(n);
    fo(i,2,n)read(q),link(q,i);
    dfs1(1,0);
    dfs2(1,0,0);
    fo(i,1,n)printf("%.1lf ",ans[i]);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值