1053 Path of Equal Weight (30 分)
Given a non-empty tree with root RRR, and with weight WiW_iWi assigned to each tree node TiT_iTi. The weight of a path from RRR to LLL is defined to be the sum of the weights of all the nodes along the path from RRR to any leaf node LLL.
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<N≤1000 < N \le 1000<N≤100, the number of nodes in a tree, MMM (<N< N<N), the number of non-leaf nodes, and 0<S<2300 < S < 2^{30}0<S<230, the given weight number. The next line contains NNN positive numbers where WiW_iWi (<1000<1000<1000) corresponds to the tree node TiT_iTi. Then MMM 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 {A1,A2,⋯,An}\{A_1, A_2, \cdots , A_n\}{A1,A2,⋯,An} is said to be greater than sequence {B1,B2,⋯,Bm}\{B_1, B_2, \cdots , B_m\}{B1,B2,⋯,Bm} if there exists 1≤k<min{n,m}1 \le k < min\{n, m\}1≤k<min{n,m} such that Ai=BiA_i = B_iAi=Bi for i=1,⋯,ki=1, \cdots , ki=1,⋯,k, and Ak+1>Bk+1A_{k+1} > B_{k+1}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
Special thanks to Zhang Yuan and Yang Han for their contribution to the judge's data.
分析
1. 权重计算用DFS
2. 找到合适的路径,从叶节点开始将权重压入stack,再导出
3. 降序输出。qsort()
1. 要么对最后的结果权重进行排序 https://www.xuebuyuan.com/2198192.html
2. 要么在最开始就对孩子节点进行权重排序(如下代码)
4. 注意输出格式
代码
#include<stdio.h>
#include<stdio.h>
#define Max 105
struct Node{
int id,weight;
int ma,child[Max],childnum;
}nodes[Max];
int N,M,S,sum=0;
///string for store weight
char we[Max][Max]={0};
int path=0;
void DFS(int);
int cmp(const int *a, const int *b){
///from max to min
return nodes[*b].weight - nodes[*a].weight;
}
int main(){
int i,j,m,n,c;
scanf("%d %d %d",&N,&M,&S);
///start init tree
for(i=0;i<N;i++) scanf("%d",&nodes[i].weight);
///tree root no ma
nodes[0].ma=-1;
for(i=0;i<M;i++){
scanf("%d %d",&m,&n);
nodes[m].childnum=n;
for(j=0;j<n;j++){
scanf("%d",&c);
nodes[m].child[j]=c;
nodes[c].ma=m;
}
///sort child from max to min according to weight
qsort(nodes[m].child,n,sizeof(int),cmp);
}
///use DFS find S
DFS(0);
//print paths in non-increasing
//qsort(we,path,sizeof(we[0]),cmp);
//a question: length of paths is not same
///output
for(i=0;i<path;i++){
printf("%d",we[i][0]);
for(j=1;we[i][j];j++) printf(" %d",we[i][j]);
//printf("-- ");
if(i!=path-1) printf("\n");
}
}
void DFS(int id){
int i,j,v,temp[Max];
sum+=nodes[id].weight;
///sum of paths from leaf node to root is equal with S, do it
if(sum == S && !nodes[id].childnum){
v=id;i=0;//temp is a stack, i is next loc
while(v != -1){
temp[i++]=nodes[v].weight;
v=nodes[v].ma;
}
// printf("leaf: %d we:",id);//delete
// for(j=i-1;j>=0;j--) printf("%d ",temp[j]);//delete
// printf("\n");//delete
j=0;
while(i>0) we[path][j++]=temp[--i];
we[path++][j]='\0';
}
for(i=0;i<nodes[id].childnum;i++){
v=nodes[id].child[i];
DFS(v);
}
sum-=nodes[id].weight;
return;
}