#P10437. [JOIST 2024] JOI 之旅 / JOI Tour
[JOIST 2024] JOI 之旅 / JOI Tour
Background
When submitting, please do not include joitour.h.
Please do not submit using C++14 (GCC 9).
Problem Description
In the country of IOI, there are cities, numbered from to , and roads, numbered from to . Road connects city and city bidirectionally. You can travel from one city to any other city via roads.
Each city in the country of IOI has a restaurant. The restaurant type in city is denoted by . Specifically:
- : juice shop.
- : Japanese omelette shop.
- : ice cream shop.
Ms. Rie is a tour guide in the country of IOI. She is planning a sightseeing program called the JOI Tour. In the JOI Tour, the plan is to visit the following types of restaurants in this order:
- Choose a city that has a juice shop, and start the trip from city .
- Visit the juice shop in city .
- Choose a city that has a Japanese omelette shop, and take a bus from city to city along a shortest path.
- Visit the Japanese omelette shop in city .
- Choose a city that has an ice cream shop, and take a bus from city to city along a shortest path.
- Visit the ice cream shop in city .
- End the trip in city .
To avoid boring the tourists, Ms. Rie decides to choose three cities such that they do not pass through the same road twice. We call such a JOI Tour a good one. To help her find an ideal plan, you are asked to compute the number of good JOI Tours. In other words, you need to find the number of triples that satisfy all of the following conditions:
- The restaurant in city is a juice shop.
- The restaurant in city is a Japanese omelette shop.
- The restaurant in city is an ice cream shop.
- While moving along shortest paths from city to , and then from to , you do not pass through the same road twice.
In the country of IOI, there will be events where restaurant types change. When the -th event happens, two integers and are given, satisfying and . Then, the restaurant type in city becomes . That is, when , the restaurant type becomes a juice shop, a Japanese omelette shop, and an ice cream shop, respectively. After each event, you should immediately compute the latest number of good JOI Tours and tell Ms. Rie.
Given the roads and restaurant information, compute the number of good JOI Tours after each type-change event.
Implementation Details
You need to implement the following functions.
void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V, int Q)- Use this function to provide the road and restaurant information.
- This function is called only once at the beginning of the program.
- The parameter
Nis the number of cities . - The parameter
Fis an array of length .F[i]() represents the restaurant type in city , i.e. . - The parameters
UandVare arrays of length .U[j]andV[j]() are the two cities and connected by road . - The parameter
Qis the number of restaurant type-change events .
void change(int X, int Y)- Use this function to provide a restaurant type-change event.
- This function is called times.
- In the -th call, the parameter
Xis the city index where the change happens, i.e. . - In the -th call, the parameter
Yis the new restaurant type, i.e. . It is guaranteed that the new type is different from the old type.
long long num_tours()- This function is called in the following situations, for a total of times.
- After executing the function
init. - After executing the function
change.
- After executing the function
- This function should return the latest number of good JOI Tours.
- This function is called in the following situations, for a total of times.
Input Format
The following is the input format of the provided grader. You should not read any information from standard input.
The first line contains an integer .
The second line contains integers .
The next lines each contain two integers .
The next line contains an integer .
The next lines each contain two integers .
Output Format
The following is the output format of the provided grader. You should not print any information to standard output.
After each call to the function num_tours, the provided grader outputs one line containing an integer, which is the return value of this function.
3
0 1 2
0 1
1 2
0
1
3
0 1 2
0 1
1 2
2
2 0
0 2
1
0
1
7
1 0 2 2 0 1 0
0 1
0 2
1 3
1 4
2 5
2 6
7
0 0
1 1
2 0
3 0
4 2
5 2
6 2
3
0
4
4
0
4
5
5
Hint
Sample Explanation 1
| Function Call | Return Value |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 0) |
|
num_tours() |
There is exactly one good JOI Tour, which is . Below is an explanation of why it satisfies the conditions of a good JOI Tour.
- , so the restaurant in city is a juice shop.
- , so the restaurant in city is a Japanese omelette shop.
- , so the restaurant in city is an ice cream shop.
- When we move along shortest paths from city to , and then from to , we do not pass through the same road twice.
Therefore, the return value of the first call to num_tours() is .
This sample satisfies the constraints of subtasks .
Sample Explanation 2
| Function Call | Return Value |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 2) |
|
num_tours() |
|
change(2, 0) |
|
num_tours() |
|
change(0, 2) |
|
num_tours() |
Initially, there is one good JOI Tour, which is . Therefore, the return value of the first call to num_tours() is .
In the first event, the restaurant in city changes from an ice cream shop to a juice shop. After this change, ice cream shops disappear from the country of IOI, so there are no good JOI Tours anymore. Therefore, the return value of the second call to num_tours() is .
In the second event, the restaurant in city changes from a juice shop to an ice cream shop. After this change, there is one good JOI Tour, which is . Therefore, the return value of the third call to num_tours() is .
This sample satisfies the constraints of subtasks .
Sample Explanation 3
This sample satisfies the constraints of subtasks .
Important Notes
- Your program may implement other functions for internal use, or use global variables.
- Your program is not allowed to use standard input/output. Your program is not allowed to interact with other files in any way. However, your program may output debugging information to standard error output.
Compilation and Test Run
You can download the sample interactor in “File - Additional Files” to test your program. The additional files also include a sample program.
The sample interactor is the file grader.cpp. To test your program, put grader.cpp, joitour.cpp, and joitour.h in the same directory, and compile with the following command. You may also run compile.sh to compile your program.
g++ -std=gnu++20 -O2 -o grader grader.cpp joitour.cpp
If compilation succeeds, an executable file grader will be generated.
Note that the interactor used for judging is different from the sample interactor. The sample interactor runs as a single process: it reads input from standard input and outputs results to standard output.
Constraints
- .
- .
- .
- It is possible to travel from any city to any other city via roads.
- 。
- 。
- 。
- For each call to the function
change, the new type is different from the old type.
Subtasks
- (6 points) , .
- (8 points) , .
- (6 points) .
- (16 points) .
- (16 points) $U_j=\lfloor\frac{j}{2}\rfloor,V_j=j+1\ (0\le j\le N-2)$.
- (34 points) , .
- (14 points) No additional constraints.
Translated by ChatGPT 5