#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define swap(a, b){vector<int> tmp = a; a = b; b = tmp;}
using namespace std;
struct node {
int weight;
vector<int> children;
};
void dfs(node *tree, int index,int sum);
bool comp(vector<int> &v1, vector<int> &v2);
void bubblesort(void);
vector<vector<int>> paths;
vector<int> path;
int s;
int main(void) {
int n, m, i, id, k, j, tmp;
node *tree = new node[100];
scanf("%d %d %d", &n, &m, &s);
for (i = 0; i < n; i++) {
scanf("%d", &tree[i].weight);
}
for (i = 0; i < m; i++) {
scanf("%d %d", &id, &k);
for (j = 0; j < k; j++) {
scanf("%d", &tmp);
tree[id].children.push_back(tmp);
}
}
dfs(tree, 0, 0);
bubblesort();
for (i = 0; i < paths.size(); i++) {
printf("%d", paths[i][0]);
for (j = 1; j < paths[i].size(); j++) {
printf(" %d", paths[i][j]);
}
putchar('\n');
}
delete[] tree;
return 0;
}
void dfs(node *tree, int index, int sum) {
sum += tree[index].weight;
path.push_back(tree[index].weight);
int i;
if (!tree[index].children.size()) {
if (s == sum) {
paths.push_back(path);
}
}
else {
for (i = 0; i < tree[index].children.size(); i++) {
dfs(tree, tree[index].children[i], sum);
}
}
path.pop_back();
return;
}
bool comp(vector<int> &v1, vector<int> &v2) {
int i;
for (i = 0; i < v1.size() && i < v2.size(); i++) {
if (v1[i] < v2[i]) {
return false;
}
else if(v1[i] > v2[i]) {
return true;
}
}
return true;
}
void bubblesort(void) {
int i, size = paths.size();
bool flag;
while (size > 0) {
flag = true;
for (i = 0; i < size - 1; i++) {
if (!comp(paths[i], paths[i + 1])) {
swap(paths[i], paths[i + 1]);
flag = false;
}
}
if (flag) {
break;
}
}
return;
}
1053. Path of Equal Weight (30)解题报告
最新推荐文章于 2022-09-04 08:24:31 发布