1053 Path of Equal Weight

news2025/7/22 9:23:56

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 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<230, the given weight number. The next line contains N positive numbers where Wi​ (<1000) corresponds to the tree node Ti​. 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 {A1​,A2​,⋯,An​} is said to be greater than sequence {B1​,B2​,⋯,Bm​} if there exists 1≤k<min{n,m} such that Ai​=Bi​ for i=1,⋯,k, and Ak+1​>Bk+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

题目大意

给你树的路径和权值,请找出找从根结点到叶⼦结点路径上的权值相加之和等于Key的
路径,并且从⼤到⼩输出路径

思路

DFS暴力遍历即可


C/C++ 

#include<bits/stdc++.h>
using namespace std;
void DFS(int now,int sum);
vector<vector<int>> result;
vector<int> child[10001],op;
int power[10001],N,M,key,a,b,c;
bool cmp(vector<int>& x,vector<int>& y){
    for(int z=0;z<min(x.size(),y.size());z++){
        if(power[x[z]]!=power[y[z]]) return power[x[z]] > power[y[z]];
    }
    return false;
}
int main()
{
    cin >> N >> M >> key;
    for(int z=0;z<N;z++) cin >> power[z];
    while (M--){
       cin >> a >> b;
        while (b--){
            cin >> c;
            child[a].push_back(c);
        }
    }
    DFS(0,power[0]);
    if(result.size()>1) sort(result.begin(),result.end(),cmp);
    for(const auto& x:result){
        cout << power[0];
        for(int y:x) cout << " " << power[y];
        putchar('\n');
    }
    return 0;
}
void DFS(int now,int sum)
{
    if(child[now].empty()){
        if(sum==key) result.push_back(op);
        return;
    }else{
        for(int x:child[now]) {
            op.push_back(x);
            DFS(x,sum+power[x]);
            op.pop_back();
        }
    }
}


 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/7730.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

QT获取计算机硬件信息

一、项目介绍 本文介绍利用QProcess获取计算机的CPU、主板、硬盘等电脑相关硬件信息。 windows提供了“wmic”&#xff08;Windows Management Instrumentation&#xff0c;Windows管理工具&#xff09;&#xff0c;提供了从命令行接口和批命令脚本执行系统管理的支持。可以打…

基于多个openEuler物理机执行mugen测试脚本

【原文链接】基于多个openEuler物理机执行mugen测试脚本 mugen脚本中有的脚本执行需要使用多个物理机&#xff0c;针对此场景&#xff0c;这里以需要两个物理机为例&#xff08;用openEuler虚拟机模拟物理机&#xff09; &#xff08;1&#xff09;首先安装两台openEuler虚拟…

【C++】C++基础知识(一)---基本概念

C基础知识&#xff08;一&#xff09;1. 输出“HelloWorld!”2. 添加注释3. 关键字4. 标识符5. 变量6. 常量1. 输出“HelloWorld!” 在visual studio中输出“HelloWorld!”。代码实现如下&#xff1a; #include <iostream> using namespace std;int main() {cout <&…

unity搭建xlua和emmy_lua的debug环境

配置步骤 1 环境 1.1 vscode 安装emmy_lua 1.2 安装对应的lua版本 1.3 安装java8并配置环境 1.4 emmy_lua的github上下载emmy_lua的64位版本&#xff0c;解压放到工程目录client\Tools\EmmyLua\ 下载地址&#xff1a;https://github.com/EmmyLua/EmmyLuaDebugger/release…

「Redis数据结构」动态字符串(SDS)

「Redis数据结构」动态字符串&#xff08;SDS&#xff09; 文章目录「Redis数据结构」动态字符串&#xff08;SDS&#xff09;[toc]一、前言二、概述三、C字符串与SDS的区别获取字符串长度复杂度杜绝缓冲区溢出减少修改字符串时的内存分配次数二进制安全兼容部分C字符串函数参考…

【PyTorch】Transforms基本使用

文章目录二、Transforms基本使用1、Transforms的结构及用法1.1 如何使用1.2 TensorBoard查看2、常用的Transforms2.1 ToTensor2.2 Normalize2.3 Resize2.4 Compose2.5 RandomCrop二、Transforms基本使用 Transforms主要是对特定格式的图片进行一些变化。 1、Transforms的结构及…

展锐UIS8310 CAT4物联网模块简介

1.简介 UIS8310是一个高度集成的应用处理器&#xff0c;支持TDD-LTE、FDD-LTE和WCDMA、GSM/GPRS/EDGE制式&#xff0c;并且支持LPDDR2。它的AP处理器是单核ARM CortexTM A7 1GHz&#xff0c;旨在为物联网提供经济高效、低功耗和高性能的解决方案。 UIS8310经过特别优化的架构可…

Servlet之RequestReponse 学习笔记

