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

#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. vue中使用window.open会在url前自动添加本地服务器的地址bug修复

    不能写成www.baidu.com 需要写成https://www.baidu.com

  2. Memcache随笔

    Tip01: Memcache 中记录的缓存如果你设置了失效时间,之后如果你在系统时间中将时间修改为超过失效时间,再次访问之前你设置的资源,Memcache 会认为你设置的缓存已失效: Tip02: ...

  3. Folyd + 路径存储

    一.Folyd 算法原理 如果 AB + AC < BC 那么, BC最短路就要经过 A. 在算法进行过程中,应该是 ,B-A 有很多路径,B 代表这些路径权值之和,A-C也有很多路径,C是这些 ...

  4. Codeforces Round #483 (Div. 2)C题

    C. Finite or not? time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. 单文件版本的netframework的net core 2.1

    如果你还在用net4.5,如果你还在用netframework,又想使用netcore2.1的库或者功能,又觉得nuget动不动就好大,可以试试下面的这个. https://pan.baidu.com ...

  6. DOCTYPE导致MyEclipse无法正常格式化HTML的问题

    今天遇到在JSP代码中Ctrl+F无法正常格式化HTML代码,经过排查是DOCTYPE的原因. 之前写的是: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...

  7. CentOS6的/etc/rc.local不执行的问题解决

    小编在一个场景下,希望java -jar abc.jar命令在系统开机自启动是自动执行,于是乎在文件/etc/rc.local中写入nohup java -jar abc.jar &,重启后发 ...

  8. thinkphp3.2.3 HTML 页面跳转

    1.   http://域名/index.php(入口文件)/模块/控制器/方法 2.{:U('控制器/方法')}

  9. WIN10远程连接winserver2012 r2,连接失败

    背景:2012开启远程的时候,默认是勾选“仅允许运行使用网络级别身份验证的远程桌面的计算机连接”,这个选项据说比较安全,但是用win10远程的时候就报错,函数不受支持,最后通过修改win10的配置得以 ...

  10. mysql 长连接断开问题

    从MySQL 5.0.3开始,默认情况下禁止再连接,这是5.0.13中的新选项,提供了一种以显式方式设置再连接行为的方法. mysql应用程序建立的长连接,大约过8小时会断开[没测过,网上都是这么说的 ...