1053 Path of Equal Weight (30 分)(******)

#include <cstdio>
#include <vector>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int weight;
    vector<int>child;
}Node[110];
int n,m,sum,path[110],index=1;

void preO(int root,int numNode,int weightS)
{
    if(weightS > sum) return;
    else if(weightS == sum)
    {
        if(Node[root].child.size()!=0) return;
        for(int i = 0;i<numNode;i++)
        {
            printf("%d",Node[path[i]].weight);
            if(i<numNode - 1)
                printf(" ");
            else
                printf("\n");
        }
    }

    for(int i = 0;i<Node[root].child.size();i++)
    {
        int child = Node[root].child[i];
        path[numNode] = child;//***
        preO(child,numNode+1,weightS + Node[child].weight);
    }

}

bool cmp(int a,int b)
{
    return Node[a].weight > Node[b].weight;
}

int main()
{
    freopen("in.txt","r",stdin);
    scanf("%d%d%d",&n,&m,&sum);
    for(int i = 0;i<n;i++)
    {
        scanf("%d",&Node[i].weight);
    }
    for(int i = 0;i<m;i++)
    {
        int id,nN;
        scanf("%d %d",&id,&nN);
        Node[id].child.clear();
        for(int j = 0;j<nN;j++)
        {
            int cid;
            scanf("%d",&cid);
            Node[id].child.push_back(cid);//??
        }
        sort(Node[id].child.begin(),Node[id].child.end(),cmp);

    }
    path[0] = 0;
    preO(0,1,Node[0].weight);
}

发布了111 篇原创文章 · 获赞 4 · 访问量 3220

猜你喜欢

转载自blog.csdn.net/qq_15556537/article/details/100176016