【PAT甲級筆記】樹的題型以及對應解法

tips:

  • 樹類的問題,一般需要用到dfs並且,創建(結構體型)節點,

1053. Path of Equal Weight (30)

tip: sort排序的時候,對父節點排序…

#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;

struct Node
{
    int w;
    vector<int> child;
};

//define
int n,m,target,fat,k,temp;
vector<Node> v;
vector<int> path;

void dfs(int sum, Node d1)
{

    if(sum>target) return;
    if(sum == target)
    {
     if(d1.child.empty()==0)
            return;
     //print
     printf("%d", v[path[0]].w);
     for(int j=1;j<path.size();j++)
        printf(" %d",v[path[j]].w);
     printf("\n");
     return;
    }
    for (int i=0;i<d1.child.size();i++)
        {
            int index = d1.child[i];
            sum+=v[index].w;
            path.push_back(index);
            dfs(sum,v[index]);
            sum-=v[index].w;
            path.pop_back();
        }
    return;
}

int cmp(int d1, int d2)
{

    return v[d1].w > v[d2].w;
}
int main()
{
    scanf("%d%d%d", &n, &m, &target);
    v.resize(n);
    for(int i=0;i<n;i++)
        scanf("%d", &v[i].w);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d", &fat, &k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&temp);
            v[fat].child.push_back(temp);
        }
        sort(v[fat].child.begin(),v[fat].child.end(),cmp);
    }
    path.push_back(0);
    dfs(v[0].w,v[0]);
    return 0;
}

1079 Total Sales of Supply Chain (25分)

tips:

  • 遞歸調用傳入的參數最好爲int等,傳入結構體會出現段錯誤,void dfs(Node d1, int depth). 具體原因並不是很清楚,大概是特別佔用內存的時候會溢出吧…

2/11補充: 使用函數傳入參數應該使用傳入引用的方式,不然都會超時,不一定非要用索引,可以在void dfs(Node &d1, int depth),這樣也能過,而且鼓勵推薦

  • 當出現段錯誤時,可以註釋掉一部分代碼,如果提交後變成了答案錯誤,那說明註釋代碼爲出問題的點。(如果提交後仍爲段錯誤,則註釋代碼並不是錯誤點)
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>

using namespace std;

struct Node
{
    //int depth;
    vector<int> child;
    bool is_ch;
};

//define
vector<Node> point;
int n,k,index;
double p,r,sum;

void dfs(int d1, int depth)
{
    if(point[d1].is_ch ==0)
    {
        sum+= point[d1].child[0]*pow(r+1,depth)*p;
        return;
    }

    for(int i=0;i<point[d1].child.size();i++)
    {
        int index1 = point[d1].child[i];
        dfs(index1, depth+1);
    }


}



int main()
{
    //init
    scanf("%d%lf%lf", &n, &p, &r);
    r = r/100;
    point.resize(n+10);
    sum = 0.0;

    for(int i=0;i<n;i++)
    {
        scanf("%d", &k);
        if(k==0)
        {
            scanf("%d", &index);
            point[i].is_ch = 0;
            point[i].child.push_back(index);
        }
        else{
            for(int j=0;j<k;j++)
               {
                 scanf("%d", &index);
                 point[i].child.push_back(index);
                 point[i].is_ch=1;
               }
        }
    }
    dfs(0,0);
    printf("%.1f", sum);
    return 0;
}

1090 Highest Price in Supply Chain (25)

tips:

  • vector 型的數組a[n],n給-1,會發生段錯誤…
  • 如果採用保存某個結點的父結點的下標的形式,然後一直遍歷到根結點的深度/廣度優先,會出現三個超時。因爲從葉子結點往上遍歷將會把所有路徑都走一遍,很多都是重複走的路徑,會超時,沒有從根結點往下遍歷的方式快~~
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>

using namespace std;
int n,num,hlen,root;
float p,r,highest;

struct Node
{
    vector<int> pos;
    int depth;
};
vector<Node> tree;


void dfs(int length, int index)
{

    if(tree[index].pos.empty())
    {
        tree[index].depth=length;
        if(length > hlen)
        {
            hlen = length;
            num=1;
        }
        else if(length == hlen)
        {
            num++;
        }
        return;
    }
    for(int j=0;j<tree[index].pos.size();j++)
    {
        dfs(length+1, tree[index].pos[j]);
    }

}

int main()
{
    int temp;
    scanf("%d%f%f", &n, &p, &r);
    r=r/100;
    tree.resize(n);
    for(int i=0;i<n;i++)
       {
           scanf("%d", &temp);
           if(temp==-1) root=i;
           tree[temp].pos.push_back(i);
       }
    hlen = num = 0;
    dfs(0,root);
    highest = pow(1+r,hlen)*p;
    printf("%.2f %d\n",highest,num);
    return 0;
}

發佈了73 篇原創文章 · 獲贊 38 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章