题意:
输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重,接下来输入M行,每行包括一个两位数字组成的数代表非叶子结点的编号以及数字x表示它的孩子结点个数,接着输入x个数字表示孩子结点的编号。以非递增序输出从根到叶子结点的路径权重,它们的和等于S。
AAAAAccepted code:
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int w[107]; 5 vector<int>v[107]; 6 int s; 7 int now; 8 vector<int>path; 9 int flag; 10 int vis[107]; 11 vector<int>ans[107]; 12 int cnt; 13 int dfs(int x){ 14 path.push_back(w[x]); 15 now+=w[x]; 16 if(now==s){ 17 if(!vis[x]) 18 ans[++cnt]=path; 19 path.pop_back(); 20 now-=w[x]; 21 return 0; 22 } 23 else if(now>s){ 24 path.pop_back(); 25 now-=w[x]; 26 return 0; 27 } 28 else 29 for(auto it:v[x]) 30 dfs(it); 31 path.pop_back(); 32 now-=w[x]; 33 return 0; 34 } 35 int main(){ 36 ios::sync_with_stdio(false); 37 cin.tie(NULL); 38 cout.tie(NULL); 39 int n,m; 40 cin>>n>>m>>s; 41 for(int i=0;i<n;++i) 42 cin>>w[i]; 43 for(int i=1;i<=m;++i){ 44 string fa; 45 cin>>fa; 46 int f=(fa[0]-'0')*10+fa[1]-'0'; 47 vis[f]=1; 48 int x; 49 cin>>x; 50 string son; 51 for(int j=1;j<=x;++j){ 52 cin>>son; 53 int s=(son[0]-'0')*10+son[1]-'0'; 54 v[f].push_back(s); 55 } 56 } 57 dfs(0); 58 sort(ans+1,ans+1+cnt); 59 for(int i=cnt;i;--i){ 60 if(i<cnt) 61 cout<<" "; 62 cout<<ans[i][0]; 63 for(int j=1;j<ans[i].size();++j) 64 cout<<" "<<ans[i][j]; 65 } 66 return 0; 67 }