Codeforces839C Journey

Codeforces 721 C. Journey(拓扑排序+DP) codeforces每日一练。 题意: 给定n个点,m条有向边,以及k时间。求不超过k时间1-n最多能经过多少个点。 思路: 数据<=5000,说明是个暴力dp。 那么可以用dp[i][j]维护从1到i点经过了j个点,然后初始化为inf,再设dp[1][1]=0,保证每个dp都是由1出发的。 因为是有向无环图,所以我们可以在拓扑排序的时候进行dp,循环n个点,复杂度On^2,然后多开一个pre[i][j]记录经过i点时的前一个点。 由于开了两个5000*5000的数组,所以得用int,如果用long long会MLE。 代码如下: #include #define ll long long # 立即下载
C. Journey
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

Input

The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.

Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road.

It is guaranteed that one can reach any city from any other by the roads.

Output

Print a number — the expected length of their journey. The journey starts in the city 1.

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

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples
input
4
1 2
1 3
2 4
output
1.500000000000000
input
5
1 2
1 3
3 4
2 5
output
2.000000000000000
Note

In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.

In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.

————————————————————————————————————

题目的意思是是给出一棵树,权值均为1,从1号点出发,遇到岔路走每个路的概率相等

不能走回头路,问走路长度的期望

思路:期望等于每个叶子节点走到的路程*走到这个点的概率,dfs维护长度和概率

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define mod 10000007
#define mem(a,b) memset(a,b,sizeof a)

struct node
{
    int u,v,next;
} tree[200005];

int s[100005],vis[100005],cnt,ct;
double sum;

void init()
{
    mem(s,-1);
    cnt=0;
    mem(vis,0);
}


void add(int u,int v)
{
    tree[cnt].u=u;
    tree[cnt].v=v;
    tree[cnt].next=s[u];
    s[u]=cnt++;

    tree[cnt].u=v;
    tree[cnt].v=u;
    tree[cnt].next=s[v];
    s[v]=cnt++;
}

void dfs(int x,int sm,int fa,double p)
{


    int k=0;
    for(int i=s[x]; ~i; i=tree[i].next)
    {
        if(tree[i].v==fa)
            continue;
        k++;
    }
    if(k==0)
    {
        sum+=sm*p;
        return;
    }
    for(int i=s[x]; ~i; i=tree[i].next)
    {
        if(!vis[tree[i].v])
        {
            vis[tree[i].v]=1;
            dfs(tree[i].v,sm+1,x,p/k);
        }
    }

}

