pat a1053 给定权值和,求路径

PAT1053多叉树带权路径搜索回溯算法 1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight W**i assigned to each tree node T**i. The **weight of a path from **R to L is defined to be the sum of the weights ... 阅读详情

题目:

https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512

https://www.nowcoder.com/pat/5/submission-detail/64114048

1053 Path of Equal Weight (30分)

Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0<N≤100, the number of nodes in a tree, M (<N), the number of non-leaf nodes, and 0<S<2​30​​, the given weight number. The next line contains N positive numbers where W​i​​ (<1000) corresponds to the tree node T​i​​. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence {A​1​​,A​2​​,⋯,A​n​​} is said to be greater than sequence {B​1​​,B​2​​,⋯,B​m​​} if there exists 1≤k<min{n,m} such that A​i​​=B​i​​ for i=1,⋯,k, and A​k+1​​>B​k+1​​.

Sample Input:

20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19

Sample Output:

10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2

 

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=110;

struct Node
{
	int weight;  //数据域 
	vector<int> child;   //指针域 
}node[maxn];  //结点数组 

bool cmp(int a,int b)
{
	return node[a].weight>node[b].weight; //按结点数据域从大到小排序 
}

int n,m,S;  //结点数、分支结点数、给定的和 
int path[maxn];  //记录路径 

//当前访问结点为index,numNode为当前路径path上的结点个数 
//sum为当前的结点权值之和 
void DFS(int index,int numNode,int sum)
{
	if(sum>S) return;
	if(sum==S) 
	{
		if(node[index].child.size()!=0) return;
		for(int i=0;i<numNode;i++)
		{
			printf("%d",node[path[i]].weight);
			if(i<numNode-1) printf(" ");
			else printf("\n");
		}
		return;
	}
	for(int i=0;i<node[index].child.size();i++)
	{
		int child=node[index].child[i];
		path[numNode]=child;
		DFS(child,numNode+1,sum+node[child].weight);
	}	
}

int main()
{
	scanf("%d%d%d",&n,&m,&S);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&node[i].weight);
	}
	int id,k,child;
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&id,&k);
		for(int j=0;j<k;j++)
		{
			scanf("%d",&child);
			node[id].child.push_back(child);
		}
		sort(node[id].child.begin(),node[id].child.end(),cmp);
	}
	path[0]=0;
	DFS(0,1,node[0].weight);
	
	return 0;	
}

 

H3C-V7防火墙配置SSL VPN IP资源典型案例(华三) 阅读详情

相关推荐

基于Java+SpringBoot+Vue的中小企业设备管理系统的设计与开发(源码+lw+部署文档+讲解等)

💗博主介绍:✌新人博主,工作经验两年+、专注于Java、小程序技术领域和毕业项目实战✌💗🌟文末获取源码+数据库🌟

2301_76502068的博客 1384

题目——PAT A1053(从树的角度看DFS)

题目链接 #include <iostream> #include <cstdio> #include <queue> #include <vector> using namespace std; const int MAXN = 101; struct TreeNode { int weight; vector<int> children; // 优先将ID值较大的子结点放在前面 } tree[MAXN];..

weixin_43878652的博客 202

Nginx反向代理MinIO私有桶签名失效?一招解决Host头校验问题

本文详细解析了Nginx反向代理MinIO私有桶时出现的签名失效问题,重点解决了Host头校验导致的"SignatureDoesNotMatch"错误。通过精准配置proxy_set_header保持Host头一致性,并提供了二级目录代理和调试指南,帮助开发者高效解决这一常见技术难题。

weixin_29201313的博客 321

PAT 甲级 A1053 Path of Equal Weight(树的遍历)

Given a non-empty tree with root R, and with weight Wi​ assigned to each tree node Ti​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.Now given any weighted tree, you are

m0_51711089的博客 325

高通SDX35:SGMII接口PHY RTL8211FSI-CG驱动适配

高通SDX35 RTL8211F SGMII

不秃头的程序猿Gabriel的博客 787

3dmax文件格式转换——.max 转换为 .flt(解决转换后.flt没有纹理贴图的问题)

本文主要介绍如何在3dmax中将.max格式文件转换为.flt格式文件,并解决转换后的文件没有纹理贴图的问题。 在3dmax中,将.max文件转换为.flt格式,只需导出选择.flt格式即可,但是导出的文件在osgEarth中加载后,缺少纹理和贴图。 下面是解决方案: 1、选中加载的模型,然后点击 渲染-> 渲染到纹理 2、将 渲染到纹理 子窗口往下拉,点击 添加 按钮,添加 ...

cchangcs 6175

PAT A1053(DFS+vector)

主要用dfs,先将权重从大到小排序,然后我们在dfs

执念斩长河 546

8.5华为OD机试真题 新系统 - 最长不连续子串 (Java/Py/C/C++/Js/Go)

8.5华为OD机试真题 新系统 - 最长不连续子串 (Java/Py/C/C++/Js/Go)

算法大师! 629

1053 Path of Equal Weight (30 分) dfs,树

Given a non-empty tree with root R, and with weight W​i assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R t...

凡心所向,素履所往,生如逆旅,一苇以航 179

30天Python入门(第一天:Python入门)

30天带你入门Python,这是第一天

Science prince的博客 584

ICC2:平行打孔create_pg_stapling_vias

power rail(follow pin)不总是metal1一成不变的,当需要使用metal2做power rail,而standard cell的pg pin仍然是metal1时就需要平行打孔,从metal2到metal1。 以往都是用create_pg_vias -allow_parallel_objects来做,但这样有两个弊端,一是需要抓出所有的via12(包括standard cell内部的和出pin绕线产生的),添加routing blockage,防止create pg via的时候产生d

m0_61544122的博客 2260

