PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于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的更多相关文章
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 Wi assigne ...
- 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 ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...
- 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 ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水
由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
随机推荐
- JS中的防抖与节流
什么是防抖?and什么是节流?一起来开心的学习下吧. 首先什么是防抖:就是在一定的时间内事件只发生一次,比如你点击button按钮,1秒内任你单身30年手速点击无数次,他也还是只触发一次.举个例子,当 ...
- mac下idea 13 在tomcat 7控制台乱码
在mac或linux下idea 13(可能其它版本也会出现乱码) tomcat 7在输出到控制台的日志中文乱码,解决方式 加一个environment variable, 在如图绿色位置添加 JA ...
- MySql详解(六)
MySql详解(六) MySql事务 一.含义 事务:一条或多条sql语句组成一个执行单位,一组sql语句要么都执行要么都不执行 二.特点(ACID) A 原子性:一个事务是不可再分割的整体,要么都执 ...
- JAVA引用的种类
最近在进行Java项目开发的时候,由于业务的原因,有时候new的对象会比较多,这个时候我总是有一个疑惑?那就是JVM在何时决定回收一个Java对象所占据的内存?这个问题其实对整个web系统来说是一个比 ...
- BZOJ 1208 宠物收养所 set+二分
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题目大意: 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠 ...
- Docker技术入门与实战 第二版-学习笔记-4-Dockerfile外其他生成镜像的方法
其它生成镜像的方法 即除了标准地使用Dockerfile来生成镜像外,还有一些其他的方法 1)从 rootfs 压缩包导入 格式:docker import [选项] <文件>|<U ...
- shiro实战系列(一)之入门实战
一.什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密. Apache Shiro 的首要目标是易于使用和理解.安全有 ...
- Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs
在部署工程时,没有加入javax.servlet-api.jar(3.0.1) 和 standard.jar(1.1.2)两个jar包导致此问题. 另外,如果原来容器中有javax.servlet-a ...
- JS日历控件优化(增加时分秒)
JS日历控件优化 在今年7月份时候 写了一篇关于 "JS日历控件" 的文章 , 当时只支持 年月日 的日历控件,现在优化如下: 1. 在原基础上 支持 yyyy ...
- Java实现Zip压缩包解压
对zip压缩包的解压是比较常见的应用场景,java代码的实现也很简单.废话不多说,直接上代码吧 一.代码 /** * zip解压 * @param srcFile zip源文件 * @para ...