PAT甲级1053、 Path of Equal Weight

server/2025/2/10 9:02:53/

题目

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

思路

本来没想写的,但是写完这个题快吃饭了,不知道干点什么,就来水一篇吧

思路很简单

先给权重后给子节点,用链表的话需要二次操作才能建树,而且涉及到了ID,放在结构体里面肯定不合适,所以考虑到结构体数组或者说静态树。 

另外说一下,总结点不过100,量级比较小,冗余的话也不要紧

然后另一个要点的话就是DFS或者说回溯算法,都差不多,去遍历这棵树寻找路径

这里的栈有点多余了,用一个vector代替的话应该会更好一点

#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{int weight;vector<int> children;
}nodes[105];
vector<vector<int>> paths;
bool cmp(vector<int> a, vector<int> b)
{int l = min(a.size(),b.size());int i;for(i=0;i<l;i++){if(a[i] != b[i])break;}return a[i] > b[i];
};
stack<int> path;
vector<int> pathWeight;
int s,weightSum = 0;
void DFS(int cur)
{path.push(cur);pathWeight.push_back(nodes[cur].weight);weightSum += nodes[cur].weight;if(nodes[cur].children.empty() && weightSum == s) paths.push_back(pathWeight);for(int i=0;i<nodes[cur].children.size();i++){DFS(nodes[cur].children[i]);}path.pop();pathWeight.pop_back();weightSum -= nodes[cur].weight;return ;
}int main()
{int n,m;cin>>n>>m>>s;for(int i=0;i<n;i++){cin>>nodes[i].weight;}for(int i=0;i<m;i++){int id,k;cin>>id>>k;for(int j=0;j<k;j++){int Id;cin>>Id;nodes[id].children.push_back(Id);}}// for(int i=0;i<n;i++)// {//     cout<<i<<" "<<nodes[i].weight<<" "<<nodes[i].children.size();//     for(int j=0;j<nodes[i].children.size();j++)//         cout<<" "<<nodes[i].children[j];//     cout<<endl;// }DFS(0);sort(paths.begin(), paths.end(),cmp);for(int i=0;i<paths.size();i++){cout<<paths[i][0];for(int j=1;j<paths[i].size();j++)cout<<" "<<paths[i][j];cout<<endl;}
}


http://www.ppmy.cn/server/166452.html

相关文章

智慧停车场解决方案(文末联系,领取整套资料,可做论文)

一、方案概述 本智慧停车场解决方案旨在通过硬件设备与软件系统的深度整合&#xff0c;实现停车场的智能化管理与服务&#xff0c;提升车主的停车体验&#xff0c;优化停车场运营效率。 二、硬件架构 硬件设备说明&#xff1a; 车牌识别摄像机&#xff1a;安装在停车场入口和…

【Pytorch函数】PyTorch随机数生成全解析 | torch.rand()家族函数使用指南

&#x1f31f; PyTorch随机数生成全解析 | torch.rand()家族函数使用指南 &#x1f31f; &#x1f4cc; 一、核心函数参数详解 PyTorch提供多种随机数生成函数&#xff08;注意&#xff1a;无直接torch.random()函数&#xff09;&#xff0c;以下是常用函数及参数&#xff1a;…

数据分析如何做EDA

探索性数据分析&#xff08;EDA&#xff0c;Exploratory Data Analysis&#xff09;是数据分析过程中至关重要的一步&#xff0c;其目的是通过统计和可视化技术对数据进行初步分析&#xff0c;从而揭示数据的潜在模式、特征和异常值&#xff0c;并为后续的数据预处理、特征工程…

【函数题】6-9 二叉树的遍历

6-9 二叉树的遍历 1 题目原文2 思路解析2.1 层序遍历2.2 先序遍历2.2.1 递归实现2.2.2 使用栈将递归转为迭代2.2.3 Morris 前序遍历 2.3 中序遍历2.3.1 递归实现2.3.2 使用栈将递归转为迭代2.3.3 Morris 中序遍历 2.4 后序遍历2.4.1 递归实现2.4.2 使用栈将递归转为迭代2.4.3 M…

【Git】ssh如何配置gitlab+github

当我们工作项目在gitlab上&#xff0c;又希望同时能更新自己个人的github项目时&#xff0c;可能因为隐私问题&#xff0c;不能使用同一′密钥。就需要在本地电脑上分别配置两次ssh。 1、分别创建ssh key 在用户主目录下&#xff0c;查询是否存在“.ssh”文件&#xff1a; 如…

BFS算法篇——广度优先搜索,探索未知的旅程(上)

文章目录 前言一、BFS的思路二、BFS的C语言实现1. 图的表示2. BFS的实现 三、代码解析四、输出结果五、总结 前言 广度优先搜索&#xff08;BFS&#xff09;是一种广泛应用于图论中的算法&#xff0c;常用于寻找最短路径、图的遍历等问题。与深度优先搜索&#xff08;DFS&…

vite + axios 代理不起作用 404 无效

vite axios 代理不起作用 先看官方示例 export default defineConfig({server: {proxy: {// 字符串简写写法/foo: http://localhost:4567,// 选项写法/api: {target: http://jsonplaceholder.typicode.com,changeOrigin: true,rewrite: (path) > path.replace(/^\/api/, )…

HTML之基本布局div|span

HTML基本布局使用 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"width<device-width>, initial-scale1.0"><title>布局</title> <…