1053. Path of Equal Weight (30)
来源:互联网 发布:如何查看淘宝客佣金 编辑:程序博客网 时间:2024/06/08 06:21
题目链接:https://www.patest.cn/contests/pat-a-practise/1053
通过map建立树图结构,采用递归调用的方法来实现
#include <cstdio>#include <vector>#include <string>#include <algorithm>#include <iostream>#include <map>#include <numeric>using namespace std;struct Node{ int id; int wt; vector<int> childrens; Node(){} Node(int _id , int _wt):id(_id), wt(_wt){}};bool cmp(const vector<int> &a, const vector<int> &b){ int t=min(a.size() , b.size()); for(int i=0;i<t;i++){ if(a[i]==b[i])continue; else return a[i]>b[i]; } return a.size() > b.size();}int givenWt;map<int, Node> tree;vector< vector<int> >output;void _search(int id, vector<int> rst){ rst.push_back(tree[id].wt); if(accumulate(rst.begin() , rst.end() , 0)==givenWt && tree[id].childrens.size()==0 ){ output.push_back(rst); return; } else { int temp=tree[id].childrens.size(); for(int i=0; i<temp;i++){ vector<int> v(rst); _search( tree[id].childrens[i] , v ); } }}int main(){ int nodesNum, nonLeaf; cin>>nodesNum>>nonLeaf>>givenWt; int wt=0; for(int i=0;i<nodesNum;i++){ cin>>wt; tree.insert( make_pair(i , Node(i , wt) ) ); } for(int i=0;i<nonLeaf;i++){ int id,num;cin>>id>>num; for(int j=0;j<num;j++){ int t;cin>>t; tree[id].childrens.push_back(t); } } vector<int> vec; _search( 0 , vec); sort(output.begin(), output.end() , cmp); int t=output.size(); for(int i=0; i<t; i++){ int tt=output[i].size(); for(int j=0;j<tt;j++){ if(j==tt-1)printf("%d",output[i][j]); else printf("%d ",output[i][j]); } if(i<t-1)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)
- LeetCode343 Integer Break
- PLSQL安装破解
- HDOJ 2566 统计硬币 (水)
- servlet写文件时发生错误
- HDU 5698:瞬间移动(排列组合)
- 1053. Path of Equal Weight (30)
- andriod——Retrofit+Fresco+MVP网络获取数据
- 通过例子学习spark dataframe -- transformations函数(1)
- mysql半同步复制
- 机房收费系统——上机操作
- 图的遍历(DFS)
- 末尾0的个数
- 人伦是否适用于AI?
- 线性分式变换(linear fractional transformation)