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

news/2024/10/19 15:25:39/

想查看其他题的真题及题解的同学可以前往查看: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/news/1427561.html

相关文章

leetcode:438. 找到字符串中所有字母异位词

给定两个字符串 s 和 p&#xff0c;找到 s 中所有 p 的 异位词 的子串&#xff0c;返回这些子串的起始索引。不考虑答案输出的顺序。 异位词 指由相同字母重排列形成的字符串&#xff08;包括相同的字符串&#xff09;。 示例 1: 输入: s "cbaebabacd", p "…

Excel高效办公:人力资源管理(AI版)

AI人力资源管理一本通&#xff1a;147个“温馨提示”53个“教您一招”&#xff0c;掌握使用Excel高效完成人力资源管理工作的“心法”&#xff0c;助你早做完、不加班。 一本书掌握人力资源高效管理的“心法”&#xff01; 案例丰富&#xff0c;参考性强&#xff1a;本书不是…

适配器模式【结构型模式C++】

1.概述 适配器模式是一种结构型设计模式&#xff0c; 又称为变压器模式、包装模式&#xff08;Wrapper&#xff09; 将一个类的接口变换成客户端所期待的另一种接口&#xff0c;从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。 2.结构 Target&#xff1a;适配…

常用的数据结构及算法

一、数据结构 &#xff08;一&#xff09;线性结构&#xff1a;一对一。 1.可以使用数组、链表来表示。数组又分为静态数组和动态数组两种。链表常用的是单链表。 2.两种特殊的线性结构&#xff1a;队列和栈。其中队列是先进先出&#xff08;排队&#xff09;&#xff0c;栈…

linux限权

shell命令以及运行原理 什么是shell命令&#xff1a; 将使用者的命令翻译给核心&#xff08;kernel&#xff09;处理。同时&#xff0c;将核心的处理结果翻译给使用者。 shell就相当于操作系统外的一层外壳 其实就是登录linux时的一个可执行程序&#xff08;进程&#xff09…

了解以太网环网保护倒换(ERPS)

以太网环网保护交换&#xff08;ERPS&#xff09;有助于实现高可靠性和网络稳定性。本文概述了ERPS&#xff0c;包括其定义、基本概念和优点。 什么是ERPS&#xff1f; 以太网环网保护切换&#xff08;ERPS&#xff09;是一种标准化的网络设计方法&#xff0c;旨在确保以太网…

使用 Spring AOP 和 Guava RateLimiter 实现 API 限流

在高并发的应用场景下,合理的限流策略是保证系统稳定性的重要手段之一。限流可以防止系统资源被耗尽,避免雪崩效应的发生。本文将介绍如何使用 Spring AOP 和 Guava RateLimiter 实现API限流,并支持自定义限流超时时间。 引入依赖 首先,需要在 pom.xml 中引入 Guava 依赖: &…

面试题集中营—分布式共识算法

分布式共识算法目标 分布式主要就是为了解决单点故障。一开始只有一个服务节点提供服务&#xff0c;如下图所示。那么如果服务节点挂了&#xff0c;对不起等着吧。 为了服务的高可用性&#xff0c;我们一般都会多引入几个副节点当备份&#xff0c;当服务节点挂了&#xff0c;就…