1053. Path of Equal Weight (30)
来源:互联网 发布:修改mysql的端口 编辑:程序博客网 时间:2024/06/18 00:50
DFS遍历,顺便记录前驱路径,注意排序问题即可。
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <stack>#include <queue>#include <algorithm>#include <set>using namespace std;struct Node{ int w; vector<int> child;};int n,m,s,cnt,pre[100];vector<Node> Tree(100);vector<int> ans[100];bool cmp (const vector<int>&a,const vector<int>& b){ for(int i=0;i<min(a.size(),b.size());i++){ if(a[i]==b[i]) continue; return a[i]>b[i]; } return false;}void dfs(int v,int sum,int deep){ sum+=Tree[v].w; pre[deep]=Tree[v].w; if(Tree[v].child.size()==0&&sum==s){ for(int i=0;i<=deep;i++) ans[cnt].push_back(pre[i]); cnt++; } for(int i=0;i<Tree[v].child.size();i++) dfs(Tree[v].child[i],sum,deep+1);}int main(){ //freopen("E://PAT/input.txt", "r", stdin); scanf("%d %d %d",&n,&m,&s); for(int i=0;i<n;i++) scanf("%d",&Tree[i].w); for(int i=0;i<m;i++){ int x,y; scanf("%d %d",&x,&y); while(y--){ int t;scanf("%d",&t); Tree[x].child.push_back(t); } } dfs(0,0,0); //参数 1当前遍历点 2当前权值 3当前遍历深度 sort(ans,ans+cnt,cmp); for(int i=0;i<cnt;i++){ for(int j=0;j<ans[i].size();j++){ if(j) printf(" "); printf("%d",ans[i][j]); } printf("\n"); } return 0;}
0 0
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)-PAT
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- FreeSWITCH 添加中文语音包
- Java编程风格学习(二)
- No.7_5 OpenCL 同步——原子操作
- u-boot的移植
- Java编程风格学习(三)
- 1053. Path of Equal Weight (30)
- MediaPlayer整理
- 添加u-boot命令
- redis-09-哨兵模式
- nginx查看配置文件是否正确 查看nginx.conf路径
- 堆中的路径
- Android知识回顾之弹幕效果的实现
- STL hash_map使用
- UVa10603_Fill(优先队列搜索)