输出树中路径之和等于给定值的所有路径

26 篇文章 2 订阅

1. 问题描述:

题目来自于PAT A 1053 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 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<, 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

Output Specification:

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

题目的大概意思是:给定一棵树和每个节点的权值,求所有从根节点到叶子节点的路径,使得每条路径上的节点的权值之和等于给定的常数S,如果存在多条这样的路径那么按照路径非递增的顺序输出

第一行给出的是节点的总个数,非叶子节点个数,给定的常数S

第二行给出的所有节点的权值

第三行到最后一行给出的是每个非叶子节点以及他们的孩子节点

 

2. 思路分析如下:

① 首先题目给出的是一棵树,而不是二叉树,所有根节点可能存在着多个孩子节点,所以这个时候不能够在结构体中只使用两个指针域来确定孩子节点,一个解决的办法是声明一个整型数组,使用数组来存放多个孩子节点,但是孩子节点的个数不太好确定,只能够声明比较大的数目,另外一个解决办法是使用vector来存储孩子节点,这样就不用管当前节点的孩子节点的个数了,因为vector的长度是根据情况进行动态变化的

② 这道题目的实质上是树的遍历,所以需要选择一种遍历的方式,下面选择的是深度优先遍历

③ 接下来就是路径的存储问题,可以有两种解决办法:

1)使用一个整型数组来进行存储,path[i]表示第i个节点的编号(i从0开始),然后使用初值为0的numNode作为下标,在递归的过程中每向下递归一层那么numNode就加1,这样最终我们确定了一条路径之后就知道当前的路径的节点个数是多少,而且假如存在着多条路径的时候,经过dfs的回溯那么会将之前位置的值给覆盖掉变为当前路径的节点的值

2)使用C++标准模板库中的vector来进行存储,每递归一次我们就加入当前的节点,等到当前层的递归结束之后那么我们应该进行回溯,在调用这个递归方法之后将之前加入的节点删除掉,这样在使用for循环往下递归的时候结果才是正确的,可以使用push_back与pop_back函数进行节点的加入和删除

④ 当递归的是发现当前路径的值已经大于了给定的S,直接返回即可,当等于了S的值之后那么我们需要判断当前节点是否是叶子节点加入不是直接return,假如是的话那么输出存放路径的数组的每一个秩即可

⑤ 因为可能存在着多条路径的,所以我们需要在输入数据的时候,对输入的孩子节点的值进行排序

3. 下面是来自于算法笔记中提供的C++代码,感觉写得挺好的,我们可以阅读源码来提高自己的编程能力:

#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上的节点个数
void dfs(int index, int numNode, int sum){
	if(sum > S) return;
	if(sum == S){
		//判断是否是叶子节点假如不是那么表示不符合路径直接return 
		if(Node[index].child.size() != 0) return;
		//到达叶子节点此时path存放了一条完整的路径
		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(void){
	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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值