由于最后输出的路径排序是降序输出,相当于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. .后面是方法不加引号 ,后面是"名"要加引号

    .后面是方法不加引号 ,后面是"名"要加引号  

  2. sdn2017 第三次作业

    1.阅读: 阅读<图解openflow>第一二章(请自己查找相应书籍) 阅读文章:http://www.sdnlab.com/19777.html 阅读<重构网络>第一二章 2 ...

  3. Alpha冲刺报告(2/12)(麻瓜制造者)

    今日任务总结 燃尽图如下: 具体完成情况如下: 江郑: 今天:完成了商品需求的数据库的基本构建. 遇到的问题:对于php的ci框架不熟,操作原理不懂 明天:和队友进行数据库的对接 符天愉: 今天:完成 ...

  4. jdk1.7环境配置

    JDK1.7的环境配置(我的是jdk1.7,文件名写快了,忽略忽略) 官网下载自己需要的版本(ps:我这是朋友发给我的就不提供官网地址,去百度搜jdk就可以了) 下载下来除了改存放路径还有记得再jdk ...

  5. PyQt5--QToggleButton

    # -*- coding:utf-8 -*- ''' Created on Sep 20, 2018 @author: SaShuangYiBing Comment: '' import sys fr ...

  6. Windows Server 2008远程桌面端口更改方法

    win2008远程桌面端口默认是用的是3389端口,但是由于安全考虑,经常我们安装好系统后一般都会考虑把原来的3389端口更改为另外的端口.本文以改为端口为25608商品为例,讲解一下具体操作过程. ...

  7. 3.Solr4.10.3目录结构

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.整个目录结构 (1)bin:是脚本的启动目录 (2)contrib:第三方包存放的目录 (3)dist:编 ...

  8. Spring-IOC RootBeanDefinition源码分析

    GenericBeanDefinition是一站式的标准bean definition,除了具有指定类.可选的构造参数值和属性参数这些其它bean definition一样的特性外,它还具有通过par ...

  9. HTML5调用百度地图API获取当前位置并直接导航目的地的方法

    <!DOCTYPE html> <html lang="zh-cmn-Hans">     <meta charset="UTF-8&quo ...

  10. Linux命令——磁盘管理

    Linux命令--磁盘管理 命令df 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等 参数:-i,查看inodes的使用状况 参数:-h,使用合适的单位显示(推荐) 命令du 作用:查看某个目录或 ...