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 a path from Rto L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.
Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0, the number of nodes in a tree, M (<), the number of non-leaf nodes, and 0, the given weight number. The next line contains Npositive numbers where Wi (<) corresponds to the tree node Ti. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 00.
Output Specification:
For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.
Note: sequence { is said to be greater than sequence { if there exists 1 such that Ai=Bi for ,, and Ak+1>Bk+1.
Sample Input:
20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19
Sample Output:
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2
分析:DFS 30分里的水题。。回溯一遍就行了。。但测试点2一直过不了。。
2.24更新,发现问题了,sort排序的时候应该对temp排序,太粗心了。另外isused数组也没啥用。。删了即可
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-21-15.57.11
* Description : A1053
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
struct node{
int weight;
vector<int> child;
}Node[maxn];
int n,m,s;
vector<int> path;
bool cmp(int a,int b){
return Node[a].weight>Node[b].weight;
}
//bool isUsed[maxn]={false};
void DFS(int index,int sum){
if(index>=n||sum>s) return;
if(sum==s){
) return;
int len=path.size();
;i<len;i++){
printf("%d",Node[path[i]].weight);
) printf(" ");
else printf("\n");
}
return;
}
int t=Node[index].child.size();
;i<t;i++){
int child=Node[index].child[i];
//if(isUsed[child]==true) continue;
path.push_back(child);
//isUsed[child]=true;
DFS(child,sum+Node[child].weight);
//isUsed[child]=false;
path.pop_back();
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int temp,k,c;
scanf("%d%d%d",&n,&m,&s);
;i<n;i++){
scanf("%d",&Node[i].weight);
}
;i<m;i++){
scanf("%d%d",&temp,&k);
;j<k;j++){
scanf("%d",&c);
Node[temp].child.push_back(c);
}
//sort(Node[i].child.begin(),Node[i].child.end(),cmp);
sort(Node[temp].child.begin(),Node[temp].child.end(),cmp);
}
path.push_back();
DFS(,Node[].weight);
;
}
1053 Path of Equal Weight (30 分)的更多相关文章
- 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 ...
- 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 分)(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 ...
- 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 weight of ...
- 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 (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
随机推荐
- PHP实现日志处理类库 - 【微信开发之微电商网站】技术笔记之二
继上篇文章[微信开发之微电商网站]技术笔记之一,昨日做了日志处理的功能. 对于现在的应用程序来说,日志的重要性是不言而喻的.很难想象没有任何日志记录功能的应用程序运行在生产环境中.日志所能提供的功能是 ...
- 2018-2019-2 网络对抗技术 20165202 Exp4 恶意代码分析
博客目录 一.实践目标 二.实践内容 1.系统运行监控 2.恶意软件分析 三.实验步骤 四.基础问题回答 五.遇到的问题及解决 六.实验总结 一.实践目标 监控你自己系统的运行状态,看有没有可疑的程序 ...
- 添加react-router
1.index.js 内容: import React from 'react' import ReactDOM from 'react-dom' import { renderRoutes } fr ...
- ARM_Instruction_Set_Encoding_hacking(ARM指令集编码格式解读)
ARM指令集编码格式解读 说明: 1.本文参考的书籍<ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition>中的Cha ...
- PHP vs Node.js
网络正在处于一个日新月异的发展时代.服务器端开发人员在选择语言的时候非常困惑,有长期占主导地位的语言,例如C.Java和Perl,也有专注于web开发的语言,例如Ruby.Clojure和Go.只要你 ...
- Appium笔记(一) 丶Appium的自我介绍
一.我是谁,我的特点是什么 Appium是一款开源测试自动化框架,可用于原生.混合和移动Web应用程序.它使用WebDriver协议驱动iOS,Android和Windows应用程序.重要的是,App ...
- MVC中未能加载程序集System.Web.Http/System.Web.Http.WebHost
==================================== 需要检查项目的Microsoft.AspNet.WebApi版本是否最新,System.Web.Http 这个命名空间需要更新 ...
- vue组件独享守卫钩子函数参数详解(beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)
一样的和前面路由钩子类似的步骤 首先在demo下面的components下面新建一个test.vue组件 test组件代码 <template> <div class="t ...
- exec函数簇
转自:http://www.cppblog.com/prayer/archive/2009/04/15/80077.html 也许有不少读者从本系列文章一推出就开始读,一直到这里还有一个很大的疑惑:既 ...
- LG2044 [NOI2012]随机数生成器
题意 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a,c ...