Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. 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 W​i​​ (<) corresponds to the tree node T​i​​. 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 A​i​​=B​i​​ for ,, and A​k+1​​>B​k+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 分)的更多相关文章

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

  3. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

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

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

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

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

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

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

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

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

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

  10. pat1053. Path of Equal Weight (30)

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

随机推荐

  1. DevExpress v17.2新版亮点—WinForms篇(四)

    用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.开篇介绍了DevExpress WinForms v17.2 Data Grid Control ...

  2. 【Grails 代理Proxy设置】部署Grails遇到Error Resolve error obtaining dependencies:错误,及解决方法

    最近在使用Grails,一开始使用3.0.2版本,一直包nullpointer错误,后来使用了2.5.0版本,可以创建工程,但是在进入到工程目录后,再执行grails程序,报错了,步骤如下: 1. g ...

  3. PlistBuddy

    最近由于工作需要,发现了这么一个小工具. PlistBuddy是一个Mac里的命令行下读写plist文件的工具. 位于/usr/libexec/下,由于这个路径不在默认的PATH里,需要通过绝对路径/ ...

  4. opencv-python教程学习系列2-读取/显示/保存图像

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍图像的读取.显示以及保存,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: ...

  5. HDU 2089:不要62

    Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来 ...

  6. Windows系统清除远程连接记录的方法

    =============================== 1.点击“开始->运行”,在输入框中键入regedit并回车. 在打开的注册表编辑器中, 找到“HKEY_CURRENT_USER ...

  7. 字符串匹配--(K)MP模板

    大白书型模板,并没有写成函数形式: 这个是下标从0开始的: 此外,对于一个长度为 m 的串自匹配,在此模板中 m-p[m] 就是这个串的最小循环节长度(最后一个循环可以不完整) 另一个关于自匹配后的失 ...

  8. JQuery判断form表单是否为空

    功能:通过jquery判断form表单中是否有内容还未填写,如果有未填写的,则阻止提交 $(function () { $('form').bind('submit',function () {  / ...

  9. MHA之Binlog Dump (GTID)僵尸进程清理

      master存活的状态下切换 masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_mas ...

  10. 在函数内部定义的函数 this 指向 undefined

    在函数内部定义的函数 this 指向 undefined 以下这个 this 就是指向 undefined. 但在非 strict 模式下是指向 window <script> 'use ...