

Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 点
問題文
高橋王国は 個の街と 本の道路からなります。
街には の番号がついており、 本目の道路は街 と街 を双方向に結んでいます。どの街からどの街へもいくつかの道路をたどることで到達できることが保証されます。
また、街 にはガソリンスタンドがあります。
個のクエリに答えてください。 番目のクエリは以下の内容です。
- 街 を出発し、ガソリンスタンドのある街を つ以上通った後、街 に行くとき、道を通る回数の最小値を求めよ。
制約
- 街 にはガソリンスタンドがない
- どの街からどの街へもいくつかの道路をたどることで到達できる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
出力
行出力せよ。 行目には、 番目のクエリに対する答えを出力せよ。
入力例 1Copy
3 2 1 1 1 1 2 2 3 3 2
出力例 1Copy
3
街 を出発し、街 、街 、街 の順に移動すると移動回数は 回です。これより少ない移動回数は実現できないので を出力します。
入力例 2Copy
5 6 2 2 3 1 1 2 2 3 2 4 1 5 2 5 3 5 5 5 5 4
出力例 2Copy
2 3
Score : points
Problem Statement
The Kingdom of Takahashi has towns and roads.
The towns are numbered , and the -th road connects Town and Town bidirectionally. It is guaranteed that one can get from every town to every town by traversing some roads.
Additionally, there are gas stations in Towns .
Answer queries. The -th query is as follows.
- Find the minimum number of times one needs to traverse a road when getting from Town to Town via a town with a gas station.
Constraints
- Nither nor has a gas station.
- It is possible to get from every town to every town by traversing some roads.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the -th query.
Sample Input 1Copy
3 2 1 1 1 1 2 2 3 3 2
Sample Output 1Copy
3
After starting in Town , we can go to Town , Town , Town in this order to complete the trip with three moves. It is impossible to do it with fewer moves, so we print .
Sample Input 2Copy
5 6 2 2 3 1 1 2 2 3 2 4 1 5 2 5 3 5 5 5 5 4
Sample Output 2Copy
2 3