/
実行時間制限: 3 sec / メモリ制限: 1024 MiB
Submission on AtCoder
When using C++
Your program must include
joitour.h. See the problem statement below for the implementation details.When using other languages
Use the following input and output format.
init
Read input in the following format.
N F_0 \cdots F_{N - 1} U_0 V_0 \vdots U_{N - 2} V_{N - 2} Qchange
Read input in the following format.
X Ynum_tours
Print the answer on one line.
Other notices
- Flush the standard output after every output.
Score: 100 points
Problem Statement
In IOI country, there are N towns which are numbered from 0 to N - 1, and N - 1 roads which are numbered from 0 to N - 2. Road j (0 \le j \le N - 2) connects town U_j and town V_j bidirectionally. You can move between any pair of towns by traversing some roads.
There is a restaurant in each town of IOI country. The type of restaurant at town i (0 \le i \le N - 1) is represented by an integer F_i, which corresponds to:
- F_i = 0: Juice restaurant
- F_i = 1: Omelette restaurant
- F_i = 2: Ice cream restaurant
Rie is a tour guide in IOI country, who plans a sightseeing tour named JOI Tour. JOI Tour is a tour to visit 3 types of restaurants in a following way:
- Choose a town i_0 with juice restaurant (0 \le i_0 \le N - 1), and start the tour at town i_0.
- Visit the juice restaurant at town i_0.
- Choose a town i_1 with omelette restaurant (0 \le i_1 \le N - 1), and move from town i_0 to town i_1 along the roads by bus, using the shortest route.
- Visit the omelette restaurant at town i_1.
- Choose a town i_2 with ice cream restaurant (0 \le i_2 \le N - 1), and move from town i_1 to town i_2 along the roads by bus, using the shortest route.
- Visit the ice cream restaurant at town i_2.
- Finish the tour at town i_2.
To avoid customers getting bored, Rie decided to choose three towns i_0, i_1, i_2 so that they don't pass the same road twice. We call such JOI tour good. In order to help her finding the ideal tour plan, you are asked to compute the number of good JOI tours. In other words, you should find the number of tuples (i_0, i_1, i_2) which meets all of the following conditions:
- The restaurant at town i_0 is a juice restaurant.
- The restaurant at town i_1 is an omelette restaurant.
- The restaurant at town i_2 is an ice cream restaurant.
- When we move from town i_0 to town i_1 then from town i_1 to town i_2, both using the shortest routes, we don't pass the same road twice.
In IOI country, there will be Q events involving change of restaurant type. When the (k + 1)-th event (0 \le k \le Q - 1) happens, two integers X_k and Y_k will be given to you, which holds 0 \le X_k \le N - 1 and 0 \le Y_k \le 2. Then, the type of the restaurant at town X_k is changed to the type represented by integer Y_k. That is, when Y_k = 0, 1, 2, it is changed to juice, omelette, ice cream restaurant, respectively. After each event, you should immediately compute the up-to-date number of good JOI tours and tell the result to Rie.
Write a program which, given information of roads and restaurants, computes the number of good JOI tours after each event of change of restaurant type.
Implementation Details
You need to submit one file.
The file is joitour.cpp. The program should include joitour.h using the preprocessing directive #include.
In joitour.cpp, the following functions should be implemented.
-
void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V, int Q)- With this function call, the information of roads and restaurants is given.
- This function is called only once, at the beginning.
- The parameter
Nis the number of towns N. - The parameter
Fis an array of length N.F[i](0 \le i \le N - 1) represents the type of restaurant at town i, that is, F_i. - The parameters
UandVare arrays of length N - 1.U[j]andV[j](0 \le j \le N - 2) are two towns connected by road j, that is, U_j and V_j. - The parameter
Qis the number of events of change of restaurant type, that is, Q.
-
void change(int X, int Y)- With this function call, the information of change of restaurant type is given.
- This function is called Q times.
- In (k + 1)-th call of this function (0 \le k \le Q - 1), the parameter
Xis the index of town that change of restaurant type happens, that is, X_k. - In (k + 1)-th call of this function (0 \le k \le Q - 1), the parameter
Yrepresents the new type of restaurant, that is, Y_k. It is guaranteed that new type is different from the old type.
-
long long num_tours()- This function is called in the following occasions, totalling Q + 1 times.
- Right after the execution of function
init - Right after the execution of function
change
- Right after the execution of function
- This function should return the up-to-date number of good JOI tours.
- This function is called in the following occasions, totalling Q + 1 times.
Important Notices
- Your program can implement other functions for internal use, or use global variables.
- Your program must not use the standard input and the standard output. Your program must not communicate with other files by any methods. However, your program may output debugging information to the standard error.
Compilation and Test Run
You can download an archive file from the contest webpage which contains the sample grader to test your program. The archive file also contains a sample source file of your program.
The sample grader is the file grader.cpp. In order to test your program, put grader.cpp, joitour.cpp, joitour.h in the same directory, and run the following command to compile your programs. Instead, you may run compile.sh contained in the archive file.
g++ -std=gnu++20 -O2 -o grader grader.cpp joitour.cpp
When the compilation succeeds, the executable file grader is generated.
Note that the actual grader is different from the sample grader. The sample grader will be executed as a single process, which will read input data from the standard input and write the results to the standard output.
Input for the Sample Grader
The sample grader reads the following data from the standard input.
N
F_0 F_1 \cdots F_{N - 1}
U_0 V_0
U_1 V_1
\vdots
U_{N - 2} V_{N - 2}
Q
X_0 Y_0
X_1 Y_1
\vdots
X_{Q - 1} Y_{Q - 1}
Output of the Sample Grader
The sample grader outputs the return value of function num_tours in one line to the standard output, after every call of this function.
Constraints
- 3 \le N \le 200\,000.
- 0 \le F_i \le 2 (0 \le i \le N - 1).
- 0 \le U_j < V_j \le N - 1 (0 \le j \le N - 2).
- You can move between any pair of towns by traversing some roads.
- 0 \le Q \le 50\,000.
- 0 \le X_k \le N - 1 (0 \le k \le Q - 1).
- 0 \le Y_k \le 2 (0 \le k \le Q - 1).
- For each call to the function
change, the new type is different from the old type. - Given values are all integers.
Subtasks
- (6 points) N \le 400, Q \le 100.
- (8 points) N \le 4\,000, Q \le 1\,000.
- (6 points) Q = 0.
- (16 points) U_j = j, V_j = j + 1 (0 \le j \le N - 2).
- (16 points) U_j = \left\lfloor \frac{j}{2} \right\rfloor, V_j = j + 1 (0 \le j \le N - 2). (\left\lfloor \frac{j}{2} \right\rfloor denotes the largest integer not exceeding \frac{j}{2}.)
- (34 points) N \le 100\,000, Q \le 25\,000.
- (14 points) No additional constraints.
Sample Communication
Here is a sample input for the sample grader and corresponding function calls.
Sample Input 1
3 0 1 2 0 1 1 2 0
Sample Output 1
1
| Function Calls | Return Values |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 0) |
|
num_tours() |
1 |
There are only one good JOI tour, which is represented by (i_0, i_1, i_2) = (0, 1, 2). The following is the explanation that it meets the conditions of good JOI tours.
- Since F_0 = 0, the restaurant at town 0 is a juice restaurant.
- Since F_1 = 1, the restaurant at town 1 is an omelette restaurant.
- Since F_2 = 2, the restaurant at town 2 is an ice cream restaurant.
- When we move from town 0 to town 1 and then from town 1 to town 2, both using the shortest routes, we don't pass the same road twice.
Therefore, the return value in the first call of num_tours should be 1.
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 6, 7.
Sample Input 2
3 0 1 2 0 1 1 2 2 2 0 0 2
Sample Output 2
1 0 1
| Function Calls | Return Values |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 2) |
|
num_tours() |
1 |
change(2, 0) |
|
num_tours() |
0 |
change(0, 2) |
|
num_tours() |
1 |
Initially, there is one good JOI tour, which is represented by (i_0, i_1, i_2) = (0, 1, 2). Thus, the return value in the first call of num_tours should be 1.
In the first event, the restaurant at town 2 is changed from ice cream restaurant to juice restaurant. After the change, ice cream restaurants disappear from IOI country, and there are no good JOI tours. Thus, the return value in the second call of num_tours should be 0.
In the second event, the restaurant at town 0 is changed from juice restaurant to ice cream restaurant. After the change, there is one good JOI tour, which is represented by (i_0, i_1, i_2) = (2, 1, 0). Thus, the return value in the third call of num_tours should be 1.
This sample input satisfies the constraints of Subtasks 1, 2, 4, 6, 7.
Sample Input 3
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
Sample Output 3
3 0 4 4 0 4 5 5
This sample input satisfies the constraints of Subtasks 1, 2, 5, 6, 7.
AtCoder での提出方法
C++ を使用する場合
C++ を使用する場合,
joitour.hを include してください.実装の詳細は以下の問題文を参照してください.その他の言語を使用する場合
以下の形式で入出力を行ってください.
init
以下の形式で入力を受け取ってください.
N F_0 \cdots F_{N - 1} U_0 V_0 \vdots U_{N - 2} V_{N - 2} Qchange
以下の形式で入力を受け取ってください.
X Ynum_tours
答えを 1 行に出力してください.
その他
- 各出力の後には必ず flush をしてください.
配点 : 100 点
問題文
IOI 国には N 個の街があり,0 から N - 1 までの番号が付けられている.また,IOI 国には N - 1 本の道路があり,0 から N - 2 までの番号が付けられている.道路 j (0 \leqq j \leqq N - 2) は街 U_j と街 V_j を双方向に結んでいる.どの街からどの街へも何本かの道路を通ることによって移動することができる.
IOI 国のそれぞれの街には飲食店が 1 店舗あり,街 i (0 \leqq i \leqq N - 1) にある飲食店の種類は整数 F_i によって表される.具体的には,
- F_i = 0 のとき,街 i にある飲食店はジュース専門店である.
- F_i = 1 のとき,街 i にある飲食店はオムレツ専門店である.
- F_i = 2 のとき,街 i にある飲食店はアイスクリーム専門店である.
IOI 国のツアーガイドである理恵さんは,JOI ツアーという観光ツアーを企画している.JOI ツアーとは,以下のような 3 種類の飲食店を巡るツアーである.
- ジュース専門店のある街 i_0 (0 \leqq i_0 \leqq N - 1) を選び,街 i_0 から JOI ツアーを開始する.
- 街 i_0 にあるジュース専門店を訪れる.
- オムレツ専門店のある街 i_1 (0 \leqq i_1 \leqq N - 1) を選び,バスで街 i_0 から街 i_1 へ最短経路で移動する.
- 街 i_1 にあるオムレツ専門店を訪れる.
- アイスクリーム専門店のある街 i_2 (0 \leqq i_2 \leqq N - 1) を選び,バスで街 i_1 から街 i_2 へ最短経路で移動する.
- 街 i_2 にあるアイスクリーム専門店を訪れる.
- 街 i_2 で JOI ツアーを終了する.
理恵さんは,参加者を飽きさせないために,ツアー中同じ道路を複数回通らないように 3 つの街 (i_0, i_1, i_2) を選ぶことにした.そのような JOI ツアーを良い JOI ツアーと呼ぶことにする.あなたの仕事は,理恵さんが最善のツアープランを見つけられるよう,良い JOI ツアーの個数を理恵さんに伝えることである.すなわち,以下の条件を満たす 3 つの街 (i_0, i_1, i_2) の選び方の個数を求めなければならない.
- 街 i_0 にある飲食店はジュース専門店である.
- 街 i_1 にある飲食店はオムレツ専門店である.
- 街 i_2 にある飲食店はアイスクリーム専門店である.
- 街 i_0 から街 i_1 へ最短経路で移動し,街 i_1 から街 i_2 へ最短経路で移動したとき,同じ道路を複数回通らない.
IOI 国では,飲食店の種類の変更が Q 回予定されている.k + 1 回目 (0 \leqq k \leqq Q - 1) の変更の際には,0 \leqq X_k \leqq N - 1 を満たす整数 X_k と 0 \leqq Y_k \leqq 2 を満たす整数 Y_k が与えられる.これは,街 X_k にある飲食店が整数 Y_k に対応した専門店に変更されることを意味する.すなわち,Y_k = 0 ならばジュース専門店に,Y_k = 1 ならばオムレツ専門店に,Y_k = 2 ならばアイスクリーム専門店に変更される.あなたは,飲食店の変更の情報が与えられたとき,すぐにその変更を反映し,良い JOI ツアーの個数を理恵さんに伝えなければならない.IOI 国の道路と飲食店の情報が与えられたとき,飲食店の変更がなされるたび,それを反映して良い JOI ツアーの個数を返すプログラムを作成せよ.
実装の詳細
あなたは 1 つのファイルを提出しなければならない.
あなたの提出するファイルは joitour.cpp という名前である.そのプログラムは #include プリプロセッサ指令によって joitour.h を読み込むこと.
joitour.cpp は以下の関数を実装していなければならない.
-
void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V, int Q)- この関数の呼び出しによって,IOI 国の道路と飲食店の情報が与えられる.
- この関数は最初に 1 回だけ実行される.
- 引数
Nは街の数 N を表す. - 引数
Fは長さ N の配列であり,F[i](0 \leqq i \leqq N - 1) は街 i にある飲食店の種類 F_i を表す. - 引数
U,Vは長さ N - 1 の配列であり,U[j],V[j](0 \leqq j \leqq N - 2) は道路 j が結ぶ 2 つの街 U_j, V_j を表す. - 引数
Qは飲食店の種類が変更される回数 Q を表す.
-
void change(int X, int Y)- この関数の呼び出しによって,飲食店の種類が変更されたという情報が与えられる.
- この関数は Q 回実行される.
- この関数の k + 1 回目 (0 \leqq k \leqq Q - 1) の呼び出しにおいて,引数
Xは変更された飲食店のある街の番号 X_k を表す. - この関数の k + 1 回目 (0 \leqq k \leqq Q - 1) の呼び出しにおいて,引数
Yは変更後の飲食店の種類に対応する整数 Y_k を表す.変更後の飲食店の種類は,変更前の飲食店の種類と異なることが保証される.
-
long long num_tours()- この関数は以下のタイミングに各 1 回,合計で Q + 1 回実行される.
- 関数
initの実行の直後 - 関数
changeの各実行の直後
- 関数
- この関数は,現時点での良い JOI ツアーの個数を返さなければならない.
- この関数は以下のタイミングに各 1 回,合計で Q + 1 回実行される.
重要な注意
- 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である.
- あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない.ただし,標準エラー出力にデバッグ情報等を出力することは許される.
コンパイル・実行の方法
作成したプログラムをテストするための採点プログラムのサンプルが,コンテストサイトからダウンロードできるアーカイブの中に含まれている.このアーカイブには,提出しなければならないファイルのサンプルも含まれている.
採点プログラムのサンプルは 1 つのファイルからなる.そのファイルは grader.cpp である.作成したプログラムをテストするには,これらのファイル grader.cpp, joitour.cpp, joitour.h を同じディレクトリに置き,次のようにコマンドを実行する.なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.
g++ -std=gnu++20 -O2 -o grader grader.cpp joitour.cpp
コンパイルが成功すれば,grader という実行ファイルが生成される.
実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意せよ.採点プログラムのサンプルは単一のプロセスとして起動する.このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.
採点プログラムのサンプルの入力
採点プログラムのサンプルは標準入力から以下の形式で入力を読み込む.
N
F_0 F_1 \cdots F_{N - 1}
U_0 V_0
U_1 V_1
\vdots
U_{N - 2} V_{N - 2}
Q
X_0 Y_0
X_1 Y_1
\vdots
X_{Q - 1} Y_{Q - 1}
採点プログラムのサンプルの出力
採点プログラムのサンプルは,関数 num_tours を実行するたび,その戻り値を標準出力に 1 行で出力する.
制約
- 3 \leqq N \leqq 200\,000.
- 0 \leqq F_i \leqq 2 (0 \leqq i \leqq N - 1).
- 0 \leqq U_j < V_j \leqq N - 1 (0 \leqq j \leqq N - 2).
- どの 2 つの街の間も,いくつかの道路を経由して移動することができる.
- 0 \leqq Q \leqq 50\,000.
- 0 \leqq X_k \leqq N - 1 (0 \leqq k \leqq Q - 1).
- 0 \leqq Y_k \leqq 2 (0 \leqq k \leqq Q - 1).
- 関数
changeの各呼び出しにおいて,変更後の飲食店の種類は変更前の飲食店の種類と異なる. - 入力される値はすべて整数である.
小課題
- (6 点) N \leqq 400,Q \leqq 100.
- (8 点) N \leqq 4\,000,Q \leqq 1\,000.
- (6 点) Q = 0.
- (16 点) U_j = j,V_j = j + 1 (0 \leqq j \leqq N - 2).
- (16 点) U_j = \left\lfloor \frac{j}{2} \right\rfloor,V_j = j + 1 (0 \leqq j \leqq N - 2).(\left\lfloor \frac{j}{2} \right\rfloor は \frac{j}{2} を超えない最大の整数を表す.)
- (34 点) N \leqq 100\,000,Q \leqq 25\,000.
- (14 点) 追加の制約はない.
やりとりの例
採点プログラムのサンプルの入出力の例と,それに対応する関数の呼び出しの例を以下に示す.
入力例 1
3 0 1 2 0 1 1 2 0
出力例 1
1
| 呼び出し | 戻り値 |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 0) |
|
num_tours() |
1 |
良い JOI ツアーは (i_0, i_1, i_2) = (0, 1, 2) の 1 通りである.これが良い JOI ツアーであることは以下のように確かめられる.
- F_0 = 0 であるから,街 0 にある飲食店はジュース専門店である.
- F_1 = 1 であるから,街 1 にある飲食店はオムレツ専門店である.
- F_2 = 2 であるから,街 2 にある飲食店はアイスクリーム専門店である.
- 街 0 から街 1 へ最短経路で移動し,街 1 から街 2 へ最短経路で移動したとき,同じ道路を複数回通らない.
したがって,1 回目の num_tours の呼び出しは 1 を返さなければならない.
この入力例は小課題 1, 2, 3, 4, 6, 7 の制約を満たす.
入力例 2
3 0 1 2 0 1 1 2 2 2 0 0 2
出力例 2
1 0 1
| 呼び出し | 戻り値 |
|---|---|
init(3, [0, 1, 2], [0, 1], [1, 2], 2) |
|
num_tours() |
1 |
change(2, 0) |
|
num_tours() |
0 |
change(0, 2) |
|
num_tours() |
1 |
はじめの時点では,良い JOI ツアーは (i_0, i_1, i_2) = (0, 1, 2) の 1 通りである.したがって,1 回目の num_tours の呼び出しは 1 を返さなければならない.
1 回目の変更では,街 2 にある飲食店がアイスクリーム専門店からジュース専門店に変更される.このとき,IOI 国からアイスクリーム専門店がなくなるため,良い JOI ツアーは存在しない.したがって,2 回目の num_tours の呼び出しは 0 を返さなければならない.
2 回目の変更では,街 0 にある飲食店がジュース専門店からアイスクリーム専門店に変更される.このとき,良い JOI ツアーは (i_0, i_1, i_2) = (2, 1, 0) の 1 通りである.したがって,3 回目の num_tours の呼び出しは 1 を返さなければならない.
この入力例は小課題 1, 2, 4, 6, 7 の制約を満たす.
入力例 3
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
3 0 4 4 0 4 5 5
この入力例は小課題 1, 2, 5, 6, 7 の制約を満たす.