PAT1053 Path of Equal Weight (30) (樹(shù)的遍歷)

題目

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 Figure 1: 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 Figure 1.

image.png

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 {B1, 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

分析

題目大意:輸入一個(gè)有N個(gè)帶權(quán)重節(jié)點(diǎn),M個(gè)非葉子節(jié)點(diǎn)的樹(shù)放椰,給定一個(gè)S喧半,求路徑從根節(jié)點(diǎn)到葉子節(jié)點(diǎn)權(quán)重加起來(lái)等于S的所有路徑馏予,并按字典序降序輸出事镣。
思路:用結(jié)構(gòu)體數(shù)組建立數(shù)據(jù)結(jié)構(gòu)驶悟,并且在建立時(shí)對(duì)每個(gè)子節(jié)點(diǎn)按權(quán)重大小重新排序焊夸。dfs遍歷的時(shí)候用數(shù)組path對(duì)路徑進(jìn)行跟蹤苛败,最后就直接輸出path中0:childNode-1的權(quán)重水孩。

代碼

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
    int w;
    int child_num;
    vector<int> child;
};
vector<int> path;
vector<node> tree;
int N, M, S;
void dfs(int r, int sum_w, int childNode)
{
    if(sum_w > S)
        return;
    if(sum_w == S)
    {
        if(tree[r].child_num != 0)
            return;
        for(int j = 0; j<childNode; j++)
        {
            cout << tree[path[j]].w;
            if(j != childNode-1)
                cout << " ";
            else
                cout << endl;
        }
    }
    for(int i = 0; i<tree[r].child_num; i++)
    {
        path[childNode] = tree[r].child[i];
        dfs(tree[r].child[i], sum_w+tree[tree[r].child[i]].w, childNode+1);
    }
}

int cmp(int a, int b)
{
    return tree[a].w > tree[b].w;
}

int main()
{

    cin >> N >> M >> S;
    tree.resize(N);
    path.resize(N);
    for(int i = 0; i<N; i++)
    {
        cin >> tree[i].w;
    }
    for(int i = 0; i<M; i++)
    {
        int ID;
        cin >> ID;
        cin >> tree[ID].child_num;
        tree[ID].child.resize(tree[ID].child_num);
        for(int j = 0; j<tree[ID].child_num; j++)
        {
            cin >> tree[ID].child[j];
        }
        sort(tree[ID].child.begin(), tree[ID].child.end(), cmp);
    }
//    for(int i = 0; i<N; i++)
//    {
//        cout << i << " " << tree[i].w << " " << tree[i].child_num << " ";
//        for(int j = 0; j<tree[i].child_num; j++)
//            cout << tree[i].child[j] << " ";
//        cout << endl;
//    }
    dfs(0, tree[0].w, 1);
    return 0;
}

總結(jié)

柳神的代碼
學(xué)會(huì)如何在遍歷樹(shù)的時(shí)候?qū)ν宦窂竭M(jìn)行跟蹤
利用sort镰矿,實(shí)現(xiàn)樹(shù)按某一屬性(權(quán)重)重新排序

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市俘种,隨后出現(xiàn)的幾起案子秤标,更是在濱河造成了極大的恐慌,老刑警劉巖宙刘,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苍姜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡悬包,警方通過(guò)查閱死者的電腦和手機(jī)衙猪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)布近,“玉大人垫释,你說(shuō)我怎么就攤上這事〉跏洌” “怎么了饶号?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)季蚂。 經(jīng)常有香客問(wèn)我茫船,道長(zhǎng),這世上最難降的妖魔是什么扭屁? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任算谈,我火速辦了婚禮,結(jié)果婚禮上料滥,老公的妹妹穿的比我還像新娘然眼。我一直安慰自己,他們只是感情好葵腹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布高每。 她就那樣靜靜地躺著屿岂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鲸匿。 梳的紋絲不亂的頭發(fā)上爷怀,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音带欢,去河邊找鬼运授。 笑死,一個(gè)胖子當(dāng)著我的面吹牛乔煞,可吹牛的內(nèi)容都是我干的吁朦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼渡贾,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼逗宜!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起剥啤,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤锦溪,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后府怯,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體刻诊,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年牺丙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了则涯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冲簿,死狀恐怖粟判,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情峦剔,我是刑警寧澤档礁,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站吝沫,受9級(jí)特大地震影響呻澜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惨险,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一羹幸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辫愉,春花似錦栅受、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)依疼。三九已至,卻和暖如春闸衫,著一層夾襖步出監(jiān)牢的瞬間涛贯,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工蔚出, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虫腋。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓骄酗,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親悦冀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子趋翻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容