Codeforces Round #756 (Div. 3)
比赛链接
Codeforces Round #756 (Div. 3)
E2. Escape The Maze (hard version)
Vlad built a maze out of rooms and bidirectional corridors. From any room any other room can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.
Vlad invited friends to play a game with them.
Vlad starts the game in the room 1 and wins if he reaches a room other than 1 , into which exactly one corridor leads. Friends are placed in the maze: the friend with number is in the room , and no two friends are in the same room (that is, for all ). Friends win if one of them meets Vlad in any room or corridor before he wins.
For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.
Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad.
In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist.
Input
The first line of the input contains an integer - the number of test cases in the input. The input contains an empty string before each test case.
The first line of the test case contains two numbers and - the number of rooms and friends, respectively.
The next line of the test case contains integers - numbers of rooms with friends. All are different.
The next lines contain descriptions of the corridors, two numbers per line and - numbers of rooms that connect the corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors.
It is guaranteed that the sum of the values over all test cases in the test is not greater than .
Output
Print lines, each line containing the answer to the corresponding test case. The answer to a test case should be if Vlad wins anyway and a minimal number of friends otherwise.
Example
input
output
解题思路
dfs,倍增
如果存在某个节点上,则根节点到不了以节点为根的子树的任意一个叶子节点上,将当前所有存在的节点都向上走到极限位置上,即根节点向下走的同时能够与当前节点相遇的位置,即假设当前节点深度为 ,根节点深度为 ,则极限位置的深度为 ,可以倍增求解,最后从根节点 一遍即可,即遇到极限位置则答案加一,否则如果到达叶子节点则答案为
- 时间复杂度:
代码
F. ATM and Students
Polycarp started working at a bank. He was assigned to monitor the ATM. The ATM initially contains rubles.
A queue of students lined up to him. Each student wants to either withdraw a certain amount of money or deposit it into an account. If is positive, then the student credits that amount of money via ATM. Otherwise, the student withdraws rubles.
In the beginning, the ATM is turned off and an arbitrary number of students are not served. At some point, Polycarp turns on the ATM, which has an initial amount of rubles. Then, the remaining students start queueing at the ATM. If at some point in time there is less money in the ATM than the student wants to withdraw, then the student is not served and Polycarp turns off the ATM and does not turn it on anymore.
More formally, the students that are served are forming a contiguous subsequence.
Polycarp wants the ATM to serve the maximum number of students. Help him in this matter. Print the numbers of the first and last student, or determine that he will not be able to serve anyone.
In other words, find such a longest continuous segment of students that, starting with the sum of at the ATM, all these students will be served. ATM serves students consistently (i.e. one after another in the order of the queue).
Input
The first line of the input contains one integer - the number of test cases.
Each test case consists of two lines. The first one contains integers and the length of the array and the initial amount of rubles in the ATM. The second contains integers elements of the array. Note that can be zero.
It is guaranteed that the sum of the values over all test cases does not exceed .
Output
Print lines, each line must contain the answer to the corresponding set of input data: if the answer exists, print the numbers of the first and last served student. If there is no solution, then print on the line.
If there are several possible answers, print any.
Example
input
output
解题思路
分治
假设开始时权值为 ,从 开始,到 结束,即 ,即 ,即 ,其中 为 的右边第一个满足此不等式的下标,即要找 的左边第一个 大于 的 ,其贡献为 ,可用线段树求解,当然这里没有修改操作,直接转换为 求解即可
- 时间复杂度:
代码
__EOF__

本文链接:https://www.cnblogs.com/zyyun/p/16257488.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!