B. High Load

Codeforces
IDCF827B
Time2000ms
Memory512MB
Difficulty
constructive algorithmsgreedyimplementationtrees
English · Original
Chinese · Translation
Formal · Original
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of _n_ nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly _k_ of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability. Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes. Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible. ## Input The first line contains two integers _n_ and _k_ (3 ≤ _n_ ≤ 2·105, 2 ≤ _k_ ≤ _n_ - 1) — the total number of nodes and the number of exit-nodes. Note that it is always possible to build at least one network with _n_ nodes and _k_ exit-nodes within the given constraints. ## Output In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next _n_ - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to _n_. Exit-nodes can have any ids. If there are multiple answers, print any of them. [samples] ## Note In the first example the only network is shown on the left picture. In the second example one of optimal networks is shown on the right picture. Exit-nodes are highlighted. <center>![image](https://espresso.codeforces.com/1e6fa9ccd34807a112dd2679f0a0d87fc763f725.png)</center>
Arkady 再次需要你的帮助!这次他决定建立自己的高速互联网交换点。该网络应由 #cf_span[n] 个节点组成,用最少数量的导线连接成一个整体(每根导线直接连接两个节点)。其中恰好有 #cf_span[k] 个节点为出口节点,即每个出口节点仅与网络中的另一个节点相连,而所有其他节点必须至少与两个节点相连,以提高系统稳定性。 Arkady 希望尽可能提高系统速度,因此他希望最小化两个出口节点之间的最大距离。两个节点之间的距离是指数据包在这两个节点之间传输所经过的导线数量。 请帮助 Arkady 找到一种构建网络的方法,使得最远的两个出口节点之间的距离尽可能小。 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[3 ≤ n ≤ 2·105],#cf_span[2 ≤ k ≤ n - 1])——节点总数和出口节点数。 保证在给定约束下,至少存在一种包含 #cf_span[n] 个节点和 #cf_span[k] 个出口节点的网络。 第一行输出两个最远出口节点之间的最小可能距离。接下来的 #cf_span[n - 1] 行,每行输出两个整数:由一根导线连接的两个节点的编号。每根导线的描述仅需输出一次。导线及其端点的输出顺序可以任意。节点编号从 #cf_span[1] 到 #cf_span[n]。出口节点的编号可以任意指定。 如果存在多个答案,输出任意一个即可。 第一个例子中唯一的网络如左图所示。 第二个例子中一个最优网络如右图所示。 出口节点已高亮显示。 ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[3 ≤ n ≤ 2·105],#cf_span[2 ≤ k ≤ n - 1])——节点总数和出口节点数。保证在给定约束下,至少存在一种包含 #cf_span[n] 个节点和 #cf_span[k] 个出口节点的网络。 ## Output 第一行输出两个最远出口节点之间的最小可能距离。接下来的 #cf_span[n - 1] 行,每行输出两个整数:由一根导线连接的两个节点的编号。每根导线的描述仅需输出一次。导线及其端点的输出顺序可以任意。节点编号从 #cf_span[1] 到 #cf_span[n]。出口节点的编号可以任意指定。如果存在多个答案,输出任意一个即可。 [samples] ## Note 在第一个例子中,唯一的网络如左图所示。 在第二个例子中,一个最优网络如右图所示。 出口节点已高亮显示。
**Definitions** Let $ n, k \in \mathbb{Z} $ with $ 3 \leq n \leq 2 \cdot 10^5 $, $ 2 \leq k \leq n - 1 $. Let $ G = (V, E) $ be a tree with $ |V| = n $, where $ k $ nodes are designated as *exit-nodes* (degree 1), and the remaining $ n - k $ nodes have degree at least 2. **Constraints** 1. $ G $ is a tree: $ |E| = n - 1 $, connected, acyclic. 2. Exactly $ k $ vertices in $ V $ have degree 1 (exit-nodes). 3. All other $ n - k $ vertices have degree $ \geq 2 $. **Objective** Minimize the diameter of the set of exit-nodes: $$ \min_{G} \max_{u,v \in \text{ExitNodes}} \text{dist}_G(u, v) $$ where $ \text{dist}_G(u, v) $ is the number of edges in the unique path between $ u $ and $ v $ in $ G $. Output the minimum possible diameter and any such tree $ G $.
Samples
Input #1
3 2
Output #1
2
1 2
2 3
Input #2
5 3
Output #2
3
1 2
2 3
3 4
3 5
API Response (JSON)
{
  "problem": {
    "name": "B. High Load",
    "description": {
      "content": "Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of _n_ nodes connected with minimum possible number of wires into one network ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF827B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of _n_ nodes connected with minimum possible number of wires into one network ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Arkady 再次需要你的帮助!这次他决定建立自己的高速互联网交换点。该网络应由 #cf_span[n] 个节点组成,用最少数量的导线连接成一个整体(每根导线直接连接两个节点)。其中恰好有 #cf_span[k] 个节点为出口节点,即每个出口节点仅与网络中的另一个节点相连,而所有其他节点必须至少与两个节点相连,以提高系统稳定性。\n\nArkady 希望尽可能提高系统速度,因此他希望最小化两个出口节点...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z} $ with $ 3 \\leq n \\leq 2 \\cdot 10^5 $, $ 2 \\leq k \\leq n - 1 $.  \nLet $ G = (V, E) $ be a tree with $ |V| = n $, where $ k $ nodes are designated as *exit-n...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments