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)代表每个结点的权重 ...
随机推荐
- 布局:上下两个div高度固定,中间自适应
需求:经典布局 —— 头尾固定高度中间高度自适应布局 头部固定高度,宽度100%自适应父容器: 底部固定高度,宽度100%自适应父容器: 中间是主体部分,自动填满,浏览器可视区域剩余部分,内容超出则中 ...
- win10管理员已阻止你运行此应用”解决方法
方法/步骤 1 按WIN+R键,打开“运行”,然后输入“gpedit.msc",就是打开组策略,这个在控制面板中也可以打开. 2 在组策略里找到“计算机配置”-“Windows设置”-“安全 ...
- P4279 [SHOI2008]小约翰的游戏
嘟嘟嘟 一道博弈论经典题,nim游戏. 只不过要考虑有奇数个石子为1的堆的时候,为Brother赢.剩下就是nim游戏了. 极简代码 #include<cstdio> using name ...
- docker swarm英文文档学习-4-swarm模式如何运行
1)How nodes work Docker引擎1.12引入了集群模式,使你能够创建一个由一个或多个Docker引擎组成的集群,称为集群.集群由一个或多个节点组成:在群模式下运行Docker引擎1. ...
- DataGridView如何绑定DataRow对象集合
DataGridView对象是我们在进行Winform程序开发中经常使用的呈现数据的控件,而数据则是通过DataSource这个Property来设置的.根据MSDN的说明,DataGridView对 ...
- PAT B1008 数组元素循环右移问题 (20 分)
一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A ...
- selenium自动化环境搭建(Windows)
参考内容:虫师<selenium2自动化测试实战-基于python语言> 一.selenium介绍 selenium主要用于web应用程序的自动化测试,还支持所有基于web的管理任务自动化 ...
- 在main函数前后执行的函数之 C语言
在gcc中,可以使用attribute关键字,声明constructor和destructor,来指定了函数在main之前或之后运行,代码如下: #include <stdio.h> __ ...
- STS-创建spring配置文件
1.创建一个bean文件 2.输入文件名applicationContext.xml 3.这里会自动显示模板文件 4.创建后,自动填充头不定义 到这里就可以发现,我们创建spring文件时,需要的配置 ...
- DOTNET Core 命令
dotnet 命令目录: 1.dotnet-new 2.dotnet-restore 3.dotnet-build 4.dotnet-run 5.dotnet-test 6.dotnet-pack 7 ...