PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. 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 Wi (<) corresponds to the tree node Ti. 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 Ai=Bi for ,, and Ak+1>Bk+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内元素排序,有一小坑点)的更多相关文章
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- 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 ...
- 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 weig ...
- 【PAT】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 t ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲级——A1053 Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- 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 ...
随机推荐
- .NET Core、EF、Dapper、MySQL 多种方式实现数据库操作(动态注册实体类)
目录 前言 一.技术选型 二.遇到的坑 2.1..NET Core 下 EF 的问题 2.2.数据库实体类的注册 切记坑 前言 最近在学习.研究 .NET Core 方面的知识,动手搭建了一些小的 D ...
- 文件操作中file.seek()方法
摘要: file.seek()可以将文件游标移动到文件的任意位置,本文具体的file.seek()文件游标移动操作方法. file.seek()方法标准格式是:seek(offset,whence=0 ...
- (C99)复合字面量
#include <stdio.h> struct argvs { char a[64]; int b; }; int pfunc(struct argvs *a) { printf(&q ...
- glog的安装使用
参考 :https://blog.csdn.net/Pig_Pig_Bang/article/details/81632962 https://blog.csdn.net/cywtd/article/ ...
- 008_硬件基础电路_RC消火花电路分析方法和思路
如上图所示是一种RC消火花电路.电路中,+V是直流工作电压,S1是电源开关,M是直流电机,R1和C1构成RC消火花电路. 1.电路分析需要了解火花产生的原因直流电机的内部是一个线圈结构,根据线圈的有关 ...
- 关于ORA-12154: TNS:could not resolve the connect identifier specified的问题
问题:net configuration assistant测试连接可以连接.但是PLSQL连接不了,报ORA-12154: TNS:could not resolve the connect ide ...
- 使用Python+selenium实现第一个自动化测试脚本
原blog 一,安装Python. python官方下载地址:https://www.python.org/downloads/ 安装后点击开始菜单,在菜单最上面能找到IDLE. IDLE是pytho ...
- Linux分区格式化
格式化(format)是指对磁盘或磁盘中的分区(partition)进行初始化的一种操作,这种操作通常会导致现有的磁盘或分区中所有的文件被清除.格式化通常分为低级格式化和高级格式化.如果没有特别指明, ...
- 原生table表格的使用
近期公司让我修改一些之前的table标签写的页面,感觉对table相关的标签不是太熟悉,于是专门整理一下: 1.如果给td标签设置百分比宽度,比如有10列内容,我们却设置了每个单元格是30%的宽度,会 ...
- 使用tmate快速分享你的终端会话
tmate 的意思是 teammates,它是 tmux 的一个分支,并且使用相同的配置信息(例如快捷键配置,配色方案等).它是一个终端多路复用器,同时具有即时分享终端的能力.它允许在单个屏幕中创建并 ...