codeforces 839 C 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.


题目:http://codeforces.com/contest/839/problem/C 
大致题意: 

给出一颗树,求从根节点出发,走到叶子节点经过的路径长度的数学期望。父节点到所有相邻子节点是等概率的。
解法: 
直接深搜可以得出答案。


代码:

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
bool used[100005]{false};
double dfs(vector<vector<int>> &vec,int src,unsigned long long dis){
	double rtn = 0;
	used[src] = true;
	int num = 0;
	for(auto dsc:vec[src]){
		if(!used[dsc]) {
			rtn +=dfs(vec,dsc,dis+1);
			num++;	
		}
	}
	used[src] = false;
	if(num == 0){
		return dis;
	}
	else return rtn/num;
}
int main(){
	int n;
	cin >> n;
	vector<vector<int> > vec(n);
	for(int i = 0 ; i < n-1 ; i++){
		int src,dsc;
		cin >> src >> dsc;
		vec[src-1].push_back(dsc-1);
		vec[dsc-1].push_back(src-1);
	}
	int num = 0;
	double res = dfs(vec,0,0);
	cout << fixed << setprecision(16) <<res << endl;
	return 0;
}


从零理解Brep:用Python玩转3D建模的边界表示法(附OpenCASCADE示例) 本文深入解析了边界表示法(Brep)在3D建模中的应用,通过Python和OpenCASCADE的实战示例,详细介绍了Brep的核心数据结构、布尔运算、网格转换及3D打印优化技巧。适合CAD设计、3D打印和计算机图形学领域的开发者学习与实践。 阅读详情

相关推荐

机器人视觉伺服控制入门:从IBVS到PBVS的实战指南(附Python代码示例)

本文深入解析机器人视觉伺服控制,对比了基于图像的视觉伺服和基于位置的视觉伺服两大核心方法。通过实战案例与Python代码示例,详细阐述了从误差计算、交互矩阵到速度控制的全流程,帮助读者掌握如何利用视觉反馈实现机器人的实时闭环控制,提升其在动态环境中的作业能力。

weixin_29061997的博客 273

C. Journey

C. Journey time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There arencities andn - 1roads in the Seven King...

weixin_34397291的博客 223

华为路由器静态路由配置实战:从零搭建企业级网络环境(含常见错误排查)

本文以真实企业组网案例为基础,详细解析华为路由器静态路由的实战配置流程。内容涵盖从网络拓扑规划、IP地址分配到具体的`ip route-static`命令配置,并重点讲解了缺省路由、浮动路由等进阶应用。文章还提供了系统化的排错指南,帮助网络工程师规避回程路由缺失、子网掩码错误等常见问题,是掌握企业级网络搭建与维护的实用指南。

weixin_29313817的博客 94

CodeForces 721C Journey(旅行时间和地点的优化)

3

Shyazhut 1391

零基础用AHK操作Excel:从数据读取到自动填写(附完整脚本解析)

本文为零基础用户详细介绍了如何使用AutoHotkey(AHK)通过COM接口高效操作Excel,实现从数据自动读取到填写的完整自动化流程。文章包含完整的脚本解析、环境搭建指南、核心代码逐行讲解以及实际应用案例,帮助办公人员大幅提升数据处理效率,告别重复劳动。

weixin_29202213的博客 567

注意力机制模块:CVPR 2026 新宠:MLLA(多尺度线性注意力)即插即用,在 YOLO 和 ConvNeXt 上全面涨点

MLLA核心创新点与实践价值摘要(146字) 清华大学LeapLab团队通过解构Mamba模型,发现其核心优势源于遗忘门机制与块设计,而非传统SSM架构。基于此提出的MLLA模块创新性地将这两项设计融入线性注意力框架:1)用位置编码替代循环计算保留遗忘门的位置感知能力;2)通过门控投影+DWConv+MLP重构高效块结构。在COCO检测任务中,MLLA使小目标AP提升11.6%而仅损失6%FPS,支持ONNX/TensorRT全链路部署,代码已开源。该工作为高分辨率视觉任务提供了近乎零代价的性能提升方案。

ZXSXJ的博客 169

Journey 数据库迁移工具使用教程

Journey 数据库迁移工具使用教程 1. 项目介绍 Journey 是一个基于 Go 语言开发的数据库迁移工具,旨在简化数据库迁移过程。它提供了一个易于实现的驱动接口,支持多种数据库类型,如 PostgreSQL、Cassandra、SQLite 和 MySQL(实验性支持)。Journey 的设计理念是避免使用硬编码的配置文件和魔法搜索路径,使得迁移过程更加灵活和可控。 2. 项目快速启动 ...

