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 a path from R to 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 Figure 1: 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 Figure 1.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0 < N <= 100, the number of nodes in a tree, M (< N), the number of non-leaf nodes, and 0 < S < 230, the given weight number. The next line contains N positive numbers where Wi (<1000) 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 {A1, A2, ..., An} is said to be greater than sequence {B1, B2, ..., Bm} if there exists 1 <= k < min{n, m} such that Ai = Bi for i=1, ... k, 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
生詞
英文 | 解釋 |
---|---|
assigned to | 分派給 |
演算法筆記題解
不是很理解,還要再看看~
#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
struct node
{
int weight; //資料域
vector<int> child; //指標域
}Node[maxn]; //結點陣列
bool cmp(int a,int b){
return Node[a].weight>Node[b].weight; //按結點資料域從大到小排序
}
int n,m,s; //結點數、非葉結點數、給定的和
int path[maxn]; //記錄的路徑
//index:當前訪問結點下標
//numNode:當前路徑上的結點個數
//sum:當前的結點權重和
void DFS(int index,int numNode,int sum)
{
if(sum>s) return; //當前和sum超過s,直接返回
if(sum==s){ //當前和sum等於s
//還沒到葉子結點,直接返回
if(Node[index].child.size()!=0) return;
//到達葉子結點,此時path[]中存放了一條完整的路徑,輸出它
for(int i=0;i<numNode;i++){
printf("%d",Node[path[i]].weight);
if(i<numNode-1) printf(" ");
else printf("\n");
}
}
for(int i=0;i<Node[index].child.size();i++){ //列舉所有子結點
int child=Node[index].child[i]; //結點index的第i個子結點編號
path[numNode]=child; //將結點child加到path末尾
DFS(child,numNode+1,sum+Node[child].weight); //遞迴進入下一層
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
scanf("%d%d%d",&n,&m,&s);
for(int i=0;i<n;i++){
scanf("%d",&Node[i].weight);
}
int id,k,child;
for(int i=0;i<m;i++){
scanf("%d%d",&id,&k); //結點編號、孩子個數
for(int j=0;j<k;j++){
scanf("%d",&child);
Node[id].child.push_back(child); //child為結點id的孩子
}
sort(Node[id].child.begin(),Node[id].child.end(),cmp);
}
//路徑的第一個結點設定為0號結點
//path裡保留的是路徑的序號,並不是權重值
path[0]=0;
DFS(0,1,Node[0].weight);
return 0;
}
題目大意:
給出樹的結構和權值,找從根結點到葉子結點的路徑上的權值相加之和等於給定目標數的路徑,並且從大到小輸出路徑
分析:
對於接收孩子結點的資料時,每次完全接收完就對孩子結點按照權值進行排序(序號變,根據權值變),這樣保證深度優先遍歷的時候直接輸出就能輸出從大到小的順序。記錄路徑採取這樣的方式:首先建立一個path陣列,傳入一個nodeNum記錄對當前路徑來說這是第幾個結點(這樣直接在path[nodeNum]裡面儲存當前結點的孩子結點的序號,這樣可以保證在先判斷return的時候,path是從0~numNum-1的值確實是要求的路徑結點)。然後每次要遍歷下一個孩子結點的之前,令path[nodeNum] = 孩子結點的序號,這樣就保證了在return的時候當前path裡面從0~nodeNum-1的值就是要輸出的路徑的結點序號,輸出這個序號的權值即可,直接在return語句裡面輸出。
注意:
當sum==target的時候,記得判斷是否孩子結點是空,要不然如果不空說明沒有到底部,就直接return而不是輸出路徑。。。(這對接下來的孩子結點都是正數才有用,負權無用)
原文連結:https://blog.csdn.net/liuchuo/article/details/52258046
柳神程式碼
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int target;
struct NODE {
int w;
vector<int> child;
};
vector<NODE> v;
vector<int> path;
void dfs(int index, int nodeNum, int sum) {
if(sum > target) return ;
if(sum == target) {
if(v[index].child.size() != 0) return;
for(int i = 0; i < nodeNum; i++)
printf("%d%c", v[path[i]].w, i != nodeNum - 1 ? ' ' : '\n');
return ;
}
for(int i = 0; i < v[index].child.size(); i++) {
int node = v[index].child[i];
path[nodeNum] = node;
dfs(node, nodeNum + 1, sum + v[node].w);
}
}
int cmp1(int a, int b) {
return v[a].w > v[b].w;
}
int main() {
int n, m, node, k;
scanf("%d %d %d", &n, &m, &target);
v.resize(n), path.resize(n);
for(int i = 0; i < n; i++)
scanf("%d", &v[i].w);
for(int i = 0; i < m; i++) {
scanf("%d %d", &node, &k);
v[node].child.resize(k);
for(int j = 0; j < k; j++)
scanf("%d", &v[node].child[j]);
sort(v[node].child.begin(), v[node].child.end(), cmp1);
}
dfs(0, 1, v[0].w);
return 0;
}
本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15535264.html