#P9331. [JOISC 2023 Day1] Passport
[JOISC 2023 Day1] Passport
题目描述
Passport is a certificate which is used worldwide when a traveler enters foreign countries.
In a planet, there are countries, numbered from to . Each country issues a passport. When a traveler has a passport issued by the country , the traveler can enter the countries . Here, it is guaranteed that the traveler can enter the country where the passport was issued. Namely, is satisfied.
You have a friend who likes traveling. Although he dreams of traveling around the world, he does not have a passport in the beginning. Thus, he plans to visit all of the countries by repeating the following two actions.
- He gets a passport issued by the country where he is currently staying.
- He moves to a country where he can enter using a passport he currently has.
When you hear about his plan, you are wondering whether it is possible to realize the plan, and, if it is possible, what is the minimum number of passports he needs to get. Since you do not know where he lives, you consider possible countries where he lives.
Write a program which, given information of the passports and the possibilities of his living place, for each possibility, determines whether it is possible for him to visit all of the countries, and, if it is possible, calculates the minimum number of passports he needs to get.
输入格式
Read the following data from the standard input.
输出格式
Write lines to the standard output. The -th line corresponds to the case where your friend lives in the country . If it is possible for him to visit all of the countries, this line should contain the minimum number of passports he needs to get. Otherwise, this line should contain .
题目大意
题目描述
护照是旅行家进入他国时使用的证件。
在一个星球上有 个国家,从 到 编号。每个国家都签发一种护照。当旅行家获得由国家 签发的护照后,他能够进入国家 。这里保证旅行家能够进入其签证的签发国。形式上地说, 必然成立。
你有一个爱旅行的朋友。即使他奢望能环游世界,但他最初一种护照也没有。因此,他想通过一下重复以下两项行为来环游这 个国家。
- 获得他当前所在国家签发的护照。
- 用他现有的护照进入某个国家。
知道他的计划后,你想知道这个计划的是否可行,和如果可行的话,他最少需要的护照数量。因为你并不清楚他现在身处何国,所以你预测了 个他可能正居住在那的国家 。
现在给定各国护照的信息 和他可能居住的 个国家,您需要写一个程序,对于每一个可能居住的国家,判断他是否可能环游这 个国家,如果可能的话,计算出他需要的最少护照种数。
输入格式
从标准输入读入:
输出格式
输出 行至标准输出,第 行一个整数代表若你的朋友位于国家 的答案。若他能环游这 个国家,则输出他需要的最少护照种数,否则输出 。
提示
【样例解释 #1】
假设你的朋友居住在国家 ,一种可行的方式如下,最后他获得了 种护照:
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
- 用国家 签发的护照去国家 旅行。
可以证明不存在使用护照种数更小的方案,故输出 。
该样例满足所有子任务的限制。
【样例解释 #2】
假设你的朋友居住在国家 。一种可行的方式如下,最后他获得了 种护照:
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
- 获得国家 签发的护照。
- 用国家 签发的护照去国家 旅行。
可以证明不存在使用护照种数更小的方案,故输出 。
该样例满足子任务 的限制。
【样例解释 #3】
例如,如果你的朋友居住在 ,一种可行的方案书获得国家 签发的护照,并用它来依次去国家 旅行。故第三行输出 。
但如果你的朋友居住在国家 ,即使他获得了国家 签发的护照,他也不可能进入任何其他国家,因此,他无法实现自己的旅行计划。故第五行输出 。
该样例满足子任务 的限制。
【样例解释 #4】
该样例满足子任务 的限制。
4
1 3
2 4
2 3
4 4
1
1
2
5
1 5
2 4
2 3
3 5
1 5
1
3
4
5
1 1
2 3
1 5
3 4
5 5
5
1
2
3
4
5
-1
2
1
2
-1
4
1 2
1 2
3 4
3 4
4
1
2
3
4
-1
-1
-1
-1
提示
【样例解释 #1】
Assume that your friend lives in the country . It is possible for him to visit all of the countries if he acts in the following way. Then, he gets passports.
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
- Using the passport issued by the country , he moves to the country $44.
Since it is impossible to realize the plan if he gets less than or equal to passport, output .
该样例满足所有子任务的限制。
【样例解释 #2】
Assume that your friend lives in the country . It is possible for him to visit all of the countries if he acts in the following way. Then, he gets passports.
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
- He gets a passport issued by the country .
- Using the passport issued by the country , he moves to the country .
Since it is impossible to realize the plan if he gets less than or equal to passports, output .
该样例满足子任务 的限制。
【样例解释 #3】
For example, if your friend lives in the country , it is possible to realize the plan if he gets a passport issued by the country , and uses it to visit the countries in this order. Therefore, output in the third line.
On the other hand, if your friend lives in the country , it is impossible for him to enter other countries even if he gets a passport issued by the country . Thus, he cannot realize the plan. Therefore, output in the fifth line.
该样例满足子任务 的限制。
【样例解释 #4】
该样例满足子任务 的限制。
【数据范围】
对于所有测试数据,满足 ,,,,保证所有输入均为整数。
子任务编号 | 分值 | 限制 |
---|---|---|
, | ||
, | ||
, | ||
无 |