gitblog_00063的博客 940

【软考高级架构】论文范文12——论生成式AI在软件开发中的应用与挑战

随着生成式AI技术的跨越式发展,以大语言模型为代表的生成式AI正在深刻改变软件开发的传统模式。在代码生成、测试构造、文档编写等环节,生成式AI展现出显著的效率提升潜力,但其应用也面临着代码质量不可控、安全漏洞植入、版权合规等严峻挑战。本文以笔者主导的某大型金融企业智能研发辅助平台建设项目为案例,围绕生成式AI在软件开发中的应用实践与应对挑战的策略展开论述。笔者担任系统架构师,负责平台架构设计、模型选型、质量保障机制及安全防线构建。

qq837993702的博客 281

2017.10.25模拟 c journey

http://www.elijahqi.win/archives/1381 自己写的渣渣代码 只有20分 留作纪念吧 #include&lt;cstdio&gt; #include&lt;cstring&gt; #define N 110 #define M 110000 #define inf 0x3f3f3f3f struct node{ int st,ed; }data[N]...

Elijahqi 211

YOLO26涨点改进 | 全网独家首发 主干改进篇 | TGRS 2025 | RepViT-TD,告别庞大骨干!YOLO26 + RepViT-TD轻量又强大,实现更快更准的小目标检测!

本文提出将RepViT-TD轻量级主干网络应用于YOLO26目标检测框架,以提升小目标检测性能。RepViT-TD通过优化计算资源分配,强化前两层高分辨率特征提取能力,在保持轻量化的同时(仅4.85M参数)显著提升微小目标的检测精度(AI-TODv2数据集AP50达54.6%)。文章详细介绍了三种主干改进方案(RepVit_ldy1/2/3)的代码实现,并提供了完整的YAML配置文件修改指南,使模型能高效部署于边缘设备。实验表明,该方法在计算复杂度(38.19G FLOPs)和检测精度间取得了良好平衡。

Ai缝合怪、助力小伙伴高效发论文 340

YOLOv8改进 | 独家创新篇 | 给YOLOv8增加辅助可逆分支结构(PGI,1:1改进)(全网独家创新,附视频讲解)

这张图(图3)展示了可编程梯度信息(PGI)及其相关网络架构和方法。这种网络结构主要用于改进特征融合,以提高目标检测的性能。然而,由于信息瓶颈的存在,网络中可能会丢失一些信息。这是一种旨在减少信息丢失的网络设计。它通过可逆的列结构来尝试维持信息流通不受损失,但如图中“Heavy Cost”所示,这种结构会增加计算成本。c) 深度监督:这种方法通过在网络的多个层次中插入额外的监督信号来提高学习的效率和最终模型的性能。图中显示了通过深度监督连接的各个层。

Snu77的博客 3754

AI角色身份识别技术:从用户行为建模到深度学习实现

在人工智能交互领域,用户身份识别是实现个性化服务的基础技术。其核心原理是通过分析用户的多维度行为特征,构建独特的身份表征模型。从技术价值看,这不仅能提升交互的私密性和安全性,还能为虚拟助手、游戏NPC等场景提供差异化的沉浸式体验。具体实现上,传统方法依赖规则引擎和用户画像,而现代方案则采用深度学习模型对对话序列进行建模,捕捉细微的语言风格和交互模式。这些技术最终应用于私人AI助手、内容创作者虚拟角色和多用户对话系统等场景,实现AI对特定用户的精准识别。其中,基于Transformer架构的预训练模型和实时推

weixin_33895516的博客 456

【k8s故障处理篇】calico-node启动失败“Felix is not live: Get “http://localhost:9099/liveness” 解决办法(V1.30.3版本)

【k8s故障处理篇】calico-node启动失败“Felix is not live: Get “http://localhost:9099/liveness” 解决办法(V1.30.3版本)

jks212454的博客 1097

Codeforces839C 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 Seve

Jelly_acm的博客 1110

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

codeforces 839C Journey

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

abc1235454的博客 368

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[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++版本的破解文件

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

【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(图的遍历)

题目传送门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

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 Seven Ki...

weixin_33953249的博客 162
上一篇: codeforces 839 B Game of the Rows
ZhouHouQian
博客等级 码龄11年 3粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值