CF1906I.Contingency Plan 2

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are working as a manager in The ICPC Company. In the company building, there are NN computers (numbered from 11 to NN). There are N−1N - 1 cables, numbered from 11 to N−1N - 1, that connect all the computers into a single network. Cable ii connects computer UiU_i and ViV_i. Each cable can be set into emergency mode, where cable ii only transfers data from computer UiU_i to computer ViV_i, but not the other way around. During a disaster, it is mandatory for all cables to be in emergency mode.

Through your research, you discover a new way to determine the vulnerability of a network. You want to add zero or more new cables to the current network such that it is not vulnerable during a disaster. Your network is not vulnerable if and only if there is exactly one permutation of 11 to NN such that uu appears before vv in the permutation for all cables that connect computer uu and vv. In other words, it should have exactly one topological order.

The following illustration shows examples of not vulnerable networks and vulnerable networks.

For the not vulnerable networks, the only permutation that satisfies the requirement for the networks on the left and on the right are [1,2,3][1, 2, 3] and [3,1,2][3, 1, 2], respectively. Meanwhile, for the vulnerable networks, there are 22 permutations that satisfy the requirement for the network on the left: [1,2,3][1, 2, 3] and [3,1,2][3, 1, 2]; while there is no permutation that satisfies the requirement for the network on the right.

You are interested in the minimum number of new cables that should be added to the current network such that it is not vulnerable during a disaster. Furthermore, you want to know, which pairs of computers should be connected by using the minimum number of cables. If there are several ways to connect, you can connect in any way of your choice. Under the given constraints, it can be proven that there exists a way to make the network not vulnerable.

你正在 ICPC 公司担任经理。在公司大楼中,共有 NN 台计算机(编号从 11 到 NN)。有 N−1N - 1 根电缆(编号从 11 到 N−1N - 1),将所有计算机连接成一个连通网络。第 ii 根电缆连接计算机 UiU_i 和 ViV_i。每根电缆均可设置为“应急模式”,此时第 ii 根电缆仅允许数据从计算机 UiU_i 单向传输至计算机 ViV_i,而不可反向传输。在灾难发生期间,所有电缆必须处于应急模式。

通过研究,你发现了一种评估网络脆弱性的新方法。你希望向当前网络中添加零条或多条新电缆,使得该网络在灾难期间不具有脆弱性。当且仅当存在唯一一个 11 到 NN 的排列,使得对每一条连接计算机 uu 与 vv 的电缆,uu 在该排列中均出现在 vv 之前时,网络才被视为不具有脆弱性。换言之,该有向图应恰好拥有唯一一个拓扑序。

下图展示了若干“不脆弱网络”与“脆弱网络”的示例:

对于图中两个“不脆弱网络”,分别仅存在唯一满足条件的排列:左侧网络为 [1,2,3][1, 2, 3],右侧网络为 [3,1,2][3, 1, 2]。而对于两个“脆弱网络”,左侧网络存在两个满足条件的排列:[1,2,3][1, 2, 3] 和 [3,1,2][3, 1, 2];右侧网络则不存在任何满足条件的排列。

你关心的是:为使当前网络在灾难期间不具有脆弱性,所需添加的最少电缆数量是多少?此外,你还希望知道:在使用该最少数量电缆的前提下,应将哪些计算机对连接起来?若存在多种可行方案,任选其一即可。在本题给定约束下,可以证明:总存在一种方式使网络变得不脆弱。

输入格式

The first line consists of an integer NN (2≤N≤100 0002 \leq N \leq 100\,000).

Each of the next N−1N - 1 lines consists of two integers UiU_i ViV_i (1≤Ui,Vi≤N1 \leq U_i, V_i \leq N). The input edges form a tree.

第一行包含一个整数 NN(2≤N≤100 0002 \leq N \leq 100\,000)。

接下来的 N−1N - 1 行,每行包含两个整数 UiU_i 和 ViV_i(1≤Ui,Vi≤N1 \leq U_i, V_i \leq N)。输入的边构成一棵树。

输出格式

The first line consists of an integer, representing the minimum number of new cables that should be added to the current network such that it is no longer vulnerable during a disaster. Denote this number as KK and the new cables are numbered from 11 to KK.

If KK is not 00, then output KK lines. Each of the next KK lines consists of two integers AiA_i BiB_i, representing the computers that are connected by the new cable ii. During a disaster, new cable ii only transfers data from computer AiA_i to computer BiB_i, but not the other way around. If there exist several solutions, you can output any of them.

第一行包含一个整数,表示为使当前网络在灾难期间不再脆弱,所需新增电缆的最少数量。将该数量记为 KK,新增电缆编号为 11 至 KK。

若 K≠0K \neq 0,则输出 KK 行。接下来的每行包含两个整数 AiA_i 和 BiB_i,表示新增电缆 ii 所连接的两台计算机。在灾难期间,新增电缆 ii 仅能从计算机 AiA_i 向计算机 BiB_i 传输数据,而不能反向传输。若存在多种可行解,输出任意一种即可。

输入输出样例

  • 输入#1

    3
    1 2
    3 2

    输出#1

    1
    3 1
  • 输入#2

    3
    1 2
    2 3

    输出#2

    0
  • 输入#3

    5
    1 2
    1 3
    3 4
    3 5

    输出#3

    2
    2 3
    4 5

说明/提示

Explanation for the sample input/output #3

The following illustration shows the original network and the new network with the added cables during a disaster. The only permutation that satisfies the requirement is [1,2,3,4,5][1, 2, 3, 4, 5].

样例输入/输出 #3 的说明

以下示意图展示了原始网络以及灾难期间新增电缆后的新网络。唯一满足要求的排列是 [1,2,3,4,5][1, 2, 3, 4, 5]。

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

首页