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

本文介绍了一个基于随机DFS算法的问题解决方法,该算法用于在一棵给定的树形结构中寻找特定节点,通过预处理和遍历技巧计算每个节点被访问的期望时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep=&#39;\n&#39;) ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值