PAT甲级1053:Path of Equal Weight (30)

题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tree, you ar

博客简介 324

【数据集】3D-GloBFP:全球首个三维建筑轮廓数据集

【数据集】3D-GloBFP:全球首个三维建筑轮廓数据集

WW、forever的博客 1691

PAT A1053 Path of Equal Weight (30分) (DFS + 剪枝)

PAT甲级:A1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of...

只想用最简短的代码刷题 300

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

PAT甲级 A1053

PAT甲级 A1053 解题思路 理论上这道题用深搜加上排序输出即可实现,但是在测试过程中,测试点3一直过不去(待更新) 代码: #include<iostream> #include<string> #include<vector> #include<cmath> #include<memory.h> #include<string> #include<algorithm> #include<iomanip>

weixin_44065549的博客 267

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【算法笔记】树的遍历 |【PAT A1053】Path of Equal Weight

【算法笔记】树的遍历树的静态写法树的先根遍历树的层序遍历从树的遍历看DFS与BFS【PAT A1053】Path of Equal Weight思路代码 树的静态写法 在二叉树中,由数据域和指针域组成,左指针指向左子树根结点的地址,右指针指向右子树根结点的地址。对一棵一般意义的树来说,仍然可以保留数据域,而令指针域存放其所有子结点的地址(或者为其开一个数组,存放所有子结点的地址)。但还是建议使用静态方法,用数组下标存放地址。需要事先开一个大小不低于结点上限个数的结点数组: struct node{ typ

null 655

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

PAT 1053

题目 PAT 1053 思路 树的DFS,另外本题需要利用排序函数cmp 代码 #include <algorithm> #include <iostream> #include <vector> using namespace std; const int maxn = 110; //定义树结构 typedef int myData; struct node ...

Do Better 226

PAT(Advanced)1053 Path of Equal Weight C++实现

PAT(Advanced)甲级1053 Path of Equal Weight C++实现 题目链接 1053 Path of Equal Weight 题目大意 给定一棵有N个节点和M个非叶子节点且所有节点都带正整数权值的树以及一个给定权值S,求解从根节点到叶子结点路径累积权值为S的所有路径,并按照非递增序列输出 算法思路 根据树的性质以及树的层次遍历定义,求解路径累积权值为给定值的序列,类似于图的DFS,即树的先序遍历,从根节点递归到叶子节点,若未到达叶子节点但累积权值已经大于S,则往下的路径无序继续

qq_44486439的博客 337

[PAT A1053]Path of Equal Weight

[PAT A1053]Path of Equal Weight 题目描述 Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weight of a path from R to L is defined to be the sum of the weig...

欢迎来到我的编程小屋 589

PAT A 1053 Path of Equal Weight

摘要:题目要求在一棵带权树中找出所有从根节点到叶子节点的路径,使得路径节点权重之和等于给定值S。采用深度优先搜索(DFS)遍历树,当到达叶子节点且权重和等于S时输出路径。为满足非递增顺序输出,将子节点按权重降序排序。测试发现代码能通过大部分用例,但存在一个特例未处理(如当不存在满足路径时,题目未明确输出要求)。需进一步检查边界条件,如权重和为0或负值的情况。

whdehcy的博客 796

PAT A1053 Path of Equal Weight

一、题目大意 PAT A1053 给出一棵带权树,输出权值之和等于给定值 s 的所有带权路径。如有多条路径,按权值非递增的顺序输出各条路径。 二、解题思路 使用数组静态实现树,结点编号可以用数组下标代替,因此结点只需要存储权值、孩子的编号数组。 读入树时对每个结点的孩子按权值进行排序。 DFS 递归找每一条带权路径,判断输出即可。参考代码如下。 三、参考代码 #include&lt;...

Armstrong的博客 189

PAT A 1053(甲级)

1053 Path of Equal Weight(30 分) 作者: CHEN, Yue 单位: 浙江大学 时间限制: 400 ms 内存限制: 64 MB 代码长度限制: 16 KB Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig...

CrazyOnes的博客 627

第一天PAT-A1053 Path of Equal Weight

A1053 description: 给一个非空树,其根为Ri,以及对于每个叶子节点Ti的权重Wi。一条由R到L的路径的权重为在该路上的所有叶子节点的权重之和。 现给定一个带权重的树,求所有权重为给定数字的路线。例如下图:对于每个节点,上方数字是其节点ID(2位数字),下方数字是该节点的权重。假设给定的数字是24,则存在4条不同的路线符合题意, {10 5 2 7},{10 4 10},{10 3 3 6 2} 和 {10 3 3 6 2},对应图中红色路径。 Input: 每个测试一个样例 第一行:一棵

giggle66的博客 159

PAT1053

1 #include<iostream> 2 #include<vector> 3 #include<stack> 4 #include<algorithm> 5 #include<iterator> 6 using namespace std; 7 8 #define MAX...

afsvl68886的专栏 104

PAT Advanced1053 Path of Equal Weight(DFS,树)

链接:PAT Advanced1053 Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes...

Plus Ultra! 203

PAT甲级 1053 等重路径

原题链接 给定一个非空的树,树根为 R。 树中每个节点 Ti 的权重为 Wi。 从 R 到 L 的路径权重定义为从根节点 R 到任何叶节点 L 的路径中包含的所有节点的权重之和。 现在给定一个加权树以及一个给定权重数字,请你找出树中所有的权重等于该数字的路径(必须从根节点到叶节点)。 例如,我们考虑下图的树,对于每个节点,上方的数字是节点 ID,它是两位数字,而下方的数字是该节点的权重。 假设给定数为 24,则存在 4 个具有相同给定权重的不同路径:{10 5 2 7},{10 4 10},{

赵小轩的博客 1856
上一篇: 树的遍历
下一篇: 二叉查找树的基本操作
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值