1053. Path of Equal Weight (30)
来源:互联网 发布:九九口诀乘法表vb编程 编辑:程序博客网 时间:2024/06/07 11:59
用deque保存各个节点的父节点,然后对每个叶子节点自下网上遍历到root,如果路径权值和等于S,保存到结果集中。最后对结果集排序输出
#include<iostream>#include<deque>#include<algorithm>#pragma warning(disable:4996)using namespace std;bool compare(deque<int> a, deque<int> b){ int i = a.size() > b.size() ? b.size() : a.size(); for (int t = 0;t < i;t++) if (a[t]>b[t]) return true; else if (a[t] < b[t]) return false; else continue; return false;}deque<deque<int>> resault;//存储要输出的结果int a[100];//存储权值int N, M, S;deque<int> node[100];//存储各节点的父节点int l[100] = { 0 };//存储是否为叶节点int main(){ cin >> N >> M >> S; for (int t = 0;t < N;t++) scanf("%d",a+t); for (int t = 0;t < M;t++)//输入数据 { int temp_n, temp; scanf("%d %d", &temp, &temp_n); l[temp] = 1; for (int i = 0;i < temp_n;i++) { int temp_l; scanf("%d", &temp_l); node[temp_l].push_back(temp); } } for (int t = 0;t < N;t++)//对各个叶子节点进行自下往上的遍历,如果等于S保存路径 { if (l[t] == 0) { deque<int> temp_p; int temp = t; int length=0; while (temp != 0) { length += a[temp]; temp_p.push_front(a[temp]); temp = node[temp].back(); } length += a[0]; temp_p.push_front(a[0]); if (length == S) resault.push_back(temp_p); } } sort(resault.begin(), resault.end(), compare);//对结果按要求排序 for (auto x : resault) { int temp = 0; for(auto y:x) if (temp == 0) { printf("%d", y);temp = 1; } else printf(" %d", y); printf("\n"); }}
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)
- 解析Linux下的stat命令
- 糟糕的畢業季
- Dynamics 365 支持使用Web Api 通过名称来检索元数据
- Linux最常用的20条命令
- 安装2502,2503错误拯救
- 1053. Path of Equal Weight (30)
- 需求获取的方法
- 单例模式
- 网页解析器和beautiful soup 实例测试
- salesforce 零基础学习(六十四)页面初始化时实现DML操作
- 串口
- QtCreator2.8.0+QtOpenSource4.8.5环境配置(初学者必看)
- Linux平台下的Eclipse配置
- 搜索技术架构