1053 Path of Equal Weight

25 篇文章 0 订阅
6 篇文章 0 订阅

按照孩子的权值来进行排序,学到了

#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct BTnode {
	int weight;
	vector<int> child;
}node[maxn];
int path[maxn];
int n, m, s;
bool cmp(int a, int b) {
	return node[a].weight > node[b].weight;
}
void DFS(int index, int numNode, int sum) {
	if (sum > s)	return;
	if (sum == s) {
		if (node[index].child.size() != 0)	return;
		for (int j = 0; j < numNode; j++) {
			printf("%d", node[path[j]].weight);
			if (j < 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 parent, k, tmp;
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &parent, &k);
		for (int j = 0; j < k; j++) {
			scanf("%d", &tmp);
			node[parent].child.push_back(tmp);
		}
		sort(node[parent].child.begin(), node[parent].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、付费专栏及课程。

余额充值