问题描述:题目链接
思路:暂略
CODE ⇩⇩⇩
#include <bits/stdc++.h>
using namespace std;
int w,weight[100],marked[100],parent[100];
map<int,vector<int>> nonLeafNode;
auto cmp = [](int a,int b){return weight[a] > weight[b];};
auto isLeaf =[](int node){return nonLeafNode.find(node) == nonLeafNode.end();};
void dfs(int node,int sum){
marked[node] = 1;
sum += weight[node];
if((sum < w && isLeaf(node)) || sum > w) return;
if(sum == w && isLeaf(node)){
stack<int> s;
for(int i = node;i != 0;i = parent[i]) s.push(i);
cout<<weight[0];
while(!s.empty()) cout<<" "<<weight[s.top()],s.pop();
cout<<endl;
return;
}
for(auto it : nonLeafNode[node]){
if(!marked[it]) dfs(it,sum);
}
marked[node] = 0;
}
int main() {
int n,m,node,cnt;
cin>>n>>m>>w;
for(int i = 0;i < n;i++) cin>>weight[i];
while(m--){
cin>>node>>cnt;
vector<int> vc(cnt);
for(int i = 0;i < cnt;i++) {
cin>>vc[i];
parent[vc[i]] = node;
}
sort(vc.begin(),vc.end(),cmp);
nonLeafNode[node] = vc;
}
dfs(0,0);
return 0;
}
本文深入探讨了一种基于权重的树形结构遍历算法,通过递归深度优先搜索实现子树权重累加,旨在寻找所有满足特定权重条件的路径。文章详细介绍了算法的核心思路,包括节点标记、权重累积及回溯机制,并提供了完整的C++代码实现。
355

被折叠的 条评论
为什么被折叠?



