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

embedded/2024/12/2 13:47:24/

想查看其他题的真题及题解的同学可以前往查看: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];
}

运行结果: 


http://www.ppmy.cn/embedded/20202.html

相关文章

《月球叛军2:烙印之人》AI重做电影宣传片

《月球叛军2&#xff1a;烙印之人》AI重做电影宣传片 In the shadow of the moon, a rebellion ignites. 月影之下&#xff0c;叛军之火熊熊燃起。 Beyond the lunar landscape, a war for freedom unfolds. 月球表面之外&#xff0c;自由之战悄然展开。 From the ashes of t…

2024OD机试卷-分配土地(java\python\c++)

题目:分配土地 题目描述 从前有个村庄,村民们喜欢在各种田地上插上小旗子,旗子上标识了各种 不同的只数字。某天集体村民决定将覆盖相同数字的最小矩阵形的土地分配给村里做出巨大贡献的村民,请问此次分配土地,做出贡献的村民种最大会分配多大面积? 输入描述 第-行输…

40 vue.js

1.1 vue的延迟挂载是什么&#xff1f;$mount是什么&#xff1f; 在vue中&#xff0c;有两种实例化的挂载方式&#xff0c;分别为el和$mount&#xff0c;这两种方式都为手动挂载&#xff0c;而在项目中&#xff0c;可用于延迟挂载&#xff08;比如在挂载之前要做一些操作或者判…

postman汉化

一、postman历史版本下载&#xff1a;Postman 10.24.16 Download for Windows / Old Versions / FileHorse.comhttps://www.filehorse.com/download-postman/old-versions/ 二、汉化包下载&#xff1a; Releases hlmd/Postman-cn GitHubPostman汉化中文版. Contribute to h…

MySQL-用户管理

MySQL 用户分为 普通用户 和 root用户。root用户即超级管理员&#xff0c;拥有所有权限&#xff0c;包含创建&#xff0c;删除和修改用户等相关权限&#xff1b;普通用户只拥有被root用户授予的各种权限MySQL的安全性需要通过账户管理来实现 1、登录MySQL服务器 命令如下&…

OpenHarmony语言基础类库【@ohos.util.Stack (线性容器Stack)】

ohos.util.Stack (线性容器Stack) Stack基于数组的数据结构实现&#xff0c;特点是先进后出&#xff0c;只能在一端进行数据的插入和删除。 Stack和[Queue]相比&#xff0c;Queue基于循环队列实现&#xff0c;只能在一端删除&#xff0c;另一端插入&#xff0c;而Stack都在一…

HarmonyOS 状态管理

在声明式 UI 框架中&#xff0c;数据的改变触发 UI 的重新渲染。在 ArkUI 中不是所有数据的变化都会触发 UI 重新渲染&#xff0c;只有 状态变量 才会引起 UI 重新渲染。 状态变量 状态变量&#xff1a; 指被状态装饰器装饰的变量&#xff0c;只有这种变量的改变才会引起 UI …

制作和合入git补丁

制作git补丁 git log -u a44bc4cf08e94741052cb471512868d14e803f2a -n 1 > log.patch -u显示详细差异 -n日志数量 合入git补丁 git apply log.patch 确保补丁目录与git目录一致