【1053】Path of Equal Weight (闲置队列)

简介: (1)由于最后的输出要按权值从大到小排序,所以在读入时就事先对每个结点的子结点child进行排序,这样在遍历时就会优先遍历到权值大的子结点。(2)递归过程中保存路径:使用path[]

1.题目

https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512

求所有从根结点到叶子结点的路径,使得每条路径上结点的权值之和等于常数S。

如果存在多条这样的路径,则按照路径非递增顺序输出。

2.思路

(1)由于最后的输出要按权值从大到小排序,所以在读入时就事先对每个结点的子结点child进行排序,这样在遍历时就会优先遍历到权值大的子结点。

(2)递归过程中保存路径:使用path[]数组表示路径 或者 使用vector。

后者:当枚举当前访问结点的子结点的过程中,就可以先使用push_back()将子结点加入路径中,然后往下一层递归。最后在下一层递归回溯上来之前讲前面加入的子结点pop_back()即可。

(3)递归出现sum==S时要判断当前访问结点是否为叶结点,如果不是则必须访问。

(4)cmp的所有情况都必须有返回值——因为程序需要能够处理两条路径上结点weight相同的情况。


3.完整代码

#include<vector>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
using namespace std;
const int maxn=110;
int num;//既定的路径之和
struct node{
  int data;
  vector<int>child;//孩子结点
}Node[maxn];
vector<int>path;//装入一条路径的所有结点
bool cmp(int a,int b){
  return Node[a].data>Node[b].data;
}
void DFS(int index,int sum){
  if(sum>num) return;
  if(sum==num){//注意还要判断是否为叶结点
    if(Node[index].child.size()==0){//为叶子结点时就结算
      printf("%d ",Node[index].data);
      for(int i=0;i<path.size();i++){//输出路径所有结点
        printf(" %d",path[i]);
      }
      printf("\n");
    }else
      return;//没到叶结点就直接返回
  }
  for(int i=0;i<Node[index].child.size();i++){//遍历当前结点的所有结点 
    int childID=Node[index].child[i];
    path.push_back(Node[childID].data);//别漏了
    DFS(childID,sum+Node[childID].data);
  }
}
int main(){
  int nodenum,nonleaf;
  scanf("%d%d%d",&nodenum,&nonleaf,&num);
  for(int i=0;i<nodenum;i++){
    scanf("%d",&Node[i].data);
  }
  for(int i=0;i<nonleaf;i++){
    int father,childnum;
    scanf("%d%d",&father,&childnum);
    for(int j=0;j<childnum;j++){
      //scanf("%d",Node[i].child[i]); 
      int cn;
      scanf("%d",&cn);
      Node[i].child.push_back(cn);
    }
    sort(Node[father].child.begin(),Node[father].child.end(),cmp);//从大到小排序
  }
  path[0]=0;
  DFS(0,Node[0].data);
  system("pause");
}
相关文章
|
11月前
|
Python
双端优先级队列(Double-Ended Priority Queue
双端优先级队列(Double-Ended Priority Queue)是一种支持在两端进行插入和删除操作的优先级队列。它可以在 O(log n) 的时间复杂度内完成插入、删除、查询操作。双端优先级队列可以使用二叉堆或线段树等数据结构实现。
258 6
错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7c1609
错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7c1609c9-9d0f-4836-85b3-cefd45f942a7.
5463 0
|
5月前
|
关系型数据库 Shell OceanBase
您的ulimit参数"max user processes"的当前值为4096,而OceanBase安装OCP 4.2.1时要求该值不能小于655350
您的ulimit参数"max user processes"的当前值为4096,而OceanBase安装OCP 4.2.1时要求该值不能小于655350
334 2
|
11月前
|
BI
[TOP]load average 负载相关
[TOP]load average 负载相关
38 0
|
机器学习/深度学习
1361:产生数(Produce)
1361:产生数(Produce)
101 0
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
未完成--Sum of Pairs
未完成--Sum of Pairs
75 0
|
流计算
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
【1053】Path of Equal Weight (30 分)
【1053】Path of Equal Weight (30 分) 【1053】Path of Equal Weight (30 分)
108 0