5-2 Path of Equal Weight (30分)/YHF/2016.11.18

**

5-2 Path of Equal Weight (30分)/YHF/2016.11.18

**
Given a non-empty tree with root RR, and with weight W_iW
​i
​​ assigned to each tree node T_iT
​i
​​ . The weight of a path from RR to LL is defined to be the sum of the weights of all the nodes along the path from RR to any leaf node LL.

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 \le 1000

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
class Tnode{
    public:
    int weight;
    Tnode *Father;   //这是指向父亲的树,用来解决儿子数不一致的问题
    int isleafnode;
    //这里注意不要定义构造函数,因为下面new调用Tnode的时候会出错,我猜是因为程序不知道是否要用构造函数。
};
int compare(int *a,int *b);
int main(){
    int node_number;
    int node_number_bare;
    int sum_require;
    int temp;
    int n,t,i,j;
    cin>>node_number>>node_number_bare>>sum_require;
    if(node_number==0)
    return 0;
    Tnode *T=new Tnode[node_number];
    for(i=0;i<node_number;i++)
    cin>>T[i].weight;
    for(i=0;i<node_number;i++){
        T[i].isleafnode=1;
        T[i].Father=NULL;
    }
    int *we_no=new int[node_number*node_number];
        for(i=0;i<node_number*node_number;i++)
        we_no[i]=0;
    for(i=0;i<node_number_bare;i++){
        cin>>temp>>n;
        T[temp].isleafnode=0;//cout<<"T["<<temp<<"].isleaf=0"<<endl ;
        for(j=0;j<n;j++){
            cin>>t;
            T[t].Father=&T[temp];
        }
    }
    //for(i=0;i<node_number;i++)
    //if(T[i].isleafnode==1)
    //cout<<"*";
    int colunm=0;
    int *tt=new int[node_number];
    int sum;j=0;Tnode *search;
    for(i=0;i<node_number;i++){
        if(T[i].isleafnode==1){
            //cout<<"*";
            search=&T[i];j=1;sum=search->weight;tt[0]=search->weight;//cout<<"BEGAN"<<search->weight;
            while(search->Father!= NULL ){
                search=search->Father;//这里的search用来指向根节点然后一层上移
                tt[j]=search->weight;
                j++;
                sum+=search->weight;
                //cout<<"%"<<search->weight;
            }
            if(sum==sum_require){
                sum=j-1;
                while(j!=0){
                    j--;
                we_no[colunm*node_number+sum-j]=tt[j];//必须把路径存起来,因为你永远不知道后面会不会有一个更greater的路径。
                }
                colunm++;
            }
            sum=0;
            for(j=0;j<node_number;j++)
            tt[j]=0;

        }
    }
    delete T;
    delete tt;
    int *tee;
    int* *t1=new int*[colunm];//指向每行第一个元素的指针数组,用来排序
    for(i=0;i<colunm;i++){
        t1[i]=&we_no[node_number*i];
    }
    for(i=0;i<colunm;i++){
        for(j=i;j<colunm;j++){
            if(compare(t1[i],t1[j])==1){
                //cout<<"change"<<i<<"and"<<j<<endl;
            tee=t1[i];
            t1[i]=t1[j];
            t1[j]=tee;  
            }
        }
    }
//  for(i=0;i<node_number*node_number;i++){
//  cout<<we_no[i]<<" ";
//if((i+1)%node_number==0)
//cout<<endl;
//}
for(i=0;i<colunm;i++){
    cout<<*t1[i];
    while(*(t1[i]+1)){
        t1[i]++;
        cout<<" "<<*t1[i];
    }
    cout<<endl;
    delete *t1;
}

}

int compare(int *a,int *b){
    while(*a==*b&&*a!=0){
        a++;b++;
    }
    if((*a)>(*b))
    return 0;
    return 1;
}

感想:第一次用C++写数据结构的题,这个是期末的bonus,贵校dalao请自行绕道,C++里指针什么的实在是不太会用,然后我的DEV C++炸了TAT,无法调试,里面注掉的部分就是假装调试的部分。(可怕我竟然在没有调试的情况下写出来了)
本次收获:1.运用**a
2.bool函数还是不会写,最后比较的时候还是用了int返回类型。
3.变量名称辨识性不强
4.函数运用不多,代码可读性不强
5.为图方便,其实内存的占用是超过限制的。
6.没有用深度优先搜索因为我还不会==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值