天天看点

PTA甲级 1053 Path of Equal Weight (C++)

Given a non-empty tree with root R R R, and with weight W i W_i Wi​ assigned to each tree node T i T_i Ti​. The weight of a path from R R R to L L L is defined to be the sum of the weights of all the nodes along the path from R R R to any leaf node L L 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.

PTA甲级 1053 Path of Equal Weight (C++)

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0 < N ≤ 100 0<N≤100 0<N≤100, the number of nodes in a tree, M ( < N ) M (<N) M(<N), the number of non-leaf nodes, and 0 < S < 2 30 0<S<2^{30} 0<S<230, the given weight number. The next line contains N positive numbers where W i ( < 1000 ) W_i(<1000) Wi​(<1000) corresponds to the tree node T i T_i Ti​. Then M M 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 { A 1 , A 2 , ⋯ , A n } \{A_1,A_2,⋯,A_n\} {A1​,A2​,⋯,An​} is said to be greater than sequence { B 1 , B 2 , ⋯ , B m } \{B_1,B_2,⋯,B_m\} {B1​,B2​,⋯,Bm​} if there exists 1 ≤ k < m i n { n , m } 1≤k<min\{n,m\} 1≤k<min{n,m} such that A i = B i A_i=B_i Ai​=Bi​ for i = 1 , ⋯ , k i=1,⋯,k i=1,⋯,k, and A k + 1 > B k + 1 A_{k+1}>B_{k+1} 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
           

Caution:

这道题本身没什么特别的点,DFS就行,但是有一点让我很迷惑。

就是要对答案进行排序嘛,对两个数组进行类似于字典排序,我刚开始是调用sort的时候传入了我自己的写的一个比较函数:

bool cmp(vector<int>& a, vector<int>& b){
    int len1 = a.size();
    int len2 = b.size();

    int pos = 0;

    while(pos < len1 && pos < len2){
        if(a[pos] > b[pos]) return true;
        else if(a[pos] < b[pos]) return false;
        pos++;
    }

    return true;
}
           

可以看到和下面AC代码中的只有一点不同就是默认返回值改成true,但是就这一点不同让我在最后一个测试点得到完全不同的结果,但是如果是一个正确一个错误的话还好解释,但是问题在于提交上面的代码显示的是段错误(如下)。

PTA甲级 1053 Path of Equal Weight (C++)

就很迷惑。

我检查好长时间才发现是这里有问题,目前可行的解决方法有以下几个:

  • 传入模板函数

    greater<vector<int>>()

    (如下面被注释掉的那一行代码所示)
  • return true;

    改成

    return a.size() > b.size();

  • return true;

    改成

    return a.size() < b.size();

没错,改成返回

a.size() > b.size()

a.size() < b.size()

都能得到正确结果,不过这个其实在预料范围内,因为根据这个题的情况,在待比较的数组中,不存在两个数组一直比较到一个数组的结尾了还得不到结果(这样的话显然两个数组的元素之和肯定不一样),所以这句 return 实际上应该是不会执行的。

但这就更诡异了啊喂!

目前这个问题尚未解决。

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-18 17:51:15
// All rights reserved.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Node{
    bool isLeaf;
    int key;
    vector<int> children;

    Node(){
        key = 0;
        children.clear();
        isLeaf = true;
    }

    ~Node(){}
};

vector<Node> tree;

void dfs(vector<vector<int>>& ans, vector<int>& tmp, int id, int sum){
    if(tree[id].isLeaf){
        if(sum == 0) ans.push_back(tmp);
        return;
    } 

    if(sum <= 0) return;

    for(int i = 0; i < tree[id].children.size(); ++i){
        tmp.push_back(tree[tree[id].children[i]].key);
        dfs(ans, tmp, tree[id].children[i], sum - tree[tree[id].children[i]].key);
        tmp.pop_back();
    }

    return;
}

bool cmp(vector<int>& a, vector<int>& b){
    int len1 = a.size();
    int len2 = b.size();

    int pos = 0;

    while(pos < len1 && pos < len2){
        if(a[pos] > b[pos]) return true;
        else if(a[pos] < b[pos]) return false;
        pos++;
    }

    return false;
}

int main(){
    int n, m, s;
    // n - the number of nodes in a tree
    // m - the number of non-leaf nodes
    // s - the given weight number

    scanf("%d %d %d", &n, &m, &s);
    tree.resize(n);

    for(int i = 0; i < n; ++i) scanf("%d", &tree[i].key);

    for(int i = 0; i < m; ++i){
        int ID, children;
        scanf("%d %d", &ID, &children);
        tree[ID].children.resize(children);
        tree[ID].isLeaf = false;

        for(int j = 0; j < children; ++j) scanf("%d", &tree[ID].children[j]);
    }

    vector<vector<int>> ans;
    vector<int> tmp;

    tmp.push_back(tree[0].key);
    dfs(ans, tmp, 0, s - tree[0].key);
    tmp.pop_back();

    // sort(ans.begin(), ans.end(), greater<vector<int>>());
    sort(ans.begin(), ans.end(), cmp);

    for(int i = 0; i < ans.size(); ++i){
        for(int j = 0; j < ans[i].size(); ++j){
            if(j == ans[i].size() - 1) printf("%d\n", ans[i][j]);
            else printf("%d ", ans[i][j]);
        }
    }

    return 0;
}
           
PTA甲级 1053 Path of Equal Weight (C++)

继续阅读