#P9597. [JOI Open 2018] 猫或狗 / Cats or Dogs
[JOI Open 2018] 猫或狗 / Cats or Dogs
Background
Special reminder: due to Luogu’s special interaction mechanism, you cannot include catdog.h in your program. You only need to implement the required functions.
Problem Description
Your son, JOI, likes keeping pets. In your garden, there are huts for keeping pets, numbered from to . There are undirected paths connecting these huts, and for any two huts, it is possible to move between them along some paths. Each hut can contain at most one pet.
JOI wants to keep cats and dogs, but he is worried that the pets may often fight. For each of the following states of the huts: having a cat, having a dog, or being empty, he defines the garden’s danger level as follows:
- For every cat and every dog, the minimum number of paths that need to be blocked so that they cannot meet via unblocked paths.
After defining the danger level, JOI starts to make a plan for using the garden over the next days. Initially, all huts are empty. The plan for day is one of the following:
- Keep a cat in an empty hut .
- Keep a dog in an empty hut .
- Give the pet in hut to a neighbor, meaning hut becomes empty.
As a parent, you are responsible for checking whether your son’s plan is dangerous. More precisely, you need to compute the danger level of the garden after each day’s plan is carried out, for all days.
To answer queries online, this problem is judged in an interactive manner.
You need to implement four functions: initialize, cat, dog, and neighbor.
At the beginning, the function initialize is called. This function receives the garden information.
initialize(N, A, B)- : the number of huts in the garden.
- : arrays of length . This means that for , there is a path between huts and . It is guaranteed that for any two different huts, it is possible to move between them along some paths.
Then, for the plans over these days, the following functions are called in chronological order:
cat(v): call this function to keep a cat in an empty hut .dog(v): call this function to keep a dog in an empty hut .neighbor(v): call this function to make the pet in hut leave.
These functions should return the garden’s danger value after the plan is executed.
Submissions in Java and Pascal are currently not supported.
Input Format
The sample interactor reads input in the following format:
The first line contains an integer .
The next lines each contain two integers .
The next line contains an integer .
The next lines each contain two integers .
Here, if the call on day is , then ; if it is , then ; if it is , then .
Output Format
The sample interactor outputs the function call result for day in the following format:
On the -th line, output .
3
1 2
2 3
4
1 1
1 3
2 2
3 2
0
0
2
0
5
1 2
2 3
2 4
4 5
5
1 3
2 5
1 2
2 1
3 2
0
1
1
2
1
Hint
Sample
Consider the case with huts and paths. These four paths connect hut and hut , hut and hut , hut and hut , and hut and hut .
- Suppose he first keeps a cat in hut and a dog in hut . By blocking the path between hut and hut , he can prevent the cat and the dog from meeting. Therefore, the danger level at this time is .
- Suppose he then keeps a new cat in hut and a new dog in hut . By blocking the path between hut and hut and the path between hut and hut , he can prevent cats and dogs from meeting. Therefore, the danger level at this time is .
- Suppose he finally gives the cat in hut to a neighbor. He only needs to block the path between hut and hut . Therefore, the danger level at this time is .
Constraints
This problem has three subtasks. The score and additional constraints for each subtask are shown in the table below:
| Subtask ID | Score | ||
|---|---|---|---|
Translated by ChatGPT 5