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);
}

上一篇:【Leetcode Medium】1318. Minimum Flips to Make a OR b Equal to c


下一篇:【CF1206B Make Product Equal One】