1053 Path of Equal Weight (30 分)(树的遍历)
题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径
#include<bits/stdc++.h> using namespace std;
int n,m,sum;
const int N=;
struct node
{
int w;
vector<int>p;
}tree[N];
bool cmp(int a,int b)
{
return tree[a].w>tree[b].w;
}
vector<int>path;
void dfs(int s,int v)
{
//cout<<s<<" "<<v<<endl;
if(s>sum) return;
if(s==sum){
if(tree[v].p.size()!=) return;
path.push_back(v);
for(int i=;i<path.size();i++){
if(i) printf(" ");
printf("%d",tree[path[i]].w);
}
printf("\n");
path.pop_back();
}
path.push_back(v);
for(int i=;i<tree[v].p.size();i++){
dfs(s+tree[tree[v].p[i]].w,tree[v].p[i]);
}
path.pop_back();
}
int main()
{
scanf("%d %d %d",&n,&m,&sum);
for(int i=;i<n;i++) scanf("%d",&tree[i].w);
for(int i=;i<m;i++){
int id;
int k;
scanf("%d %d",&id,&k);
for(int j=;j<k;j++){
int x;
scanf("%d",&x);
tree[id].p.push_back(x);
}
sort(tree[id].p.begin(),tree[id].p.end(),cmp);
}
dfs(tree[].w,);
return ;
}
1053 Path of Equal Weight (30 分)(树的遍历)的更多相关文章
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- 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 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- 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 weight of ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
随机推荐
- 一、hadoop 及 hadoop的环境搭建
一.Hadoop引言 Hadoop是在2006年雅虎从Nutch(给予Java爬虫框架)工程中剥离一套分布式的解决方案.该方案参考了Goggle的GFS(Google File System)和Map ...
- 基于java的简易计算器实现
方法: 1.将string类型的表达式输入转换成后缀表达式 2.计算后缀表达式 步骤一:将string类型的表达式输入转换成后缀表达式 输入字符串表达式,并将表达式转换成char型数组 String ...
- 有了SSL证书,如何在IIS环境下部署https?
昨天各位小伙伴都很开心的领取了自己的SSL证书,但是大部分小伙伴却不知道如何部署,也许是因为第一次接触SSL这种高端的东西吧,不过个人觉得就是懒懒懒...本来小编也挺懒的,但是答应了各位小伙伴的,那么 ...
- Spring知识点小结(四)
一.JdbcTemplate(jdbc模版--抽取的工具) web阶段DBUtils: QueryRunner runner = new QueryRunner(dataSource); ...
- Dubbo 改造普通单体项目
一.新建普通maven项目 1.首先,新建3个普通maven商城项目,模拟以往常见的Java单体应用开发,mall-interface是存放接口和公共代码部分,order-service-consum ...
- 解决ajax请求(SpringMVC后台)响应415/400/405错误
解决ajax请求(SpringMVC后台)响应415/400/405错误 后端代码 bean public class user { private String username; private ...
- 第3章 jQuery中的DOM操作
parent() .parents().closest() 区别示例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...
- opencv3 学习三 - 图像输入输出显示等
程序如下 #include "opencv2/opencv.hpp" using namespace cv; int main() { Mat file1 = imread(&qu ...
- Ubuntu Linux TinySerial串口调试助手 可视化界面 安装使用
ubuntu Linux下串口调试助手使用 Tiny Serial为一个开源项目,欢迎大家使用,基于Qt开发的串口调试助手,有一般串口助手的基本功能,更多功能正在完善. Github地址:https: ...
- centos升级数据库
Centos下升级MySQL数据库 备份数据 $ mysqldump -u xxx -h xxx -P 3306 -p --all-databases > databases.sql 查看版本 ...