CCF-CSP真题《202312-3 树上搜索》思路+c++满分题解

想查看其他题的真题及题解的同学可以前往查看:CCF-CSP真题附题解大全

问题描述

试题编号: 202312-3
试题名称: 树上搜索
时间限制: 1.0s
内存限制: 512.0MB
问题描述:

题目背景

问题描述

输入格式

输出格式

样例1输入

5 2
10 50 10 10 20
1 1 3 3
5
3

样例1输出

2 5
2 5 3 4

样例解释

子任务

真题来源:树上搜索

感兴趣的同学可以如此编码进去进行练习提交

 c++满分题解:

#include <bits/stdc++.h>
using  namespace std;
inline long long read()
{
        long long x = 0, f = 1;
        char c = getchar();
        while (c < '0' || c > '9')
        {
                if (c == '-') f = -1;
                c = getchar();
        }
        while ( c >= '0' && c <= '9')
        {
                x = x * 10 + c - '0';
                c = getchar();
        }
        return x * f;
}

struct Node
{
        long long Weight;
        long long WeightSum;
        long long Index;
        bool IsOut;
        Node* Child, * Brother, * Above, * LastChild, * PrevBrother;
        Node(long long Weigh, long long i) : Weight(Weigh), Child(nullptr), LastChild(nullptr), Above(nullptr), Brother(nullptr), WeightSum(Weigh), PrevBrother(nullptr), Index(i) {}
};

Node* Nodes[2010];

void LinkNode(Node* Parent, Node* Child)
{
        if (!Parent || !Child) return;
        Child->Above = Parent;
        if (!Parent->Child)
        {
                Parent->Child = Child;
                Parent->LastChild = Child;
                return;
        }
        Parent->LastChild->Brother = Child;
        Child->PrevBrother = Parent->LastChild;
        Parent->LastChild = Child;
}


long long AccWeights(Node* CurRoot)
{
        if (!CurRoot) return 0;
        CurRoot->WeightSum = CurRoot->Weight;
        Node* CurNode = CurRoot->Child;
        while (CurNode)
        {
                CurRoot->WeightSum += AccWeights(CurNode);
                CurNode = CurNode->Brother;
        }
        return CurRoot->WeightSum;      
}

bool HasTarIndex(Node* Root, long long Tar)
{
        if (!Root) return 0;
        else if (Root->Index == Tar) return 1;
        Node* CurNode = Root->Child;
        while(CurNode)
        {
                if (HasTarIndex(CurNode, Tar)) return 1;
                CurNode = CurNode->Brother;
        }
        return 0;
}

long long RemovedParents[2010]{-1}, RemovedChilds[2010]{-1};
long long RemovedPairs = 0;
void RemoveNode(Node* TarNode)
{
        if (!TarNode) return;
        if (!TarNode->PrevBrother) TarNode->Above->Child = TarNode->Brother;
        else TarNode->PrevBrother->Brother = TarNode->Brother;
        if (!TarNode->Brother) TarNode->Above->LastChild = TarNode->PrevBrother;
        else TarNode->Brother->PrevBrother = TarNode->PrevBrother;
        TarNode->Above = nullptr;
        TarNode->Brother = nullptr;
        TarNode->PrevBrother = nullptr;
}
void ReBuild()
{
        for (long long i = 0; i < RemovedPairs; ++i) LinkNode(Nodes[RemovedParents[i]], Nodes[RemovedChilds[i]]);
        RemovedPairs = 0;
}
long long GetDelta(Node* TarNode, long long& CurTotalWeight)
{
        if(!TarNode) return 114514;
        else if (!TarNode->Above) return TarNode->WeightSum;
        return abs(CurTotalWeight - 2 * TarNode->WeightSum);
}
long long CurMin = 0, CurIdx = 0;
void Bianli(Node* CurRoot, long long& CurTotalWeight)
{
        if (!CurRoot) return;
        long long Delta = GetDelta(CurRoot, CurTotalWeight);
        //cout << CurRoot->Index << ' '<<Delta<<'\n';
        if (Delta < CurMin || (CurMin == Delta && CurRoot->Index < CurIdx))
        {
                CurMin = Delta;
                CurIdx = CurRoot->Index;
        }
        Node* CurNode = CurRoot->Child;
        while (CurNode)
        {
                Bianli(CurNode, CurTotalWeight);
                CurNode = CurNode->Brother;
        }
}
void PrintTree(Node* CurRoot)
{
        if (!CurRoot) return;
        cout << CurRoot->Index << '\n';
        Node* CurNode = CurRoot->Child;
        while (CurNode)
        {
                cout << '\t' << CurNode->Index << '\n';
                system("pause");
                CurNode = CurNode->Brother;
        }
        CurNode = CurRoot->Child;
        while (CurNode)
        {
                PrintTree(CurNode);
                CurNode = CurNode->Brother;
        }
}
int main()
{
        long long n = read(), m = read();
        for (long long i = 1; i<= n; ++i) Nodes[i] = new Node(read(), i);
        for (long long i = 2; i <= n; ++i) LinkNode(Nodes[read()], Nodes[i]);
        long long Index = 0;
        for (long long i = 1; i <= m; ++i)
        {
                RemovedPairs = 0;
                Index = read();
                Node* CurRoot = Nodes[1];
                while(CurRoot->Child)
                {
                        /*
                        cout << "___________________\n";
                        PrintTree(CurRoot);
                        cout << "___________________\n";
                        */
                        long long SumWeight = AccWeights(CurRoot);
                        CurMin = SumWeight, CurIdx = 0;
                        Bianli(CurRoot, SumWeight);
                        cout << CurIdx << ' ';
                        if (HasTarIndex(Nodes[CurIdx], Index)) CurRoot = Nodes[CurIdx];
                        else
                        {
                                RemovedParents[RemovedPairs] = Nodes[CurIdx]->Above->Index;
                                RemovedChilds[RemovedPairs] = Nodes[CurIdx]->Index;
                                ++RemovedPairs;
                                RemoveNode(Nodes[CurIdx]);
                        }
                }
                ReBuild();
                cout << '\n';
        }
        for (long long i = 1; i<= n; ++i) delete Nodes[i];
}

运行结果: 

相关推荐

  1. CCF-CSP 201809-2 买菜 C++满分题解

    2024-04-21 06:46:05       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 06:46:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 06:46:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 06:46:05       87 阅读
  4. Python语言-面向对象

    2024-04-21 06:46:05       96 阅读

热门阅读

  1. web自动化测试

    2024-04-21 06:46:05       38 阅读
  2. 【grpc】grpc进阶三,服务发现

    2024-04-21 06:46:05       33 阅读
  3. 【PHP快速上手(十三)】

    2024-04-21 06:46:05       37 阅读
  4. 计算机视觉入门

    2024-04-21 06:46:05       38 阅读
  5. Spring Boot集成atomikos快速入门Demo

    2024-04-21 06:46:05       29 阅读
  6. 数据分析(3)

    2024-04-21 06:46:05       29 阅读
  7. SonarQube 安装、部署及配置使用

    2024-04-21 06:46:05       33 阅读
  8. Dispatch架构

    2024-04-21 06:46:05       36 阅读
  9. MySQL 全表扫描成本计算

    2024-04-21 06:46:05       32 阅读
  10. Lustre架构介绍的阅读笔记-客户端

    2024-04-21 06:46:05       39 阅读
  11. Context Vector与Representation Learning

    2024-04-21 06:46:05       41 阅读
  12. 27、Lua 学习笔记之五(Lua中的数学库)

    2024-04-21 06:46:05       37 阅读