1 Request 1.1 Request继承关系 ServletRequest Java提供的请求对象根接口 HttpServletRequest Java提供的对Http协议封装的请求对象接口 RequestFacade Tomcat定义的实现类WebServlet("/demo2") public class Servlet2Demo extends HttpServlet {Overridep…

Vue 路由router的介绍以及使用方法

路由&#xff1a;简单来说类似于路由器&#xff0c;中转站。 1.理解: 一个路由&#xff08;route&#xff09;就是一组映射关系&#xff08;key - value&#xff09;&#xff0c;多个路由需要路由器&#xff08;router&#xff09;进行管理。 2.前端路由&#xff1a;key是路径&…

同源策略、跨域与JSONP

跨域与JSONP一、了解同源策略和跨域1.1 同源策略1.2 跨域二、JSONP2.1 什么是JSONP2.2 JSONP的实现原理2.3 自己实现一个简单的JSONP2.4 JSONP的缺点2.5 jQuery中的JSONP2.6 自定义参数及回调函数名称2.7 jQuery中JSONP的实现过程三、案例-淘宝搜索一、了解同源策略和跨域 1.1…

软考考完了,如何评职称?

很多考生以为软考初级、中级、高级资格考试通过了就是相应的职称了&#xff0c;然而并不是这样的&#xff0c;通过了软考考试只是相当于有了评职称的资格&#xff0c;并不是有了相应职称。 有了软考证书是可以申请评职称的&#xff0c;因为有了软考证书就有了评职称的资格哦&a…

【前端】flet:一款(即将)支持多语言开发的UI库

文章目录介绍开发生态支持语言运行体验组件API热更新开发计划 Roadmap2022 7月-8月安全手机端桌面端Controls(控件)核心功能用户指引&#xff08;User education&#xff09;2022 9月到12月手机端控件&#xff08;Controls&#xff09;编程语言支持核心功能介绍 Flet enables …

【云原生 | Kubernetes 系列】--Gitops持续交付 CD Push Pipeline实现

1. Tekton Trigger基础 Tekton Triggers简介 - 监控特定的事件,并在满足条件时自动触发Tekton PipelineTekton Triggers 为用户提供了一种声明式API 它允许用户按需定义监视的事件,并将其与特定的Pipeline连接,从而实例化出PipelineRun还允许将事件中的某些属性值信息注入到P…

使用springboot每日推送早安问候语到用户微信

本文主要实现给不同的用户推动不同的问候模板 准备工作 申请微信公众平台的测试号 申请微信测试公众号 创建成功后&#xff0c;可以看到appid和appsecret&#xff0c;这个后面认证时需要 申请模板 可自行修改 今天是&#xff1a;{{now.DATA}} 不管那一天&#xff0c;每一天…

【科学文献计量】科学文献知识网络分析基础

科学文献知识网络分析基础 1 知识网络分析基础2 知识网络图构成2.1 简单网络图绘制2.2 完整网络图绘制3 知识网络图中的术语3.1 术语和统计量概念3.2 获取术语信息实例3.3 最大子群和网络图孤立点的识别3.4 网络图节点度信息的统计1 知识网络分析基础 从分析的角度来看,社会网…

安保公司的商业计划书

安保公司的商业计划书年轻人&#xff0c;来做保安吧&#xff0c;少走二十年弯路安保公司的商业计划书安保品牌公司的商业模式真正的路反潮流&#xff0c;不上大学去创业&#xff0c;其实反而是领先的。 工厂肯定不能创业的&#xff0c;因为打工的技能和创业的技能不同。 如果…

【附源码】计算机毕业设计JAVA茶园文化交流平台演示录像2020

【附源码】计算机毕业设计JAVA茶园文化交流平台演示录像2020 目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1…

SpringBoot框架Mockito的使用

SpringBoot框架Mockito的使用 一、简介 mock测试就是在测试过程中&#xff0c;对于某些不容易构造或者不容易获取的对象&#xff0c;用一个虚拟的对象来创建以便测试的测试方法。 在具体的测试过程中&#xff0c;我们经常会碰到需要模拟数据或者接口的情况&#xff0c;因为环…

线性模型(穷举法实现)

参考视频&#xff1a;2.线性模型_哔哩哔哩_bilibili 参考视频中实现ywxywxywx 的代码&#xff0c;在加上偏置b后实现 ywxbywxbywxb 的线性模型 假设我们有这样一个线性模型&#xff1a;ywxbywxbywxb X和Y对应的数据如下 XY1.05.02.08.03.011.04.0&#xff1f; 预测值&#x…

开源知识付费APP代码分析

如今&#xff0c;传统的学校已经不能满足大众多元化的需求&#xff0c;各种教育培训机构落地生根。随着时间的推移&#xff0c;互联网与传统教育的结合也开拓了一种新的教育方式&#xff0c;这就是广为人知的知识付费。在线教育的突然崛起多半是因为疫情的“催化”&#xff0c;…