#P4367. [Code+#4] Tommy 的结合
[Code+#4] Tommy 的结合
Description
Life is probably like this. In this materialistic world, people hustle for a living, talk less and less, and grow more distant emotionally. However, the latest research by the renowned scientist Access Globe can solve this: increase the shared topics by increasing the things done simultaneously.
A and B each receive some tasks. Let the sets of tasks they need to execute be and , respectively. For each person, task must be done first, and each other task has a prerequisite task , meaning task can be executed only after is completed. That is, the dependency of each person’s tasks forms a rooted directed tree whose edges point away from the root. A task can be executed if and only if all tasks have been executed. We say task depends on tasks .
Now, A and B hope to complete some tasks together, so they decide to select tasks as follows: A selects tasks with the requirements and, for any , task depends on . Similarly, B selects tasks satisfying the same requirements. Then A can execute tasks along the path from to in order, and B can execute tasks along the path from to in order. Moreover, after scheduling, when A is executing task , B is executing task at the same time. At such moments, A and B can get in touch and increase their intimacy.
The objective is to maximize the intimacy. For each pair of simultaneously executed tasks and , they gain a score of . Meanwhile, once A and B lose contact, their intimacy decreases: in each minute, if it is the -th minute since one side last contacted the other, the intimacy decreases by .
For example, suppose the times for the two people’s tasks are 2, 1, 4, 7 and 4, 8, 3, 6, 4, and they jointly complete only the first and the last tasks. Then A has 1 + 4 = 5 minutes without contact while executing the two middle tasks, which causes an intimacy decrease of ; B has 8 + 3 + 6 = 17 minutes without contact while executing the three middle tasks, which causes an intimacy decrease of . Note that the intimacy calculation depends only on task execution times, and any two tasks can be executed simultaneously as and ; they need not take the same amount of time.
Given A’s and B’s tasks, dependencies, and the execution time of each task, please compute the maximum intimacy that can be achieved.
Input Format
Read from standard input.
- The first line contains two integers .
- The second line contains integers , the duration of each of A’s tasks.
- The third line contains integers , the duration of each of B’s tasks.
- The fourth line contains integers , the prerequisite of each of A’s tasks, with guaranteed.
- The fifth line contains integers , the prerequisite of each of B’s tasks, with guaranteed.
- Then follow lines, each with integers. In the -th row and -th column is , the intimacy gained if A and B simultaneously execute tasks and , respectively. Note that these intimacy scores are not necessarily nonnegative.
Note: the above input does not include information for task , because it is irrelevant to the intimacy calculation.
Output Format
Output to standard output.
Print the maximum achievable intimacy.
5 4
2 1 2 1
1 1 1
1 2 3 4
1 2 2
-8 -1 6
4 -3 7
-7 5 5
-7 5 -5
5
Hint
Sample explanation:
A and B select tasks and , respectively. The simultaneous pairs , , and yield intimacy . A executes task alone for 2 minutes and loses intimacy. Therefore, the final total intimacy is . This is the optimal plan.

Constraints: For all testdata, , , and .
Credit: https://www.luogu.org/discuss/show/38908
Credit: idea and problemsetting/Chen Junkun; problem verification/Tommy > <
Git Repo: https://git.thusaac.org/publish/CodePlus4
Thanks to Tencent for supporting this contest.
Translated by ChatGPT 5
京公网安备 11011102002149号