int main()
{
    int n,u,v;
    while(~scanf("%d",&n))
    {
        init();
        if(n==1)
        {

            printf("0.0000000000\n");
            continue;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        vis[1]=1;
        sum=0;
        ct=0;
        dfs(1,0,-1,1);
        printf("%.10f\n",sum);
    }
    return 0;
}


深入排查127.0.0.1拒绝连接:从端口占用到Tomcat服务启动的完整解决方案 本文针对开发者访问127.0.0.1时遭遇“拒绝连接”的常见问题,提供了一套完整的排查与解决方案。文章从端口占用检查入手,详细讲解了使用netstat命令识别进程,并深入分析了服务监听地址、IPv6配置、防火墙以及Tomcat服务安装与启动等核心环节。通过实战演练,指导读者系统性地解决从网络层到应用层的各类连接问题,最终确保本地服务成功运行并可通过本地回环地址正常访问。 阅读详情

相关推荐

利用Matlab高效处理Comsol仿真数据的可视化技巧

本文详细介绍了如何将Comsol仿真数据高效导入Matlab并进行专业可视化与后处理。核心内容包括正确的数据导出格式设置、三维矩阵重构方法、二维截面与三维体渲染技巧,以及基于Matlab的定量分析与批处理自动化流程,帮助工程师和科研人员深度挖掘仿真数据价值,提升工作效率。

weixin_29065791的博客 366

codeforces-839-C-Journey(旅行期望值)(构建树,深搜总结)

题目描述如下 1、数据结构选择 树是图的退化,所以采用二位数组的形式对树进行存储。 构建形式 vector G[N] ; 要看数据是怎样输入的,进而采用不同的构建方法 我的感觉是,看题目怎么说,要是明确指出是树,那就单向存储, 要是说是图,但它还是树,那就建立双向的联系。 对于双向树: 有几个技巧, 1:求结点的子分支数 2:只找找该结点的子代,不找父代 对于单向树: 暂时没想到,就是顺着往下找吧 2、算法(深搜) 读懂题意,并进行子问题的分解很关键,要深搜,不可能像数学公式一样带入数直接求得最终结

weixin_51232943的博客 496

快速傅里叶变换(FFT)原理详解:从理论到实践

本文系统介绍了快速傅里叶变换(FFT)的理论与实践。首先阐述了傅里叶变换的数学定义和离散傅里叶变换(DFT)的矩阵表示,指出其O(N²)计算复杂度限制了实际应用。然后详细解析了Cooley-Tukey算法的核心思想,包括分治策略、蝶形运算原理和旋转因子的周期性特性,将复杂度降至O(Nlog₂N)。文章比较了按时间抽取(DIT)和按频率抽取(DIF)两种FFT算法实现,并讨论了FFT在信号处理等领域的应用及其局限性。通过理论分析和实例说明,展现了FFT如何成为现代数字信号处理的基石算法。

qq_42212808的博客 2958

codeforces 839C Journey(图的遍历)

题目传送门There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.Theon and Yara Greyjoy are on a horse in the first cit

henuzxy 1259

高速接口电平标准互连设计:LVPECL、PECL与LVDS的实战解析

本文深入解析了LVPECL、PECL与LVDS三种高速接口电平标准的互连设计实战。针对信号共模电压、阻抗匹配等核心差异,详细对比了直流耦合与交流耦合两种主流方案,并提供了电阻网络计算、布局布线要点及光模块等特殊场景下的选型建议,帮助工程师解决高速信号间的“翻译”难题,确保信号完整性。

weixin_29198045的博客 546

OneNet平台MQTT连接避坑指南:C语言Token计算实战(附完整代码)

本文详细解析了OneNet平台MQTT连接中的Token计算机制,提供了C语言实现的完整代码和实战指南。通过HMAC算法和Base64编码,开发者可以高效生成安全凭证,避免常见连接问题,适用于物联网设备开发。

weixin_29193359的博客 470

CodeForces 721C Journey(DP)

题目链接:http://codeforces.com/problemset/problem/721/C

ACM签到中... 762

高效打卡系统设计:从行为心理学到实践工具

打卡系统作为现代时间管理的重要工具,其核心原理植根于行为心理学中的即时反馈机制和习惯养成理论。通过科学设计的打卡周期和可视化反馈,能有效提升个人自律性和目标达成率。在技术实现上,从纸质记录到数字化工具(如Notion、Excel自动化模板)的演进,为不同场景提供了灵活解决方案。特别在习惯追踪和进度管理领域,结合热力图生成、数据统计等技巧,打卡系统能转化为强大的自我监督工具。本文通过实践案例,详解如何构建兼顾弹性和约束力的个性化打卡方案,帮助读者突破'三天打鱼两天晒网'的常见困境。

weixin_30047059的博客 347

Codeforces - Journey

题目链接:Codeforces - Journey 令dp[x][i] 为1到点x,经过i个点的最短距离。 然后DAG上面dp即可。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define int long long using namespace std...

青烟绕指柔的博客 500

DDS信号发生器设计避坑指南:从AD9834配置到巴特沃斯滤波实战

本文详细解析了基于AD9834芯片的DDS信号发生器设计中的关键技术要点与常见陷阱,涵盖时钟源选择、寄存器配置时序、巴特沃斯滤波器设计等核心内容。针对中高频段信号质量优化,提供了实用的解决方案和代码示例,帮助工程师规避设计误区,提升信号发生器的性能和稳定性。

2y3u4i5o6p的博客 794

【差分运放电路加偏置电压

摘要:该电路采用单电源供电运放(A3V3)处理双极性差分信号,通过1V直流偏置解决负半周信号削波问题。关键设计包括:15kΩ/10kΩ电阻分压建立1V基准,对称电阻网络实现2.5倍差分放大,0.1μF电容滤除电源噪声。适用于传感器信号调理(如电流采样、压力传感)和3.3V系统处理±0.5V交流信号,输出0-2.5V适配ADC输入。注意事项强调电阻匹配对共模抑制的重要性,以及电源滤波电容需靠近运放引脚布局。

qq_37999850的博客 614

codeforces 839C Journey

题意就是给你一个n个点的树,然后然你判断到每个叶子节点的期望。 因为有n-1条边,所以每个叶子节点只会有一条边与它相连,所以只要dfs一边,然后纪录一下到这个点得长度,以及概率就可以了,注意一下,如果这个点不是根节点,那么计算这个与这个节点相邻的点的概率要减去1,因为这个点一定是由一个点扩展而来的。比如样例1,到2这个几点的概率是1/2,然后再扩展4这个节点概率就是1/(2-1),2是与2这个节点...

abc1235454的博客 368

编织数据的实时脉络——构建基于MySQL的数据编织平台,实现实时数据治理

除了上述基本设置外,我们还可以根据实际应用场景自定义一些特定的监控项目。例如,对于某些高并发读写的表,可能需要特别关注它们的锁等待时间和回滚段使用率;而对于涉及到跨库关联查询的操作,则应当着重检查网络延迟及缓存命中率等因素。-- 获取InnoDB行级锁等待情况-- 统计各表的缓存命中率这段SQL脚本不仅展示了如何查询InnoDB引擎下的锁争用状况,还介绍了计算表级别缓存命中率的方法,有助于深入理解系统的运行状态。

墨夶的博客 1800

[Codeforces 839C] Journey

[题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...

104

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Codeforces 839C Journey

题目链接:http://codeforces.com/contest/839/problem/C题解:昨晚脑子一抽,算了半天的平均期望。。。代码:#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; #define met(a,

silver_bone的博客 732

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

codeforces 839 C Journey

C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n cities and n - 1 roads in the Seven Kingdoms

Qian998的博客 776

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【CodeForces - 839C Journey】 DFS

C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n cities and n - 1 roads in the Seve

于心有愧丶 385

Journey CodeForces - 839C

There arencities andn - 1roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first ...

aocan6909的博客 239

CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #include <bits/stdc++.h> using namespace std; const int N = 100005; int n; vector<i...

weixin_34024034的博客 148

CodeForces - 839C journey

Example Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 题意:输入点的个数N,然后输入N-1条边(无向),判断叶子的期望。 期望=概率*路径#include<io

Seeyouer的博客 409

CodeForces - 839C Journey dfs+期望

题意:有一棵树,根节点为1。从根节点出发,每次不能经过已经到达过的结点。求最长能走的长度的数学期望。 题解:期望没学好,昨天4wa。从根结点走,最长的长度就是根节点到叶子结点的长度。所以求出根节点到每个叶子结点的概率*长度就是期望了。

ACZone 351

CodeForces - 839C Journey 期望 dfs

先大喊:我是傻逼!首先这个题很简单 n 个结点, n-1 条边, 不会出现环 我写 MLE 了,因为我很自信的在 dfs 里面判 v 的父结点的时候判成了第 1 个结点,所以,,GG要是说求期望的话 推一下就能发现答案就是 到每个结点的概率之和#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring>

床前明月光 503

Codeforces 839C-Journey

Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n cities and n - 1 roads in the Seven K

wang_128的博客 450

Codeforces 839C Journey【DFS】

C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output There are n cities and n - 1 roads in the Seve...

weixin_33963189的博客 111

【暴力DP+思维】Codeforces839 C - Journey

C - Journey Recently Irina arrived to one of the most famous cities of Berland— the Berlatov city. There arenshowplaces in the city, numbered from1ton, and some of them are connected by one-...

qq_41037114的博客 643

Codeforces 839C Journey(期望dp)

传送门 题意:给一棵大小为n,边长都为1的树,从1号点开始每次等概率地走向某个非父节点,走到叶节点为止,求走的长度的期望。 题解:定义f[x]为从x出发走到不能再走的期望,dfs一次,用子节点更新父节点即可。 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const

KGV093的博客 357
上一篇: Codeforces839B Game of the Rows
下一篇: Codeforces839D Winter is here
Jelly_acm
博客等级 码龄11年 89粉丝 421原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值