【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意:
输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重,接下来输入M行,每行包括一个两位数字组成的数代表非叶子结点的编号以及数字x表示它的孩子结点个数,接着输入x个数字表示孩子结点的编号。以非递增序输出从根到叶子结点的路径权重,它们的和等于S。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int w[];
vector<int>v[];
int s;
int now;
vector<int>path;
int flag;
int vis[];
vector<int>ans[];
int cnt;
int dfs(int x){
path.push_back(w[x]);
now+=w[x];
if(now==s){
if(!vis[x])
ans[++cnt]=path;
path.pop_back();
now-=w[x];
return ;
}
else if(now>s){
path.pop_back();
now-=w[x];
return ;
}
else
for(auto it:v[x])
dfs(it);
path.pop_back();
now-=w[x];
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m>>s;
for(int i=;i<n;++i)
cin>>w[i];
for(int i=;i<=m;++i){
string fa;
cin>>fa;
int f=(fa[]-'')*+fa[]-'';
vis[f]=;
int x;
cin>>x;
string son;
for(int j=;j<=x;++j){
cin>>son;
int s=(son[]-'')*+son[]-'';
v[f].push_back(s);
}
}
dfs();
sort(ans+,ans++cnt);
for(int i=cnt;i;--i){
if(i<cnt)
cout<<"\n";
cout<<ans[i][];
for(int j=;j<ans[i].size();++j)
cout<<" "<<ans[i][j];
}
return ;
}
【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)的更多相关文章
- 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 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- 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 ...
- 【PAT】1053 Path of Equal Weight(30 分)
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲级——A1053 Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- 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 ...
随机推荐
- MP3 文件格式解析
目录: 1.mp3 文件简介 2.ID3 tag id3 v2 3.音频帧 要注意的地方 4.参考 5.一个临时解析方法 一.MP3文件简介 MP3(mpeg-1 Ⅲ 或者 mpeg-2 Ⅲ)是一种将 ...
- 揭秘jQuery-选择器
先看代码: $(“li”)只选择第一个无序列表中的一个li元素,而不会选择另一个无序列表中的li元素 <!DOCTYPE html> <html> <head> & ...
- Jarvis OJ - 软件密码破解-1 -Writeup
Jarvis OJ - 软件密码破解-1 -Writeup 转载请标明出处http://www.cnblogs.com/WangAoBo/p/7243801.html 记录这道题主要是想记录一下动态调 ...
- CSS input
去除激活 input 的默认边框 // 三种方法都能实现 input{ outline: none; outline: medium; outline:; } 修改光标颜色 input{ outl ...
- [MongoDB]MongoDB分页显示
MongoDB Limit与Skip方法配合进行分页MongoDB Limit() 方法如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接 ...
- 集合的操作 contains(),containsAll() ,addAll(),removeAll(),
package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;/** ...
- web前端-基础篇
该篇仅是本人学习前端时,做的备忘笔记: 一.背景图片设置: 设置背景图时的css代码:background-image:url(图片的url路径); ps:设置好这个背景后请一定要设置该背景图片的大小 ...
- (DFS)HDU_1241 Oil Deposits
HDU_1241 Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible f ...
- SQL过滤条件on和where
在使用left jion时,会生成一张中间的临时表,然后再将这张临时表返回给用户. on和where条件的区别如下:1. on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表 ...
- 剑指OFFER之合并两个排序的链表
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解决办法 1.递归方法: if(pHead1==NULL) return pHead2; els ...