#include<stdio.h>
#include<vector>
#include<queue> #include<algorithm> using namespace std; const int MAX=1010; int n,m; //n个节点,m个非叶子节点 long long int S; //待测权值 long long int weight[MAX]; //每个节点的权值 vector<int> child[MAX]; //存储节点 bool cmp(int a,int b) { return weight[a]>weight[b]; } long long int sum=0; vector<int> path; //存路径权值 void DFS(int root) { if(child[root].size()==0) { if(sum==S) { printf("%d",path.front()); //权值S不为0,符合条件一定有路径。单独输出首个,控制后面空格数 for(int i=1 ; i<path.size() ; ++i) printf(" %d",path[i]); //输出剩余路径 printf("\n"); } return; } for(int i=0 ; i<child[root].size() ; ++i) { sum+=weight[child[root][i]]; //加上该孩子的权值 //printf("root=%d, i=%d, sum=%lld, child[root][i]=%d\n", root, i, sum, child[root][i]); path.push_back(weight[child[root][i]]); //该孩子加入到路径 DFS(child[root][i]); sum-=path[path.size()-1]; path.pop_back(); } } int main() { scanf("%d%d%lld",&n,&m,&S); for(int i=0 ; i<n ; ++i) scanf("%lld",&weight[i]); for(int i=0 ; i<m ; ++i) { int father,k,c; scanf("%d%d",&father,&k); for(int j=0 ; j<k ; ++j) { scanf("%d",&c); child[father].push_back(c); } sort(child[father].begin(),child[father].end(),cmp); //【skill】对vector进行排序,保证遍历的时候从大到小 } path.push_back(weight[0]); sum+=weight[0]; DFS(0); return 0; }

转载于:https://www.cnblogs.com/Evence/p/4321869.html

PAT:1053. Path of Equal Weight (30) AC相关推荐

  1. PAT甲级1053 Path of Equal Weight (30分) :[C++题解]dfs求树的路径长度、邻接表

    文章目录 题目分析 题目链接 题目分析 输入样例: 20 9 24 10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2 00 4 01 02 03 04 02 1 ...

  2. PAT甲级 -- 1053 Path of Equal Weight (30 分)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  3. PAT (Advanced Level) Practice 1053 Path of Equal Weight (30 分)

    1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigned to ...

  4. 1053 Path of Equal Weight (30分)

    1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  5. 1053 Path of Equal Weight

    1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned ...

  6. 1053 Path of Equal Weight (30 分)

    题目 Given a non-empty tree with root R, and with weight WiW_iWi​ assigned to each tree node TiT_iTi​. ...

  7. 【PAT】A1053 Path of Equal Weight

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  8. 1053 Path of Equal Weight(超级无敌详细注释+45行代码)

    分数 30 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Given a non-empty tree with root R, and with weight Wi​ assig ...

  9. 1053 Path of Equal Weight

    1. 以下两组关系很大的概念 树的深度优先搜索 - 先根遍历 - 递归 树的广度优先搜索 - 层序遍历 - 非递归 本题考察的是前者,我设置了这样一个结构体 struct Prestruct{int ...

最新文章

  1. php 公共的 受保护的,php-不使用$this-gt;调用受保护的属性
  2. ASP.NET MVC 4 中的JSON数据交互总结
  3. 阻塞、非阻塞与同步、异步的区别
  4. 安装虚拟机和docker封装
  5. 如何把荣耀手机的计算机移动到桌面,华为荣耀怎么把天气预报设置到桌面
  6. mysql date类型加一个月jdbc_JDBC操作数据库Date类型数据
  7. [Objective-c 基础 - 1.3] OC带返回值的类方法
  8. 【Python】Matplotlib绘制七彩锥面
  9. Intel Sandy Bridge/Ivy Bridge架构/微架构/流水线 (8) - 流水线前端/分支预测
  10. python telnet server_python工具库介绍-dubbo:通过telnet接口访问dubbo服务
  11. python:filter用法
  12. 3.SOA架构:服务和微服务分析及设计--- 理解服务与微服务的层次
  13. 算法面试_怎么简单高效的理解递归?//TODO
  14. sourceTree把当前分支合并到远程分支
  15. Laravel 实现任务调度功能
  16. boost升压电路解析
  17. 计算机环模实验报告,误差配套实验报告
  18. python利用reportlab打印图文并茂内容
  19. 无cmd窗口运行jar包的方法
  20. C++笔试笔记1(4399 西山居 深信服 剑心互娱 快手)

热门文章

  1. 命令行mvn打包的时候报错:No compiler is provided in this environment. Perhaps you are running on a JRE
  2. 【迁移学习(Transfer L)全面指南】元学习
  3. 【深度学习】Dropout与学习率衰减
  4. 深度学习生态圈【详解深度学习工具Keras】
  5. 机器学习(MACHINE LEARNING)多属性决策模型
  6. python【数据结构与算法】 python3 deque模块(双端队列)
  7. js移除某个样式_js中如何移除css样式?
  8. java读取欧姆龙plc_欧姆龙CJ2M系列PLC与PLC之间的数据相互读取设定
  9. 实现点击空白关闭模态框
  10. sqluldr2支持mysql吗_Oracle SQLULDR2 以及 SQLLDR 进行导入导出的功能说明