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 weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0, the number of nodes in a tree, M (<), the number of non-leaf nodes, and 0, the given weight number. The next line contains N positive numbers where W​i​​ (<) corresponds to the tree node T​i​​. Then Mlines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence { is said to be greater than sequence { if there exists 1 such that A​i​​=B​i​​ for ,, and A​k+1​​>B​k+1​​.

Sample Input:

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 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19

Sample Output:

10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2

题意:

给一个树,根为1 ,输出从根到叶子的权值和=m的所有路径(顺序有要求)

题解:

乍一看,就是用vector存储各节点和它的子节点,然后再用个结构体队列bfs一下,后来发现路径是可以找到,但是bfs很难做到题目要求的输出顺序。

所以,改用dfs,但dfs首先要对各节点的子节点按权重从大到小排序,就需要vector内元素排序,以前没用过,sort(v.begin(), v.end(), sortFun)。dfs以一个结构体为单位,存有遍历到的节点编号、权重和和路径p。

但是测试点1一直过不了,有个小坑点,没注意到,就是根节点要特判,可能一个根节点就符合条件了,不需要再遍历了。

AC代码:

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int a[];//每个点的权重
int n,m,w;
vector<int>v[];//存储每个点的字节点编号
struct node{
int key;//当前点编号
int wei;//到当前点的权重和
queue<int>p;//记录路径
};
bool sortFun(const int &p1, const int &p2)//vector内元素排序
{
return a[p1] > a[p2];//升序排列
}
void dfs(node x){
for(int i=;i<v[x.key].size();i++){
node y;
y.key=v[x.key].at(i);
if(x.wei+a[y.key]==w&&v[y.key].size()==){//必须要保证已经走到底了
queue<int>Q=x.p;//输出
while(!Q.empty()){
cout<<Q.front()<<" ";
Q.pop();
}
cout<<a[y.key]<<endl;
}else if(x.wei+a[y.key]<w){//新的递归下去
y.wei=x.wei+a[y.key];
y.p=x.p;
y.p.push(a[y.key]);
dfs(y);
}
}
} int main(){
cin>>n>>m>>w;
for(int i=;i<n;i++) cin>>a[i];
for(int i=;i<=m;i++){
int x,k;
cin>>x>>k;
for(int j=;j<=k;j++){
int y;
cin>>y;
v[x].push_back(y);
}
//因为输出的路径权重 要按降序,所以可以在DFS之前:给每一个结点的子节点排序
sort(v[x].begin(), v[x].end(), sortFun);
}
if(a[]==w){//坑点:根节点要特判
cout<<a[]<<endl;
}else{
node x;
x.key=;
x.wei=a[];
while(!x.p.empty()) x.p.pop();
x.p.push(a[]);
dfs(x);
}
return ;
}

PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)的更多相关文章

  1. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

  3. PAT Advanced 1053 Path of Equal Weight (30) [树的遍历]

    题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight ...

  4. 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 ...

  5. 【PAT】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 ...

  6. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  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. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

    由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...

  9. 1053 Path of Equal Weight (30)(30 分)

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

随机推荐

  1. WebAPI ModelValidata(模型验证)——DataAnnotations 解析

    爱做一个新的项目,在该项目中的 WebAPI 中对数据的验证用到了 ModelValidata, 以前也没有用到过,也不是很熟悉,在查看了一些资料和代码后稍有了解,这里记录下来. 这里主要介绍的是 S ...

  2. 前端学习笔记--CSS布局--盒子模型

    1.概述: 2.盒子模型 overflow属性: border属性: 1.padding属性: margin:

  3. svn优点跟缺点?

    优点: 1.svn优点:集中式管理,管理方式在服务端配置好,客户端只需要同步提交即可,使用方便,操作简单,很容易就可以上手 2.在服务端统一控制好访问权限,利用代码的安全管理. 3.所有的代码以服务端 ...

  4. 题解 [51nod1225]余数之和

    题解 [51nod1225]余数之和 题面 解析 首先可以发现,\(a\)%\(b\)\(=a-b*\lfloor a/b \rfloor\). 而对于一段连续的\(b\)来说\(\lfloor a/ ...

  5. sql server select 1 from 的作用

    select 1 from table 语句中的1代表什么意思   在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条selec ...

  6. Python学习之--基础语法

    一.定义 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 二.Python变量的命名规则 1. 变量名只能包含字母.数字和下划线: 2. 变量名不能包含空格: 3. 不要将Py ...

  7. Mxnet框架搭建

    Mxnet框架搭建 小书匠 kindle  Mxnet是亚马逊开发的深度学习框架,和谷歌Tensorflow是同类型的框架. 1.安装Mxnet 这里只展示在线安装,源码编译安装等不演示:GPU安装与 ...

  8. 贴两个mysql优化的配置文件

    MySQL5.7以上my.cnf配置文件配置 低配置服务器配置 [client] #客户端设置 port = 3306 socket = /data/mysql/data/mysql.sock def ...

  9. TCP数据的传输过程

    建立连接后,两台主机就可以相互传输数据了.如下图所示: 上图给出了主机A分2次(分2个数据包)向主机B传递200字节的过程.首先,主机A通过1个数据包发送100个字节的数据,数据包的 Seq 号设置为 ...

  10. puppteer的使用

    官方文档:Puppeteer 今天大概介绍一下我项目用到的puppeteer操作: // 启动浏览器 const browser = await puppeteer.launch({ executab ...