CF1252L.Road Construction
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are N cities in the country of Numbata, numbered from 1 to N. Currently, there is no road connecting them. Therefore, each of these N cities proposes a road candidate to be constructed.
City i likes to connect with city Ai, so city i proposes to add a direct bidirectional road connecting city i and city Ai. It is guaranteed that no two cities like to connect with each other. In other words, there is no pair of integers i and j where Ai=j and Aj=i. It is also guaranteed that any pair of cities are connected by a sequence of road proposals. In other words, if all proposed roads are constructed, then any pair of cities are connected by a sequence of constructed road.
City i also prefers the road to be constructed using a specific material. Each material can be represented by an integer (for example, 0 for asphalt, 1 for wood, etc.). The material that can be used for the road connecting city i and city Ai is represented by an array Bi containing Mi integers: [(Bi)1,(Bi)2,…,(Bi)Mi]. This means that the road connecting city i and city Ai can be constructed with either of the material in Bi.
There are K workers to construct the roads. Each worker is only familiar with one material, thus can only construct a road with a specific material. In particular, the ith worker can only construct a road with material Ci. Each worker can only construct at most one road. You want to assign each worker to construct a road such that any pair of cities are connected by a sequence of constructed road.
Numbata 国有 N 座城市,编号从 1 到 N。目前,这些城市之间没有任何道路相连。因此,每座城市均提出一条待修建的道路候选方案。
城市 i 希望与城市 Ai 相连,因此城市 i 提出修建一条连接城市 i 与城市 Ai 的双向直连道路。保证不存在两座城市彼此互为偏好对象,即:不存在整数对 (i,j) 满足 Ai=j 且 Aj=i。同时保证:若将所有提出的道路全部修建,则任意两座城市均可通过若干条已建道路相互连通(即所有道路构成的图是连通的)。
此外,城市 i 还倾向于使用某种特定材料来修建该道路。每种材料可用一个整数表示(例如,0 表示沥青,1 表示木材等)。用于修建连接城市 i 与城市 Ai 的道路的可选材料由数组 Bi 给出,其中包含 Mi 个整数:[(Bi)1,(Bi)2,…,(Bi)Mi]。这意味着连接城市 i 与城市 Ai 的道路可选用 Bi 中任意一种材料建造。
共有 K 名工人负责修建道路。每位工人仅熟悉一种材料,因此只能修建使用该特定材料的道路。具体而言,第 i 位工人仅能修建材料为 Ci 的道路。每位工人最多只能修建一条道路。你的目标是为每位工人分配一条道路进行修建,使得最终修建的道路集合仍能保证任意两座城市均可通过若干条已建道路相互连通。
输入格式
Input begins with a line containing two integers: N K (3≤N≤2000; 1≤K≤2000) representing the number of cities and the number of workers, respectively. The next N lines each contains several integers: Ai Mi (Bi)1, (Bi)2, ⋯, (Bi)Mi (1≤Ai≤N; Ai=i; 1≤Mi≤10000; 0≤(Bi)1<(Bi)2<⋯<(Bi)Mi≤109) representing the bidirectional road that city i likes to construct. It is guaranteed that the sum of Mi does not exceed 10000. It is also guaranteed that no two cities like to connect with each other and any pair of cities are connected by a sequence of road proposals. The next line contains K integers: Ci (0≤Ci≤109) representing the material that is familiarized by the workers.
输入的第一行包含两个整数:N 和 K(3≤N≤2000;1≤K≤2000),分别表示城市的数量和工人的数量。接下来的 N 行,每行包含若干个整数:Ai Mi (Bi)1, (Bi)2, ⋯, (Bi)Mi(其中 1≤Ai≤N;Ai=i;1≤Mi≤10000;0≤(Bi)1<(Bi)2<⋯<(Bi)Mi≤109),表示城市 i 希望建造的一条双向道路。保证所有 Mi 的总和不超过 10000。同时保证任意两座城市之间至多存在一条彼此希望连接的道路,且任意两座城市均可通过一系列道路提案相互连通。下一行包含 K 个整数:Ci(0≤Ci≤109),表示各工人所熟悉的材料。
输出格式
If it is not possible to assign each worker to construct a road such that any pair of cities are connected by a sequence of constructed road, simply output -1 in a line. Otherwise, for each worker in the same order as input, output in a line two integers (separated by a single space): u and v in any order. This means that the worker constructs a direct bidirectional road connecting city u and v. If the worker does not construct any road, output "0 0" (without quotes) instead. Each pair of cities can only be assigned to at most one worker. You may output any assignment as long as any pair of cities are connected by a sequence of constructed road.
如果无法为每位工人分配一条道路,使得任意两座城市之间都可通过已修建的道路序列相互连通,则直接在一行中输出 -1。否则,对于输入中给出的每位工人(按相同顺序),在一行中输出两个整数(用单个空格分隔):u 和 v(顺序任意),表示该工人修建一条连接城市 u 和 v 的双向直连道路;若该工人不修建任何道路,则输出 0 0(不带引号)。每对城市至多只能被分配给一位工人。只要最终所有城市构成连通图,可输出任意一种满足条件的分配方案。
输入输出样例
输入#1
4 5 2 2 1 2 3 2 2 3 4 2 3 4 2 2 4 5 1 2 3 4 5
输出#1
1 2 2 3 3 4 0 0 4 2
输入#2
4 5 2 2 10 20 3 2 2 3 4 2 3 4 2 2 4 5 1 2 3 4 5
输出#2
-1
说明/提示
Explanation for the sample input/output #1
We can assign the workers to construct the following roads:
- The first worker constructs a road connecting city 1 and city 2.
- The second worker constructs a road connecting city 2 and city 3.
- The third worker constructs a road connecting city 3 and city 4.
- The fourth worker does not construct any road.
- The fifth worker constructs a road connecting city 4 and city 2.
Therefore, any pair of cities are now connected by a sequence of constructed road.
Explanation for the sample input/output #2
There is no worker that can construct a road connecting city 1, thus city 1 is certainly isolated.
样例输入/输出 #1 的说明
我们可以安排工人修建以下道路:
- 第一位工人修建连接城市 1 和城市 2 的道路。
- 第二位工人修建连接城市 2 和城市 3 的道路。
- 第三位工人修建连接城市 3 和城市 4 的道路。
- 第四位工人不修建任何道路。
- 第五位工人修建连接城市 4 和城市 2 的道路。
因此,任意两个城市之间现在均可通过一系列已修建的道路相互连通。
样例输入/输出 #2 的说明
没有任何工人能够修建连接城市 1 的道路,因此城市 1 必然孤立。
输入解题思路,AI测评打分。不知道怎么写?