题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径

#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 分)(树的遍历)的更多相关文章

  1. 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 W​i​​ assigne ...

  2. 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甲级】1053 Path of Equal Weight (30 分)(DFS)

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

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

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

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

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

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

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

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

  9. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

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

  10. pat1053. Path of Equal Weight (30)

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

随机推荐

  1. jQuery 遍历 - children() 方法 获取指定id下子元素的值

    <a id="Aobj_2_2" class="" specid="2" specvid="2" href=&qu ...

  2. React最佳实践(1)

    React最佳实践不敢妄谈,但最差实践非知乎莫属. 旧版知乎看起来土了点,但体验流畅,起码用起来舒服. 新版知乎看起来UI现代化,技术实现上采用了React,但是可能因为知乎缺钱,请不起高水平的前端工 ...

  3. redis参数AOF参数的bug

    问题:redis 不想开启AOF了.但是还老是出现BGREWRITEAOF .(本redis版本为4.0.6) 涉及持久化参数设置如下: 排查及结果: 该redis以前开启过 AOF ,后来停止AOF ...

  4. cmd tab自动补全

  5. jQuery语法、选择器、效果等使用

    1.jQuery语法 1.1 基础语法:$(selector).action( ) 美元符号定义 jQuery 选择符(selector)“查询”和“查找” HTML 元素 jQuery 的 acti ...

  6. 登录验证码的生成Java代码

    package example7; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java. ...

  7. Git命令行和Xcode结合使用(我来告诉你这行代码谁写的)

    现在一直使用Git来管理代码,对于有强迫症的我来说,依旧选择了命令行,下面这段话可以更好的解释我为什么喜欢使用终端敲命令. There are a lot of different ways to u ...

  8. 在树莓派上用 python 做一个炫酷的天气预报

    教大家如何在树莓派上自己动手做一个天气预报.此次教程需要大家有一定的python 基础,没有也没关系,文末我会放出我已写好的代码供大家下载. 首先在开始之前 需要申请高德地图API,去高德地图官网注册 ...

  9. 谷歌浏览器修改cookie访问网页的小插件——EditsThisCookie

    cookie是服务器用来区分不同的浏览器客户端的,比如学生A和B同一时段用各自的电脑访问学校访问学校的教务系统查看成绩,登录之后,访问同一页面确出来不同的信息,并且不能查看对方的信息,这就是因为服务器 ...

  10. WebService第二天——WebService框架CXF

    一.CXF 1.什么是CXF Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承 ...