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

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

#include 
#include 
#include 
#include 
#include 

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

你可能感兴趣的:(CCF刷题,c语言,开发语言)