复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57#include<iostream> #include<algorithm> #include<vector> using namespace std; struct node{ int weight,num_of_children=0; vector<int>children; }; int N,M,S;//N节点数,M非叶节点,S指定权重和 node n[100]; vector<int>path; vector<vector<int>>ans; bool comp(int x,int y){ return n[x].weight>n[y].weight; } void dfs(int cur, int curweight){ path.emplace_back(n[cur].weight); if(!n[cur].num_of_children){//叶子结点 if(curweight==S){ ans.emplace_back(path); } } else{//非叶子节点 for(int each:n[cur].children){ dfs(each,curweight+n[each].weight); } } path.pop_back(); } int main() { int i,j,k; cin>>N>>M>>S; for(i=0;i<N;i++)cin>>n[i].weight; while(M--){ cin>>i>>j; n[i].num_of_children=j; while(j--){ cin>>k; n[i].children.emplace_back(k); } }//数据输入 for(i=0;i<N;i++){ if(n[i].num_of_children){ sort(n[i].children.begin(),n[i].children.end(),comp); } }//孩子节点排序 dfs(0,n[0].weight); for(i=0;i<ans.size();i++){ for(j=0;j<ans[i].size();j++){ if(j>0)cout<<' '; cout<<ans[i][j]; } cout<<endl; } }
最后
以上就是爱笑八宝粥最近收集整理的关于1053 Path of Equal Weight的全部内容,更多相关1053内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复