CCF题库刷题(C语言)---树上搜索

树上搜索
树上搜索
很遗憾挑战失败,在VScode以及Dev-CPP5.11中均能正常运行,但是在模拟测试提交时全部显示编译失败,这也算是一个大坑了吧。。。图中编译没失败的是没有用指针和动态分配时的,后来改用指针和动态分配malloc函数后,就一直失败。这里有一个成功的,但是是用C++写的。https://blog.csdn.net/qq_51594008/article/details/135006254
在这里插入图片描述

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct item
{
    int id;
    int parentID;
    int weight;
    int sign;
    int weightgap;
} item;


bool IsChilditem(item items[], int parentID, int childID, int size)
{
    int *Queue = malloc(sizeof(int) * 2000);
    int front = -1, rear = -1;
    Queue[++rear] = parentID;
    if (Queue[front + 1] == childID)
    {
        free(Queue);
        Queue = NULL;
        return true;
    }
    while (front < rear)
    {
        for (int i = 0; i < size; i++)
        {
            if (items[i].parentID == Queue[front + 1])
            {
                Queue[++rear] = items[i].id;
                if (Queue[front + 1] == childID)
                {
                    free(Queue);
                    Queue = NULL;
                    return true;
                }
            }
        }
        front++;
    }
    free(Queue);
    Queue = NULL;
    return false;
}


void GetChilditems(item items[], int parentID, int childitems[], int size)
{
    int *Queue = malloc(sizeof(int) * 2000);
    int front = -1, rear = -1;
    Queue[++rear] = parentID;
    int num = 0;
    while (front < rear)
    {
        for (int i = 0; i < size; i++)
        {
            if (items[i].parentID == Queue[front + 1])
            {
                childitems[num++] = items[i].id;
            }
        }
        front++;
    }
    childitems[num] = 0;
    free(Queue);
    Queue = NULL;
}


void CalculateWeightGap(item items[], int size)
{

    int weightsall = 0;
    for (int i = 0; i < size; i++)
    {
        if (items[i].sign == 1)
        {
            weightsall += items[i].weight;
        }
    }
    int *queue = malloc(sizeof(int) * 2000);
    int front = -1, rear = -1;

    for (int i = 0; i < size; i++)
    {
        if (items[i].sign == 0)
        {
            continue;
        }

        items[i].weightgap = 0;
        int weightsSum = items[i].weight;
        queue[++rear] = items[i].id;

        while (front < rear)
        {
            for (int j = 0; j < size; j++)
            {
                if (items[j].sign == 0)
                    continue;
                if (items[j].parentID == queue[front + 1])
                {
                    weightsSum += items[j].weight;
                    queue[++rear] = items[j].id;
                }
            }
            front++;
        }
        items[i].weightgap = abs(2 * weightsSum - weightsall);
    }
    free(queue);
    queue = NULL;
}


int CountSignItems(item items[], int size)
{
    int count = 0;
    for (int i = 0; i < size; i++)
    {
        if (items[i].sign == 1)
        {
            count++;
        }
    }
    return count;
}

int main()
{

    int n, m;
    scanf("%d %d", &n, &m);
    item *items = malloc(sizeof(item) * n);
    int *weights = malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &weights[i]);
    }
    int *parentIDs = malloc(sizeof(int) * n);
    parentIDs[0] = 0;
    for (int i = 1; i < n; i++)
    {
        scanf("%d", &parentIDs[i]);
    }

    int *tobetesteddata = malloc(sizeof(int) * m);
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &tobetesteddata[i]);
    }

    int weightsall = 0;
    for (int i = 0; i < n; i++)
    {
        items[i].id = i + 1;
        items[i].weight = weights[i];
        items[i].parentID = parentIDs[i];
        items[i].sign = 1;
        weightsall += weights[i];
    }

    CalculateWeightGap(items, n);

    int **result;
    result = (int **)malloc(m * sizeof(int *));
    for (int i = 0; i < m; i++)
    {
        result[i] = (int *)malloc(n * sizeof(int));
    }

    for (int d = 0; d < m; d++)
    {
        int number = 0;
        while (CountSignItems(items, n) > 1)
        {
            int min = 100000000;
            int index = 0;
            for (int i = 0; i < n; i++)
            {
                if (items[i].sign == 0)
                    continue;
                if (items[i].weightgap < min)
                {
                    min = items[i].weightgap;
                    index = i + 1;
                }
            }
            result[d][number++] = index;
            int *childitems=malloc(sizeof(int)*2000);
            GetChilditems(items, index, childitems, n);

            if (IsChilditem(items, index, tobetesteddata[d], n))
            {
                for (int i = 0; i < n; i++)
                {
                    bool flag = false;
                    for (int j = 0; childitems[j] != 0; j++)
                    {
                        if (items[i].id == childitems[j])
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        items[i].sign = 0;
                    }
                }
                items[index - 1].sign = 1;
                CalculateWeightGap(items, n);
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    bool flag = false;
                    for (int j = 0; childitems[j] != 0; j++)
                    {
                        if (items[i].id == childitems[j])
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        items[i].sign = 0;
                    }
                }
                items[index - 1].sign = 0;
                CalculateWeightGap(items, n);
            }
        }
        for (int i = 0; i < number - 1; i++)
        {
            printf("%d ", result[d][i]);
        }
        printf("%d\n", result[d][number - 1]);
        for (int i = 0; i < n; i++)
        {
            items[i].sign = 1;
        }
    }
    free(items);
    items = NULL;
    free(weights);
    weights = NULL;
    free(parentIDs);
    parentIDs = NULL;
    free(tobetesteddata);
    tobetesteddata = NULL;
    for (int i = 0; i < m; i++)
    {
        free(result[i]);
        result[i] = NULL;
    }
    free(result);
    result = NULL;
    return 0;
}
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝凡FR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值