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数组也没啥用。。删了即可
1 /**
2 * Copyright(c)
3 * All rights reserved.
4 * Author : Mered1th
5 * Date : 2019-02-21-15.57.11
6 * Description : A1053
7 */
8 #include<cstdio>
9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=110;
20 struct node{
21 int weight;
22 vector<int> child;
23 }Node[maxn];
24 int n,m,s;
25 vector<int> path;
26 bool cmp(int a,int b){
27 return Node[a].weight>Node[b].weight;
28 }
29 //bool isUsed[maxn]={false};
30 void DFS(int index,int sum){
31 if(index>=n||sum>s) return;
32 if(sum==s){
33 if(Node[index].child.size()!=0) return;
34 int len=path.size();
35 for(int i=0;i<len;i++){
36 printf("%d",Node[path[i]].weight);
37 if(i!=len-1) printf(" ");
38 else printf("\n");
39 }
40 return;
41 }
42 int t=Node[index].child.size();
43 for(int i=0;i<t;i++){
44 int child=Node[index].child[i];
45 //if(isUsed[child]==true) continue;
46 path.push_back(child);
47 //isUsed[child]=true;
48 DFS(child,sum+Node[child].weight);
49 //isUsed[child]=false;
50 path.pop_back();
51 }
52 }
53
54 int main(){
55 #ifdef ONLINE_JUDGE
56 #else
57 freopen("1.txt", "r", stdin);
58 #endif
59 int temp,k,c;
60 scanf("%d%d%d",&n,&m,&s);
61 for(int i=0;i<n;i++){
62 scanf("%d",&Node[i].weight);
63 }
64 for(int i=0;i<m;i++){
65 scanf("%d%d",&temp,&k);
66 for(int j=0;j<k;j++){
67 scanf("%d",&c);
68 Node[temp].child.push_back(c);
69 }
70 //sort(Node[i].child.begin(),Node[i].child.end(),cmp);
71 sort(Node[temp].child.begin(),Node[temp].child.end(),cmp);
72 }
73 path.push_back(0);
74 DFS(0,Node[0].weight);
75 return 0;
76 }