1053 Path of Equal Weight (30 分)(树的遍历)

题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径

#include<bits/stdc++.h>

using namespace std;
int n,m,sum;
const int N=120;
struct node
{
    int w;
    vector<int>p;
}tree[N];
bool cmp(int a,int b)
{
    return tree[a].w>tree[b].w;
}
vector<int>path;
void dfs(int s,int v)
{
    //cout<<s<<"  "<<v<<endl;
    if(s>sum) return;
    if(s==sum){
        if(tree[v].p.size()!=0) return;
        path.push_back(v);
        for(int i=0;i<path.size();i++){
            if(i) printf(" ");
            printf("%d",tree[path[i]].w);
        }
        printf("\n");
        path.pop_back();
    }
    path.push_back(v);
    for(int i=0;i<tree[v].p.size();i++){
        dfs(s+tree[tree[v].p[i]].w,tree[v].p[i]);
    }
    path.pop_back();
}
int main()
{
    scanf("%d %d %d",&n,&m,&sum);
    for(int i=0;i<n;i++) scanf("%d",&tree[i].w);
    for(int i=0;i<m;i++){
        int id;
        int k;
        scanf("%d %d",&id,&k);
        for(int j=0;j<k;j++){
            int x;
            scanf("%d",&x);
            tree[id].p.push_back(x);
        }
        sort(tree[id].p.begin(),tree[id].p.end(),cmp);
    }
    dfs(tree[0].w,0);
    return 0;
}

原文地址:https://www.cnblogs.com/chenchen-12/p/10226074.html

时间: 2024-11-29 10:55:26

1053 Path of Equal Weight (30 分)(树的遍历)的相关文章

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. 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 tre

1053 Path of Equal Weight (30分)

1. 题目 2. 思路 定义结构体, 并且使用下标作为序号 struct node{ string weight; vector<int> children; }nodes[101]; 读取数据,并且排序children,方便输出 使用先序遍历,处理数据 3. 注意点 权重的值很大,用字符串处理,要自己写加法和比较函数 4. 代码 #include<cstdio> #include<algorithm> #include<vector> #include&l

PAT 1053. Path of Equal Weight (30)

1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 Lis defined to be the sum of the

1053. Path of Equal Weight (30)

dfs函数携带vector形参记录搜索路径 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT Advanced 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,

PAT:1053. Path of Equal Weight (30) AC

#include<stdio.h> #include<vector> #include<queue> #include<algorithm> using namespace std; const int MAX=1010; int n,m; //n个节点,m个非叶子节点 long long int S; //待测权值 long long int weight[MAX]; //每个节点的权值 vector<int> child[MAX]; //存储

PAT (Advanced Level) 1053. Path of Equal Weight (30)

简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; const int maxn=100+10;

PAT甲题题解-1053. Path of Equal Weight (30)-dfs

由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排序,然后再add建边. 水题一个,不多说了. #include <iostream> #include <algorithm> #include <cstdio> #include <string.h> using namespace std; const in

pat1053. Path of Equal Weight (30)

1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT Advanced Level 1053 Path of Equal Weight

1053 Path of Equal Weight (30)(30 分) Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. 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