pat 1053 Path of Equal Weight 树的路径求和,数组排序,father[]用法
来源:互联网 发布:企业网络需求分析报告 编辑:程序博客网 时间:2024/06/05 11:32
题意:
给出一个树的结构,每个节点有个权值,求从根到叶子节点, 总和为s的所有路径。
思路:
直接用father来存储树结构了, 从每个叶子节点不断向上回溯到根节点,即可找到所有和为s路径。
这样得到的是反的序列,故用 reverse(pathv.begin(),pathv.end()); 进行vector翻转。
确定输出顺序时,要进行sort,cmp函数如下:
bool cmp(vector<int> &v1, vector<int> &v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致 { int i,j;int n1=v1.size(),n2=v2.size();int n = n1<n2?n1:n2;for(i=0;i<n;i++){if(v1[i]>v2[i])return true;else if(v1[i]<v2[i])return false;}if(n1>n2 )return true;else return false;}
代码如下:
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<stack>#include<vector>#include<queue>#include<string>#include<map>using namespace std;#define INF 99999999#define M 1000bool cmp(vector<int> &v1, vector<int> &v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致 { int i,j;int n1=v1.size(),n2=v2.size();int n = n1<n2?n1:n2;for(i=0;i<n;i++){if(v1[i]>v2[i])return true;else if(v1[i]<v2[i])return false;}if(n1>n2 )return true;else return false;} //start 16:28//stop 17:25int main(){int nonleaf[200],w[200],father[200];int i,j,cid,m,n,s,id,cn;memset(nonleaf,0,sizeof(nonleaf));scanf("%d%d%d",&n,&m,&s);for(i=0;i<n;i++)scanf("%d",&w[i]);for(i=0;i<m;i++){scanf("%d%d",&id,&cn);nonleaf[id]=1;for(j=0;j<cn;j++){scanf("%d",&cid);father[cid] = id;}}father[0] = -1;int sum=0;int p;vector<int> pathv;vector< vector<int> > ansv;for(i=0;i<n;i++){if(!nonleaf[i]){pathv.clear();p=i;sum=0;while(p != -1){sum += w[p];pathv.push_back(w[p]);p = father[p];}if(sum==s){reverse(pathv.begin(),pathv.end());ansv.push_back(pathv);}}}std::sort(ansv.begin(),ansv.end(),cmp);for(i=0;i<ansv.size();i++){for(j=0;j<ansv[i].size();j++){printf("%d",ansv[i][j]);if(j!=ansv[i].size()-1)cout<<" ";else cout<<endl;}}}
阅读全文
0 0
- pat 1053 Path of Equal Weight 树的路径求和,数组排序,father[]用法
- PAT 1053 Path of Equal Weight DFS
- PAT 1053 Path of Equal Weight
- 浙大PAT 1053题 1053. Path of Equal Weight
- 【PAT 1053】 Path of Equal Weight 深度优先搜索
- Pat(Advanced Level)Practice--1053(Path of Equal Weight)
- PAT (Advanced Level) Practise 1053 Path of Equal Weight (30)
- PAT (Advanced Level) Practise 1053 Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)-PAT
- PAT 1053. Path of Equal Weight
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 【PAT】1053. Path of Equal Weight (30)
- PAT 1042Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight
- PAT A1053. Path of Equal Weight (30)
- select()和poll()的区别是什么?
- 第一行代码 第二章 Activity的生命周期
- Android:进程间通信AIDL和广播的效率
- 用Gson解析纯json数组的方法总结
- Redis 与列表(List)相关的命令
- pat 1053 Path of Equal Weight 树的路径求和,数组排序,father[]用法
- 单片机移植LUA
- C/C++:拷贝文件
- AndroidStudio中默认不导入org.apache.http等包的解决方法
- 卡尔曼滤波器-Kalman Filter
- Ubuntu16.04安装ROS操作系统
- 编程规范——Java注释样式规范与模板
- Service基本用法
- 第一章 “开始”学习笔记(第一遍阅读)