PAT甲级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 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 {B
1, 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

Special thanks to Zhang Yuan and Yang Han for their contribution to the judge’s data.

解题思路

起初想的是首先利用结构体数组存储结点(数组下标即为结点编号),然后将每一个非叶子结点的孩子按照权值大小排序后存入结构体当中,;之后,再进行深度搜索,每次搜索一个点,就存入路径,当从根节点搜索到叶子结点时,判断权和是否等于给定数值,若等于,则直接输出路径上的点权。但是发现这样只能得到29分,原因是,上述的排序方法只能使得同一个父亲节点的孩子结点权值非递增排列,而不同父亲结点的孩子不能进行排序;当两个孩子结点A、B权值相同,A、B各自的孩子结点权值不同时,会出现错序的问题(参考博客:http://t.csdn.cn/IYwdH,感谢分享!)。

测试样例:

7 5 8
1 2 2 2 3 3 2
00 2 01 02
01 1 03
02 1 04
03 1 05
04 1 06

因此,思路需要修改为:先利用深度优先搜索(DFS)找到所有权和为S的路径,然后对这些路径进行排序;由于各个路径的长度不等,且需要按照类似字符串对比的方式对int类型的各个一维数组之间进行排序,显然,C++中的vector<vector <int>> a来存储是比较合适的,它可以通过sort(a.begin(),a.end())对其中的元素按照行来排列。

易错点

对于vector<vector <int>> a的使用,由于本题需要进行DFS,因此可以另开一个vector<int> t来进行存储新结点并回溯,当走到根结点的时候,判断权和是否等于给定值,若等于,将t push_back进入vector<vector <int>>

代码

#include<bits/stdc++.h>
using namespace std;

struct Node{
    int weight;//weight<1000
    vector <int> child;//孩子结点的编号
};

struct Node n[100];
int M,N;
long int S;//S指定权和
vector <vector <int>> node_list;
vector <int> t;

void DFS(int s, long int sum){
    int i,temp,len = n[s].child.size();
    if (len==0)//搜索到叶子节点了,判断是否权和是否相等
    {
        if (sum==S)
            node_list.push_back(t);
        return ;
    }
    for (i=0;i<len;i++)
    {
        temp = n[n[s].child[i]].weight;
        t.push_back(temp);
        DFS(n[s].child[i],sum+temp);
        t.pop_back();//回溯
    }
}

int main(){
    int i,j,t1,t2,t3;//N结点总数,M非叶子结点数目
    scanf("%d %d %ld",&N,&M,&S);
    for (i=0;i<N;i++)//读入第二行的权值
        scanf("%d",&n[i].weight);
    for (i=0;i<M;i++)
    {
        scanf("%d %d",&t1,&t2);
        for (j=0;j<t2;j++)//读取孩子结点
        {
            scanf("%d",&t3);
            n[t1].child.push_back(t3);
        }
    }
    t.push_back(n[0].weight);
    DFS(0,n[0].weight);
    if (node_list.empty()==1)
        return 0;
    //printf("%d",node_list[0].size());
    sort(node_list.begin(),node_list.end());//一维数组排序
    for (i=node_list.size()-1;i>=0;i--)
        for (j=0;j<node_list[i].size();j++)
            printf("%d%c",node_list[i][j],(j==node_list[i].size()-1)?'\n':' ');
    return 0;
}

错误代码

以下是29分的代码:

#include<bits/stdc++.h>
using namespace std;

struct Node{
    int weight;//weight<1000
    vector <int> child;//孩子结点的编号
};

struct Node n[100];
int M,N;
long int S;//S指定权和
vector <int> node_list;

int cmp(const void *a, const void *b){
    int c = *(int *)a;
    int d = *(int *)b;
    return (n[c].weight>=n[d].weight)?-1:1;//权重降序
}

void DFS(int s, int sum){
    int i,temp,len = n[s].child.size();
    if (len==0)//搜索到叶子节点了,判断是否权和是否相等
    {
        if (sum==S)
        {
            for (i=0;i<node_list.size();i++)
            {
                if (i!=0)
                    printf(" ");
                printf("%d",node_list[i]);
            }
            printf("\n");
        }
        return ;
    }
    for (i=0;i<len;i++)
    {
        temp = n[s].child[i];
        node_list.push_back(n[temp].weight);
        DFS(temp,sum+n[temp].weight);
        node_list.pop_back();//回溯
    }
}

int main(){
    int i,j,t1,t2,t3;//N结点总数,M非叶子结点数目
    scanf("%d %d %ld",&N,&M,&S);
    for (i=0;i<N;i++)//读入第二行的权值
        scanf("%d",&n[i].weight);
    for (i=0;i<M;i++)
    {
        scanf("%d %d",&t1,&t2);
        int a[t2];
        for (j=0;j<t2;j++)
            scanf("%d",&a[j]);
        qsort(a,t2,sizeof(int),cmp);
        for (j=0;j<t2;j++)//孩子结点按照权重从小到大的顺序排列
            n[t1].child.push_back(a[j]);
    }
    node_list.push_back(n[0].weight);
    DFS(0,n[0].weight);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值