天天看點

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++)

繼續閱讀