CF847L.Berland SU Computer Network

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

In the computer network of the Berland State University there are n routers numbered from 1 to n. Some pairs of routers are connected by patch cords. Information can be transmitted over patch cords in both direction. The network is arranged in such a way that communication between any two routers (directly or through other routers) is possible. There are no cycles in the network, so there is only one path between each pair of routers over patch cords.

Unfortunately, the exact topology of the network was lost by administrators. In order to restore it, the following auxiliary information was collected.

For each patch cord p, directly connected to the router i, list of routers located behind the patch cord p relatively i is known. In other words, all routers path from which to the router i goes through p are known. So for each router i there are k__i lists, where k__i is the number of patch cords connected to i.

For example, let the network consists of three routers connected in chain 1 - 2 - 3. Then:

  • the router 1: for the single patch cord connected to the first router there is a single list containing two routers: 2 and 3;
  • the router 2: for each of the patch cords connected to the second router there is a list: one list contains the router 1 and the other — the router 3;
  • the router 3: for the single patch cord connected to the third router there is a single list containing two routers: 1 and 2.

Your task is to help administrators to restore the network topology, i. e. to identify all pairs of routers directly connected by a patch cord.

贝兰国立大学的计算机网络中有 nn 台路由器,编号从 11 到 nn。某些路由器对之间通过网线(patch cord)相连。信息可在网线中双向传输。该网络结构保证任意两台路由器之间(无论直接相连还是经由其他路由器中转)均可通信。网络中不存在环路,因此任意两台路由器之间仅存在唯一一条经由网线构成的路径。

不幸的是,网络的确切拓扑结构已被管理员遗失。为恢复该结构,已收集到如下辅助信息:

对于路由器 ii 所连接的每条网线 pp,已知“位于 pp 相对于 ii 后方”的所有路由器列表。换言之,所有从该列表中的路由器出发、到达路由器 ii 的路径均必经过网线 pp。因此,对每台路由器 ii,共有 kik_i 个列表,其中 kik_i 表示与路由器 ii 相连的网线数量。

例如,设网络由三台路由器以链状连接:1–2–31\text{--}2\text{--}3。则:

  • 路由器 11:与其相连的唯一一条网线对应一个列表,包含两台路由器:22 和 33;
  • 路由器 22:与其相连的两条网线各对应一个列表:一个列表包含路由器 11,另一个列表包含路由器 33;
  • 路由器 33:与其相连的唯一一条网线对应一个列表,包含两台路由器:11 和 22。

你的任务是协助管理员恢复网络拓扑结构,即确定所有由网线直接相连的路由器对。

输入格式

The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of routers in the network.

The i-th of the following n lines contains a description of the lists for the router i.

The description of each list begins with the number of routers in it. Then the symbol ':' follows, and after that the numbers of routers from the list are given. This numbers are separated by comma. Lists are separated by symbol '-'.

It is guaranteed, that for each router i the total number of routers in its lists equals to n - 1 and all the numbers in lists of each router are distinct. For each router i lists do not contain the number i.

第一行包含一个整数 nn(2≤n≤10002 \leq n \leq 1000)—— 网络中路由器的数量。

接下来的 nn 行中,第 ii 行描述路由器 ii 的列表。

每个列表的描述以该列表中路由器的数量开头,随后是符号 :,之后给出该列表中所有路由器的编号,这些编号之间用逗号分隔。不同列表之间用符号 - 分隔。

保证:对每个路由器 ii,其所有列表中路由器的总数恰好为 n−1n-1,且每个路由器 ii 的各列表中的编号互不相同;此外,每个路由器 ii 的列表中均不包含编号 ii。

输出格式

Print -1 if no solution exists.

In the other case print to the first line n - 1 — the total number of patch cords in the network. In each of the following n - 1 lines print two integers — the routers which are directly connected by a patch cord. Information about each patch cord must be printed exactly once.

Patch cords and routers can be printed in arbitrary order.

如果无解,输出 -1。

否则,在第一行输出 n - 1 —— 网络中网线的总数。在接下来的 n - 1 行中,每行输出两个整数 —— 由该网线直接连接的两台路由器。每条网线的信息必须恰好输出一次。

网线和路由器的输出顺序可以任意。

输入输出样例

  • 输入#1

    3
    2:3,2
    1:1-1:3
    2:1,2

    输出#1

    2
    2 1
    2 3
  • 输入#2

    5
    4:2,5,3,4
    1:4-1:1-2:5,3
    4:4,5,2,1
    4:2,1,3,5
    1:3-3:4,2,1

    输出#2

    4
    2 1
    2 4
    5 2
    3 5
  • 输入#3

    3
    1:2-1:3
    1:1-1:3
    1:1-1:2

    输出#3

    -1

说明/提示

The first example is analyzed in the statement.

The answer to the second example is shown on the picture.

The first router has one list, which contains all other routers. The second router has three lists: the first — the single router 4, the second — the single router 1, the third — two routers 3 and 5. The third router has one list, which contains all other routers. The fourth router also has one list, which contains all other routers. The fifth router has two lists: the first — the single router 3, the second — three routers 1, 2 and 4.

第一个示例在题目描述中已进行分析。

第二个示例的答案如图所示。

第一台路由器拥有一张列表,其中包含其余所有路由器。
第二台路由器拥有三张列表:第一张仅含路由器 4;第二张仅含路由器 1;第三张包含路由器 3 和 5。
第三台路由器拥有一张列表,其中包含其余所有路由器。
第四台路由器也拥有一张列表,其中包含其余所有路由器。
第五台路由器拥有两张列表:第一张仅含路由器 3;第二张包含路由器 1、2 和 4。

输入解题思路,AI测评打分。不知道怎么写?

首页