当前位置:网站首页>1053 Path of Equal Weight
1053 Path of Equal Weight
2022-07-19 21:39:00 【Brosto_Cloud】
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 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≤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
dfs,答案要先保存,经过排序后再输出。
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int a[1000];//存储每个节点的权值
bool vis[1000];
int ans[1000];
int n, m, num, x, y, k;
struct Array {
int size;
int a[1000];
} b[1000];
int cnt;
vector<int>g[1000];//存储非叶节点的孩子
bool cmp(Array a1, Array a2) {
int t = min(a1.size, a2.size);
for (int i = 1; i <= t; i++) {
if (a1.a[i] > a2.a[i]) {
return true;
} else if (a1.a[i] < a2.a[i]) {
return false;
}
}
return false;//这句必须有,不然会出现段错误
}
void dfs(int u, int sum, int ind) {
if (sum + a[u] == num && !(g[u].size() > 0)) {
ans[ind] = a[u];
for (int i = 1; i <= ind; i++) {
b[cnt].a[i] = ans[i];
b[cnt].size = ind;
}
cnt++;
return;
}
if (sum + a[u] < num) {
sum += a[u];
ans[ind] = a[u];
if (g[u].size() > 0) {
for (int j = 0; j < g[u].size(); j++) {
if (!vis[g[u][j]]) {
vis[g[u][j]] = 1;
dfs(g[u][j], sum, ind + 1);
vis[g[u][j]] = 0;
}
}
}
}
}
int main() {
cin >> n >> m >> num;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> x >> k;
for (int j = 0; j < k; j++) {
cin >> y;
g[x].push_back(y);
}
}
vis[0] = 1;
dfs(0, 0, 1);
sort(b, b + cnt, cmp);
for (int i = 0; i < cnt; i++) {
for (int j = 1; j <= b[i].size; j++) {
cout << b[i].a[j];
if (j != b[i].size) {
cout << ' ';
}
}
if (i != cnt - 1) {
cout << endl;
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Microorganisms are closely related to our life
Debezium系列之:深入解读Debezium重要的jmx指标
可以 DIY 装修的商城系统,你也能拥有!
集群时钟同步配置
The most complete idea debug debugging skills in history (super detailed cases)
Jenkins持续集成入门到精通
d静态导入对象
查缺补漏C语言:字符串处理函数
What is the difference between ASUS Tinker board 2S and raspberry pie 4B
API interface for adding and deleting videos in easydss platform customization project of internet live broadcast on demand
散户炒股选哪个证券公司手续费低,手机上开户安全吗
Debezium系列之:show slave status查看主从延迟可能存在的不同情形
LeetCode:733. 图像渲染【BFS】
优化冒泡排序
阻力很大的舵机电流特性
How did musk and twitter "break up" after falling in love for more than half a year?
按最少次数开关点亮所有灯
Arthas Xiaobai beginner
串行加法器/减法器
连接无限·协同无界|融云首届全球企业通信云大会 WECC 来了