码迷,mamicode.com
首页 > 其他好文 > 详细

[PAT] A1053 Path of Equal Weight

时间:2020-07-14 00:27:26      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:方案   目标   输出   解法   pac   ==   code   set   存在   

(要熟练!)(树的遍历)

题目大意

(题目链接)https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512
题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径。

思路

算法笔记P307解法存在一个问题:一开始在输入时就对一个结点下的所有子节点按照权重排序,则如果子节点权重相等的话,分不清谁大谁小。
解决方案:路径全部保存下来,最后再排序。

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <map>
#include<algorithm>
using namespace std;
struct node {
	int weight;
	vector<int> child;//孩子的下标
}point[100];
int n, m, s;
int ans[100];
bool cmp(int a, int b) {
	return point[a].weight > point[b].weight;
}
void DFS(int p, int sum, int pathlength) {
	if (sum > s)return;
	if (point[p].child.size() == 0) {
		if (sum == s) {
			printf("%d", point[0].weight);
			for (int i = 0;i <= pathlength - 1;i++)
				printf(" %d", point[ans[i]].weight);
			printf("\n");
		}
	}
	else {
		for (int i = 0;i < point[p].child.size();i++) {
			ans[pathlength] = point[p].child[i];
			DFS(point[p].child[i], sum + point[point[p].child[i]].weight, pathlength + 1);
		}
	}
}
int main() {
	int i, j;
	scanf("%d%d%d", &n, &m, &s);
	for (i = 0;i < n;i++)
		scanf("%d", &point[i].weight);
	for (i = 0;i < m;i++) {
		int father;
		scanf("%d", &father);
		int numchild, haizi;
		scanf("%d", &numchild);
		for (j = 0;j < numchild;j++) {
			scanf("%d", &haizi);
			point[father].child.push_back(haizi);
		}
		sort(point[father].child.begin(), point[father].child.end(), cmp);
	}
	DFS(0, point[0].weight, 0);
	return 0;
}

正确代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstdio>
#include <vector>
#include <map>
#include<algorithm>
using namespace std;
struct node {
	int weight;
	vector<int> child;//孩子的下标
}point[100];
int n, m, s;
int path[100];
vector<vector<int>>ans;
bool cmp(vector<int> a, vector<int> b) {
	return a > b;
}
void DFS(int p, int sum, int pathlength) {
	if (sum > s)return;
	if (point[p].child.size() == 0) {
		if (sum == s) {
			vector<int>tpath;
			for (int i = 0;i < pathlength;i++)
				tpath.push_back(point[path[i]].weight);
			ans.push_back(tpath);
		}
	}
	else {
		for (int i = 0;i < point[p].child.size();i++) {
			path[pathlength] = point[p].child[i];
			DFS(point[p].child[i], sum + point[point[p].child[i]].weight, pathlength + 1);
		}
	}
}
int main() {
	int i, j;
	scanf("%d%d%d", &n, &m, &s);
	for (i = 0;i < n;i++)
		scanf("%d", &point[i].weight);
	for (i = 0;i < m;i++) {
		int father;
		scanf("%d", &father);
		int numchild, haizi;
		scanf("%d", &numchild);
		for (j = 0;j < numchild;j++) {
			scanf("%d", &haizi);
			point[father].child.push_back(haizi);
		}
		//sort(point[father].child.begin(), point[father].child.end(), cmp);
	}
	DFS(0, point[0].weight, 0);
	sort(ans.begin(), ans.end(), cmp);

	for (i = 0;i < ans.size();i++)
	{
		printf("%d", point[0].weight);
		for (j = 0; j < ans[i].size(); j++)
			printf(" %d", ans[i][j]);
		printf("\n");
	}
	return 0;
}

[PAT] A1053 Path of Equal Weight

标签:方案   目标   输出   解法   pac   ==   code   set   存在   

原文地址:https://www.cnblogs.com/yue36/p/13296626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!