CF330B.Road Construction

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities — roads cannot be constructed between these pairs of cities.

Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.

一个国家有 nn 座城市。最初,该国内没有任何道路。某天,国王决定修建若干条道路,以连接某些城市对。道路是双向的。他希望所修建的道路满足如下条件:任意两座城市之间均可通过至多两条道路相互到达。此外,你还被给定了 mm 对城市——这些城市对之间不允许修建道路。

你的任务是构造满足上述条件的最少数量的道路。题目保证在给定约束下,这样的方案总是存在的。

输入格式

The first line consists of two integers n and m .

Then m lines follow, each consisting of two integers a__i and b__i (1 ≤ a__i, b__i ≤ n, a__i ≠ b__i), which means that it is not possible to construct a road connecting cities a__i and b__i. Consider the cities are numbered from 1 to n.

It is guaranteed that every pair of cities will appear at most once in the input.

第一行包含两个整数 nn 和 mm 。

接下来有 mm 行,每行包含两个整数 aia_i 和 bib_i(1 ≤ ai, bi ≤ n1 ≤ a_i, b_i ≤ n,且 ai ≠ bia_i ≠ b_i),表示无法在城市 aia_i 和 bib_i 之间修建道路。城市编号为 11 到 nn。

保证输入中每对城市至多出现一次。

输出格式

You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers a__i and b__i (1 ≤ a__i, b__i ≤ n, a__i ≠ b__i), which means that a road should be constructed between cities a__i and b__i.

If there are several solutions, you may print any of them.

第一行应输出一个整数 ss:需要修建的道路的最少数量。随后应输出 ss 行,每行包含两个整数 aia_i 和 bib_i(1 ≤ ai, bi ≤ n1 \le a_i, b_i \le n,且 ai ≠ bia_i \ne b_i),表示应在城市 aia_i 与 bib_i 之间修建一条道路。

若存在多种解法,可输出其中任意一种。

输入输出样例

  • 输入#1

    4 1
    1 3

    输出#1

    3
    1 2
    4 2
    2 3

说明/提示

This is one possible solution of the example:

These are examples of wrong solutions:

The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair.

The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads.

Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3, and 4 to 3.

这是示例的一种可能解法:

以下是若干错误解法的示例:

上述解法错误,原因在于它未使用最少数量的边(用了 4 条边,而最优解只需 3 条)。此外,该解法还试图在城市 1 和城市 3 之间修建道路,但题目输入明确说明:禁止在该城市对之间修建道路。

上述解法错误,因为从城市 1 到城市 3 至少需经过 3 条道路,而在你的国家中,任意两座城市之间必须能通过至多 2 条道路互相到达。

最后,上述解法错误,因为必须保证任意两座城市之间均可互相到达;但在该方案中,无法从城市 1 到达城市 3、从城市 2 到达城市 3,以及从城市 4 到达城市 3。

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

首页