#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 NN huts for keeping pets, numbered from 11 to NN. There are N1N-1 undirected paths connecting these NN 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 QQ days. Initially, all huts are empty. The plan for day ii is one of the following:

  • Keep a cat in an empty hut vv.
  • Keep a dog in an empty hut vv.
  • Give the pet in hut vv to a neighbor, meaning hut vv 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 QQ 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)
    • NN: the number of huts in the garden.
    • A,BA, B: arrays of length N1N-1. This means that for 0iN20 \le i \le N-2, there is a path between huts AiA_i and BiB_i. 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 QQ days, the following functions are called in chronological order:

  • cat(v): call this function to keep a cat in an empty hut vv.
  • dog(v): call this function to keep a dog in an empty hut vv.
  • neighbor(v): call this function to make the pet in hut vv 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 NN.

The next N1N-1 lines each contain two integers Ai,BiA_i, B_i.

The next line contains an integer QQ.

The next QQ lines each contain two integers Tj,vjT_j, v_j.

Here, if the call on day jj is cat\texttt{cat}, then Tj=1T_j = 1; if it is dog\texttt{dog}, then Tj=2T_j = 2; if it is neighbor\texttt{neighbor}, then Tj=3T_j = 3.

Output Format

The sample interactor outputs the function call result DjD_j for day jj in the following format:

On the jj-th line, output DjD_j.

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 55 huts and 44 paths. These four paths connect hut 11 and hut 22, hut 22 and hut 33, hut 22 and hut 44, and hut 44 and hut 55.

  1. Suppose he first keeps a cat in hut 33 and a dog in hut 55. By blocking the path between hut 22 and hut 44, he can prevent the cat and the dog from meeting. Therefore, the danger level at this time is 11.
  2. Suppose he then keeps a new cat in hut 22 and a new dog in hut 11. By blocking the path between hut 22 and hut 44 and the path between hut 11 and hut 22, he can prevent cats and dogs from meeting. Therefore, the danger level at this time is 22.
  3. Suppose he finally gives the cat in hut 22 to a neighbor. He only needs to block the path between hut 22 and hut 33. Therefore, the danger level at this time is 11.

Constraints

This problem has three subtasks. The score and additional constraints for each subtask are shown in the table below:

Subtask ID Score NN QQ
11 88 1N151 \le N \le 15 1Q1001 \le Q \le 100
22 3030 1N1 0001 \le N \le 1\ 000 1Q1 0001 \le Q \le 1\ 000
33 6262 1N100 0001 \le N \le 100\ 000 1Q100 0001 \le Q \le 100\ 000

Translated by ChatGPT 5