CF1252L.Road Construction

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are NN cities in the country of Numbata, numbered from 11 to NN . Currently, there is no road connecting them. Therefore, each of these NN cities proposes a road candidate to be constructed.

City ii likes to connect with city AiA_i , so city ii proposes to add a direct bidirectional road connecting city ii and city AiA_i . It is guaranteed that no two cities like to connect with each other. In other words, there is no pair of integers ii and jj where Ai=jA_i = j and Aj=iA_j = 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 ii also prefers the road to be constructed using a specific material. Each material can be represented by an integer (for example, 00 for asphalt, 11 for wood, etc.). The material that can be used for the road connecting city ii and city AiA_i is represented by an array BiB_i containing MiM_i integers: [(Bi)1,(Bi)2,,(Bi)Mi][(B_i)_1, (B_i)_2, \dots, (B_i)_{M_i}] . This means that the road connecting city ii and city AiA_i can be constructed with either of the material in BiB_i .

There are KK 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 ithi^{th} worker can only construct a road with material CiC_i . 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.

输入格式

Input begins with a line containing two integers: NN KK ( 3N20003 \le N \le 2000 ; 1K20001 \le K \le 2000 ) representing the number of cities and the number of workers, respectively. The next NN lines each contains several integers: AiA_i MiM_i (Bi)1(B_i)_1 , (Bi)2(B_i)_2 , \cdots , (Bi)Mi(B_i)_{M_i} ( 1AiN1 \le A_i \le N ; AiiA_i \ne i ; 1Mi100001 \le M_i \le 10\,000 ; 0(Bi)1<(Bi)2<<(Bi)Mi1090 \le (B_i)_1 < (B_i)_2 < \dots < (B_i)_{M_i} \le 10^9 ) representing the bidirectional road that city ii likes to construct. It is guaranteed that the sum of MiM_i does not exceed 1000010\,000 . 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 KK integers: CiC_i ( 0Ci1090 \le C_i \le 10^9 ) representing the material that is familiarized by the workers.

输出格式

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): uu and vv in any order. This means that the worker constructs a direct bidirectional road connecting city uu and vv . 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

    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 11 and city 22 .
  • The second worker constructs a road connecting city 22 and city 33 .
  • The third worker constructs a road connecting city 33 and city 44 .
  • The fourth worker does not construct any road.
  • The fifth worker constructs a road connecting city 44 and city 22 .

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 11 , thus city 11 is certainly isolated.

首页