由于最后输出的路径排序是降序输出,相当于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. MySQL报错:error1130

    ERROR (HY000): Host 'ip-172-31-x-x.ec2.internal' is not allowed to connect to this MySQL server 分析,从 ...

  2. [BZOJ 3652]大新闻

    [BZOJ 3652] 大新闻 题意 随机从 \([0,n)\) 中选取一个整数 \(x\), 并从 \([0,n)\) 中再选取一个整数 \(y\). 有 \(p\) 的概率选取一个能令 \(x\o ...

  3. Jenkins 在mac平台的安装与配置

    1. 安装: 强烈推荐直接下载war包方式安装,方便修改项目工作目录.首先确保电脑上安装了java,接着下载tomcat(任意版本).将tomcat安装在任意想要的目录,然后下载jenkins war ...

  4. cachecloud搭建.md

    Cachecloud github地址 https://github.com/sohutv/cachecloud 搭建环境 主机 IP Java 7 Maven 3 MySQL Redis 3 cac ...

  5. nginx反向代理和tomcat集群(适用于ubutnu16.04及其centos7)

    下面示例,本人亲测有效 为什么要反向代理和集群? 因为并发问题,很多请求如果全部分发给一个tomcat,一个tomcat优化最好的话,据说可达到800负载,但是面对成千上万的请求,单单一个tomcat ...

  6. MySQL数据类型字节长度

    1.字符串 char(n): n 字节长度 varchar(n): 如果是 utf8 编码, 则是 3 n + 2字节; 如果是 utf8mb4 编码, 则是 4 n + 2 字节. 2.数值类型: ...

  7. PAT B1003 我要通过!

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...

  8. VBA删除 语法

    Option Explicit '清空数据  Private Sub CommandButton1_Click() Dim qknum As Integer  '选择是或者否 来确认删除数据 '中对话 ...

  9. 林帆:Docker运行GUI软件的方法

    继上周的“Kubernetes v1.0特性解析”分享之后,本周我们邀请到ThoughtWorks咨询师林帆为大家带来主题为“Docker运行GUI软件的方法”的分享. 嘉宾简介:林帆,Thought ...

  10. 20155207 实验5 MSF基础应用

    20155207 实验5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:让攻击方式能够发挥作用的通道,更像是没有子弹的枪,提供了攻击方 ...