?
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct TreeNode
{
int weight, K=0, child[99];
} TreeNode;
int N, M, S;
TreeNode node[100];
vector<vector<int>> ans;
vector<int> stk;
void dfs(int ID, int sum);
void dfs(int ID, int sum)
{
sum += node[ID].weight;
if (sum > S)
return;
else if (sum == S && node[ID].K == 0)
{
stk.push_back(node[ID].weight);
ans.push_back(stk);
stk.pop_back();
return;
}
for (int i = 0; i < node[ID].K; i++)
{
stk.push_back(node[ID].weight);
dfs(node[ID].child[i], sum);
stk.pop_back();
}
}
bool cmp(vector<int> a, vector<int> b)
{
for (int i = 0; i < a.size() && i < b.size(); i++)
{
if (a[i] != b[i])
return a[i] > b[i];
}
return false;
}
int main()
{
scanf("%d%d%d", &N, &M, &S);
for (int i = 0; i < N; i++)
scanf("%d", &node[i].weight);
for (int i = 0; i < M; i++)
{
int tmp;
scanf("%d", &tmp);
scanf("%d", &node[tmp].K);
for (int j = 0; j < node[tmp].K; j++)
scanf("%d", &(node[tmp].child[j]));
}
dfs(0, 0);
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++)
{
for (int j = 0; j < ans[i].size(); j++)
{
printf("%d", ans[i][j]);
if (j != ans[i].size() - 1)
printf(" ");
else
printf("\n");
}
}
return 0;
}
如果将cmp函数的默认返回值更改为true,则会出现最后一个测试用例的“段错误”
段错误参考了这篇博文
|