由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点。

链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点,

因此建树的时候先对child按w从小到大排序,然后再add建边。

水题一个,不多说了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h> using namespace std;
const int maxn=;
int head[maxn];
int tot=;
int path[maxn]; struct Node{
int id;
int w;
bool operator<(const Node tmp)const{
return w<tmp.w;
}
}node[maxn]; struct Edge{
int to;
int next;
}edge[maxn]; void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int layer,int sum,int s){
if(sum>s)
return;
if(sum==s){
if(head[u]!=-)
return;
printf("%d",path[]);
for(int i=;i<=layer;i++){
printf(" %d",path[i]);
}
printf("\n");
return;
}
for(int k=head[u];k!=-;k=edge[k].next){
int v=edge[k].to;
path[layer+]=node[v].w;
dfs(v,layer+,sum+node[v].w,s);
}
}
int main()
{
int n,m;
int s; //顶多100个点,wi最大也只有1000,所以S的范围肯定不会超过100000,int即可
int w;
init();
scanf("%d %d %d",&n,&m,&s);
for(int i=;i<n;i++){
scanf("%d",&w);
node[i].id=i;
node[i].w=w;
}
int id,k,child;
Node tmp[maxn];
for(int i=;i<m;i++){
scanf("%d %d",&id,&k);
for(int j=;j<k;j++){
scanf("%d",&child);
tmp[j]=node[child];
}
//因为遍历的时候是从最后add的边开始,所以先将child按w从小到大排序
//这样dfs的时候就会先访问w最大的节点
sort(tmp,tmp+k);
for(int j=;j<k;j++){
add(id,tmp[j].id);
}
}
path[]=node[].w;
dfs(,,node[].w,s);
return ;
}

PAT甲题题解-1053. Path of Equal Weight (30)-dfs的更多相关文章

  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,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

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

  4. PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)

    又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...

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

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

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

  7. PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

    由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...

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

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

随机推荐

  1. November 12th, 2017 Week 46th Sunday

    I love you not for who you are, but for who I am with you. 我爱你不是因为你是谁,而是因为跟你在一起,我是谁. I enjoy the fee ...

  2. Actor模型---SwiftActors

    actor是一个无线程区别的内存访问对象:actor背后有线程支持:actor的事件处理依赖与这个线程(队列.池). actor是一种面向对象的线程(池)模型,强调对事件的响应:在iOS中相当于一种通 ...

  3. 3675: [Apio2014]序列分割

    Description 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列分割成k+1个非空的子序列.为了得到k+1个子序列,小H需要重复k次以下的步骤: 1.小H首 ...

  4. RMAN删除备份

    删除备份--DELETE命令 用于删除RMAN备份记录及相应的物理文件.当使用RMAN执行备份操作时,会在RMAN资料库(RMAN Repository)中生成RMAN备份记录,默认情况下RMAN备份 ...

  5. C++与C#互调dll的实现步骤

    这篇文章主要介绍了C++与C#互调dll的实现步骤,dll动态链接库的共享在一些大型项目中有一定的应用价值,需要的朋友可以参考下 本文实例展示了C++与C#互调dll的实现步骤,在进行大型项目共享dl ...

  6. 【Git】删除某个全局配置项

    1.查看Git所有配置 git config --list 2.删除全局配置项 (1)终端执行命令: git config --global --unset user.name (2)编辑配置文件: ...

  7. Jquery简单的placeholder效果

    Jquery简单的placeholder效果 由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考! 先看看效果吧!如下JSFiddle地址 ...

  8. css盒子模型(box-sizing)

    盒子模型 关于CSS重要的一个概念就是CSS盒子模型.它控制着页面这些元素的高度和宽度.盒子模型多少会让人产生一些困惑,尤其当涉及到高度和宽度计算的时候.真正盒子的宽度(在页面呈现出来的宽度)和高度, ...

  9. httpclient介绍与使用

    什么是httpclient HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HT ...

  10. 架构图+kubernetes 问题理解 -- kube-pproxy - endpoint

    1.详述kube-proxy原理,一个请求是如何经过层层转发落到某个pod上的整个过程.请求可能来自pod也可能来自外部. 1.1kube-proxy为集群提供service功能,相同功能的pods对 ...