题意:

输入三个正整数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)的更多相关文章

  1. 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 ...

  2. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. 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 ...

  4. 1053 Path of Equal Weight (30分)(并查集)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  5. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  6. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT甲级——A1053 Path of Equal Weight

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  8. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

    由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...

  9. 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 ...

随机推荐

  1. pocketsphinx实现连续大词汇量语音识别

    之前有个项目需求是要在客户内网实现一个连续大词汇语音识别功能,由于客户的内网是独立的,不能访问互联网,所以我只能到开源社区去找找碰碰运气了.后来在网上找到了cmusphinx(地址:http://cm ...

  2. 560. 和为K的子数组

    Q: A: 1.暴力找所有可能的子数组,n^2个子数组,最长长度n,则n ^3. 2.n^2解法 从1~n-1各起点开始,一直找到结尾,n^2 class Solution { public: int ...

  3. sqlserver 查看视图语句

    本人sql小白一个,在项目中遇到了视图的使用,但是不知道视图的语句怎么查看,所以在网上搜索了一下,查到了一下的查看方式,再次记录一下: 方法一->前提: 已经创建好的视图 sp_helptext ...

  4. think PHP5中,模板、控制器、JavaScript的url跳转重定向方法

    php控制器中的跳转: 1, header()函数是PHP中进行页面跳转的一种十分简单的方法.主要功能是将HTTP协议标头(header)输出到浏览器. header("Location: ...

  5. 静态方法使用synchronized修饰.

    package seday10;/** * @author xingsir * 静态方法若使用synchronized修饰,这个方法一定具有同步效果.静态方法上使用的同步监视器对象为这个类的" ...

  6. TXT文件也能挂木马

    什么?TXT文件也能挂马?是的!TXT文件不仅有挂马的危险,而且有时候可能非常的危险!不过,严格说来,应该给这个所谓的"TXT"文件加个引号,因为它们是看起来是TXT文件,实则是隐 ...

  7. sql server获取查询时间

    declare @d datetime set @d=getdate() /*你的SQL脚本开始*/ /*你的SQL脚本结束*/ select [语句执行花费时间(毫秒)]=datediff(ms,@ ...

  8. 断点调试,issubclass和ininstance的使用

    一等公民 只要可以把一个东西赋值给一个变量,这个东西就叫一等公民 断点调试 在想要加断点的地方用鼠标点击一下,你会看到一个红色圆圈 变红的地方,程序执行到,就会暂停 断电应该加载报错之前 绿色箭头表示 ...

  9. webRTC中回声消除(AEC)模块编译时aec_rdft.c文件报错:

    webRTC中回声消除(AEC)模块编译时aec_rdft.c文件报错. 原因是: 局部变量ip跟全局变量冲突的问题,可以将局部变量重新命名一下,就可以通过编译了. aec_rdft.c修改以后文件代 ...

  10. libcurl库的简介(一)

    一.Libcurl库简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP ...