您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页PAT_A1053#Path of Equal Weight

PAT_A1053#Path of Equal Weight

来源:爱玩科技网

Source:

Description:

Given a non-empty tree with root R, and with weight Wi​​ assigned to each tree node Ti​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from Rto 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 N positive 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

Keys:

Code:

 1 /*
 2 time: 2019-06-28 16:28:41
 3 problem: PAT_A1053#Path of Equal Weight
 4 AC: 21:32
 5 
 6 题目大意:
 7 树的带权路径长度:根结点到各个叶子结点带权路径长度之和
 8 现给定带权路径长度,打印所有等于该长度的路径
 9 
10 输入:
11 第一行给出:结点数N<100,分支结点数M,给定带权路径长度S
12 接下来一行,N个结点的权值(0~n-1)
13 接下来M行,分支结点id,孩子数K,各孩子id(根结点00)
14 输出:
15 各路径按照权值非增打印
16 
17 基本思路:
18 递减存储各分支结点的孩子结点,遍历并统计带权路径,符合条件的输出之
19 */
20 #include<cstdio>
21 #include<vector>
22 #include<algorithm>
23 using namespace std;
24 const int M=110;
25 vector<int> tree[M],path;
26 int w[M],pt=0,s;
27 
28 bool cmp(int a, int b)
29 {
30     return w[a] > w[b];
31 }
32 
33 void Travel(int root, int value)
34 {
35     if(tree[root].size()==0 && value==s)
36     {
37         path.push_back(root);
38         for(int i=0; i<path.size(); i++)
39             printf("%d%c", w[path[i]],i==path.size()-1?'\n':' ');
40         path.pop_back();
41         return;
42     }
43     path.push_back(root);
44     for(int i=0; i<tree[root].size(); i++)
45         Travel(tree[root][i],value+w[tree[root][i]]);
46     path.pop_back();
47 }
48 
49 int main()
50 {
51 #ifdef ONLINE_JUDGE
52 #else
53     freopen("Test.txt", "r", stdin);
54 #endif // ONLINE_JUDGE
55 
56     int n,m;
57     scanf("%d%d%d", &n,&m,&s);
58     for(int i=0; i<n; i++)
59         scanf("%d", &w[i]);
60     for(int i=0; i<m; i++)
61     {
62         int id,k,kid;
63         scanf("%d%d", &id, &k);
         for(int j=0; j<k; j++)
65         {
66             scanf("%d", &kid);
67             tree[id].push_back(kid);
68         }
69         sort(tree[id].begin(), tree[id].end(), cmp);
70     }
71     Travel(0,w[0]);
72 
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11103446.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务