题意:

输入三个正整数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. DB技能数据库里把技能伤害调整

    1. MagID 技能代号 2. MagName 技能名称 3. Effect Type 效果类型(使用技能时角色的动作效果) 4. Effect 效果(技能产生的动画效果) 5. Spell 每次耗 ...

  2. Python,正则表达式 - (?:)示例

    例如正则表达式a(?:b),匹配后没有包含'b'的分组 >>> string 'ab ac' >>> import re >>> string = ...

  3. 如何开通linux机器的对外访问端口

    1.先查看是否已经开通 2.没有开通,去linux机器查看防火墙,确实没有开通 3.修改防火墙 vim /etc/sysconfig/iptables 4.重启防火墙之后重新查看已经可以看到8000端 ...

  4. 矩阵快速幂+概率DP poj 3744

    题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

  5. Python turtle库详解

    Python turtle库详解 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在 ...

  6. python3中的正则表达式

    精确匹配: \d: 匹配一个数字     \w: 匹配一个字母或数字     . : 匹配任意一个字符     \s: 匹配一个空格(包括tab等空白符) 匹配变长的字符:     * : 匹配任意个 ...

  7. python学习之matplotlib绘制动图(FuncAnimation()参数)

    1.函数FuncAnimation(fig,func,frames,init_func,interval,blit)是绘制动图的主要函数,其参数如下: a.fig 绘制动图的画布名称 b.func自定 ...

  8. 前后端分离之 跨域和JWT

    书接上回:https://www.cnblogs.com/yangyuanhu/p/12081525.html 前后端分离案例 现在把自己当成是前端,要开发一个前后分离的简单页面,用于展示学生信息列表 ...

  9. css实现单行和多行省略号

    1.单行省略 { width:300px; overflow: hidden; text-overflow:ellipsis; whitewhite-space: nowrap; } 注:单行省略必须 ...

  10. 使用js制作 下拉选择日期列表 (即日期选择器)

    上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...