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);
     ;
 }

  

最新文章

  1. fedora22,fedora24最简单的安装virtaulbox的方法
  2. destoon公司搜索页面显示公司类型
  3. Thinking in java学习笔记之初始化
  4. /etc/bashrc和/etc/profile傻傻分不清楚?
  5. FastReport调用Delphi中的人民币大写转换自定义函数
  6. 判断是苹果还是安卓app联调
  7. App应用与思考
  8. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
  9. 【HDOJ】5155 Harry And Magic Box
  10. jQuery性能优化篇
  11. Samba文件共享服务
  12. 前端里移动端到底比pc端多哪些知识?
  13. 关于windows映射网络驱动器,登录时重新连接
  14. javascript日期时间操作库推荐
  15. 浅谈String中的==和对象中引用对象类型的==
  16. Python模块练习题
  17. MHA 实现VIP切换用到脚本
  18. js 垃圾回收机制和引起内存泄漏的操作
  19. SSH防止超时的设置
  20. cyml

热门文章

  1. L215 Visual impairment
  2. workflow
  3. ZooKeeper客户端原生API的使用以及ZkClient第三方API的使用
  4. java程序员常用的八个工具
  5. SWIFT中将信息保存到plist文件内
  6. HDU 1548 A strange lift(BFS)
  7. 第32课 初探C++标准库
  8. VC++ 6.0 C8051F340 USB 通信 CAN 数据解析
  9. opencv-python教程学习系列5-处理鼠标事件
  10. PR使用技巧