【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 ...
随机推荐
- Oracle 11G在用EXP 导入、导出时,若有空表对导入导出中遇到的问题的解决
11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...
- c#项目调用Python模块的方法
将Python模块用pyinstaller打包成exe程序 下载安装UPX((http://upx.sourceforge.net/)) ,并把路径加到环境变量中. UPX是开源的加壳和压缩exe的程 ...
- 同步块:synchronized(同步监视器对象){同步运行代码片段}
package seday10; import seday03.Test2; /** * @author xingsir * 同步块:synchronized(同步监视器对象){需要同步运行的代码片段 ...
- 线性筛-mobius,强大O(n)
首先,你要知道什么是莫比乌斯函数 然后,你要知道什么是积性函数 最后,你最好知道什么是线性筛 莫比乌斯反演 积性函数 线性筛,见上一篇 知道了,就可以愉快的写mobius函数了 由定义: μ(n)= ...
- Spring Cloud netflix feign【服务间通信】
一.简介 1,进程间通讯的本质是交换消息 2,服务间通信的两种方式 (1)RESTFul风格 (2)RPC风格 (3)两种风格的比较 3.基于RESTFul风格服务调用模型 4.基于Spring Cl ...
- AD 快捷键设置
TAA 设置自动标记原件位号 AR 设置元件向右对齐 AL 设置元件向左对齐 MS 移动所选择 CO 错误报告设置 CTRL + w 设置电气线 PN 添加net label TG 打开封装管理器 P ...
- Linux - XShell - alt 快捷键的设置
1. 概述 命令行的 alt 快捷键可能会冲突 2. 环境 os win10 centos7 xshell xhell6 3. 场景 开启 centos7 虚拟机 在 win10 打开 xshell6 ...
- jquery实现登录后右下角弹窗提醒(附带简单样式)
页面代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 【Python】爬虫原理
前言 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什 ...
- 【C语言】用指针描述数组,实现冒泡法排序
#include <stdio.h> int main() { ], t; int i, j;printf("请输入10个数:\n"); ; i < ; i++) ...