PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
vector<int>Tree[maxn];
long long val[maxn];
long long path[maxn];
int n,m;
long long W; bool cmp(const int &a,const int &b)
{
return val[a]>val[b];
} void read()
{
scanf("%d%d%lld",&n,&m,&W);
for(int i=;i<n;i++) scanf("%lld",&val[i]);
for(int i=;i<=m;i++)
{
int id; scanf("%d",&id);
int k; scanf("%d",&k);
while(k--)
{
int to; scanf("%d",&to);
Tree[id].push_back(to);
}
}
} void dfs(long long sum,int x,int deep)
{
if(sum>W) return;
if(sum==W)
{
if(Tree[x].size()==){
printf("%lld",val[]);
for(int i=;i<deep;i++)
printf(" %lld",path[i]);
printf("\n");
}
return;
} for(int i=;i<Tree[x].size();i++)
{
path[deep]=val[Tree[x][i]];
dfs(sum+val[Tree[x][i]],Tree[x][i],deep+);
}
} void work()
{
for(int i=;i<n;i++)
sort(Tree[i].begin(),Tree[i].end(),cmp);
dfs(val[],,);
} int main()
{
read();
work();
return ;
}
PAT (Advanced Level) 1053. Path of Equal Weight (30)的更多相关文章
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- 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 ...
- 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甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- 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 ...
- 1053 Path of Equal Weight (30)(30 分)
Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight ...
- 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 weig ...
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
随机推荐
- HDU2206:IP的计算
Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...
- ios中的关键词retain release
内存分析 在函数中只要用new alloc copy 这样的分配空间时 则计算器retain就要为一 每调用一次就要加一 在.m文件中引用手动计数时 一定要调用[super dealloc]这 ...
- MinGW 运行C++程序的方法
1:安装好 MinGW 及 GCC 编译器后 , 当然 MinGW 全安装最保险了,不会太费劲 2:配置环境变量(注:可能需要重启后生效) 计算机 --> 属性 --> 高级系统设置 -- ...
- BMP文件格式详解
BMP文件格式详解(BMP file format) BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),是Windows系统中广 ...
- Kickstart 自动化安装配置
自动化安装案例: 一 ,系统环境 # cat /etc/redhat-release CentOS release 6.6 (Final) #Hostname [root@boot ~]# hostn ...
- c++判断一个字符串是否是数字
bool isNum(const string& str) { bool bRet = false; bool point = false; ) { return bRet; } ]) &am ...
- 国内的cdn
测试了一下,百度的非常快 ----------------------------------------------------------------------- 原文:https://www. ...
- java中对List中对象排序实现
package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...
- C++矩阵处理库--Eigen初步使用
项目要进行比较多的矩阵操作,特别是二维矩阵.刚开始做实验时,使用了动态二维数组,于是写了一堆Matrix函数,作矩阵的乘除加减求逆求行列式.实验做完了,开始做代码优化,发现Matrix.h文件里适 ...
- Android OpenGL ES(九)绘制线段Line Segment .
创建一个DrawLine Activity,定义四个顶点: float vertexArray[] = { -0.8f, -0.4f * 1.732f, 0.0f, -0.4f, 0.4f * 1.7 ...