F. Berland and the Shortest Paths

Codeforces
IDCF1005F
Time5000ms
Memory256MB
Difficulty
brute forcedfs and similargraphsshortest paths
English · Original
Chinese · Translation
Formal · Original
There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $1$ to $n$. It is known that, from the capital (the city with the number $1$), you can reach any other city by moving along the roads. The President of Berland plans to improve the country's road network. The budget is enough to repair exactly $n-1$ roads. The President plans to choose a set of $n-1$ roads such that: * it is possible to travel from the capital to any other city along the $n-1$ chosen roads, * if $d_i$ is the number of roads needed to travel from the capital to city $i$, moving only along the $n-1$ chosen roads, then $d_1 + d_2 + \dots + d_n$ is minimized (i.e. as minimal as possible). In other words, the set of $n-1$ roads should preserve the connectivity of the country, and the sum of distances from city $1$ to all cities should be minimized (where you can only use the $n-1$ chosen roads). The president instructed the ministry to prepare $k$ possible options to choose $n-1$ roads so that both conditions above are met. Write a program that will find $k$ possible ways to choose roads for repair. If there are fewer than $k$ ways, then the program should output all possible valid ways to choose roads. ## Input The first line of the input contains integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5$), where $n$ is the number of cities in the country, $m$ is the number of roads and $k$ is the number of options to choose a set of roads for repair. It is guaranteed that $m \cdot k \le 10^6$. The following $m$ lines describe the roads, one road per line. Each line contains two integers $a_i$, $b_i$ ($1 \le a_i, b_i \le n$, $a_i \ne b_i$) — the numbers of the cities that the $i$\-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital. ## Output Print $t$ ($1 \le t \le k$) — the number of ways to choose a set of roads for repair. Recall that you need to find $k$ different options; if there are fewer than $k$ of them, then you need to find all possible different valid options. In the following $t$ lines, print the options, one per line. Print an option as a string of $m$ characters where the $j$\-th character is equal to '_1_' if the $j$\-th road is included in the option, and is equal to '_0_' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $t$ lines should be different. Since it is guaranteed that $m \cdot k \le 10^6$, the total length of all the $t$ lines will not exceed $10^6$. If there are several answers, output any of them. [samples]
伯兰有 $n$ 座城市。某些城市对之间由道路连接。所有道路都是双向的。每条道路连接两个不同的城市,任意两个城市之间至多有一条道路。城市编号为 $1$ 到 $n$。 已知从首都(编号为 $1$ 的城市)出发,可以通过道路到达任何其他城市。 伯兰总统计划改善国家的道路网络。预算恰好足够修复 $n -1$ 条道路。总统计划选择一个包含 $n -1$ 条道路的集合,使得: 换句话说,所选的 $n -1$ 条道路应保持国家的连通性,并最小化从城市 $1$ 到所有其他城市的距离之和(仅允许使用这 $n -1$ 条选定的道路)。 总统指示部级部门准备 $k$ 种可能的方案,以选择满足上述两个条件的 $n -1$ 条道路。 编写一个程序,找出 $k$ 种可能的道路修复方案。如果少于 $k$ 种方案,则程序应输出所有可能的有效方案。 输入的第一行包含整数 $n$、$m$ 和 $k$($2 \leq n \leq 2 \cdot 10^5$,$n -1 \leq m \leq 2 \cdot 10^5$,$1 \leq k \leq 2 \cdot 10^5$),其中 $n$ 是国家的城市数量,$m$ 是道路数量,$k$ 是选择道路修复集合的方案数。保证 $m \cdot k \leq 10^6$。 接下来的 $m$ 行描述道路,每行一条。每行包含两个整数 $a_i$、$b_i$($1 \leq a_i, b_i \leq n$,$a_i \ne b_i$)——表示第 $i$ 条道路连接的两个城市编号。任意两个城市之间至多有一条道路。给定的道路集合保证可以从首都到达任何城市。 输出 $t$($1 \leq t \leq k$)——选择道路修复集合的方案数。注意,你需要找出 $k$ 种不同的方案;如果少于 $k$ 种,则需找出所有可能的不同有效方案。 接下来的 $t$ 行,每行输出一个方案。每个方案用一个长度为 $m$ 的字符串表示,其中第 $j$ 个字符为 '_1_' 表示第 $j$ 条道路被包含在该方案中,为 '_0_' 表示未被包含。道路编号应按照输入顺序。方案的输出顺序任意,所有 $t$ 行必须互不相同。 由于保证 $m \cdot k \leq 10^6$,所有 $t$ 行的总长度不超过 $10^6$。 如果有多个答案,输出任意一个即可。 ## Input 输入的第一行包含整数 $n$、$m$ 和 $k$($2 \leq n \leq 2 \cdot 10^5$,$n -1 \leq m \leq 2 \cdot 10^5$,$1 \leq k \leq 2 \cdot 10^5$),其中 $n$ 是国家的城市数量,$m$ 是道路数量,$k$ 是选择道路修复集合的方案数。保证 $m \cdot k \leq 10^6$。接下来的 $m$ 行描述道路,每行一条。每行包含两个整数 $a_i$、$b_i$($1 \leq a_i, b_i \leq n$,$a_i \ne b_i$)——表示第 $i$ 条道路连接的两个城市编号。任意两个城市之间至多有一条道路。给定的道路集合保证可以从首都到达任何城市。 ## Output 输出 $t$($1 \leq t \leq k$)——选择道路修复集合的方案数。注意,你需要找出 $k$ 种不同的方案;如果少于 $k$ 种,则需找出所有可能的不同有效方案。接下来的 $t$ 行,每行输出一个方案。每个方案用一个长度为 $m$ 的字符串表示,其中第 $j$ 个字符为 '_1_' 表示第 $j$ 条道路被包含在该方案中,为 '_0_' 表示未被包含。道路编号应按照输入顺序。方案的输出顺序任意,所有 $t$ 行必须互不相同。由于保证 $m \cdot k \leq 10^6$,所有 $t$ 行的总长度不超过 $10^6$。如果有多个答案,输出任意一个即可。 [samples]
**Definitions** Let $ G = (V, E) $ be an undirected, connected graph with: - $ V = \{1, 2, \dots, n\} $: set of cities (vertices), - $ E = \{e_1, e_2, \dots, e_m\} $: set of roads (edges), with $ e_j = (a_j, b_j) $, - Vertex $ 1 $ is the capital. Let $ \mathcal{T} $ be the set of all spanning trees of $ G $ that minimize the total distance from vertex $ 1 $ to all other vertices (i.e., minimize $ \sum_{v \in V \setminus \{1\}} \text{dist}_T(1, v) $). **Constraints** 1. $ 2 \le n \le 2 \cdot 10^5 $ 2. $ n - 1 \le m \le 2 \cdot 10^5 $ 3. $ 1 \le k \le 2 \cdot 10^5 $ 4. $ m \cdot k \le 10^6 $ 5. $ G $ is connected. **Objective** Find up to $ k $ distinct spanning trees $ T \in \mathcal{T} $, where each $ T $ minimizes: $$ \sum_{v=2}^{n} \text{dist}_T(1, v) $$ and output each as a binary string of length $ m $: the $ j $-th character is $ 1 $ if $ e_j \in T $, else $ 0 $. If $ |\mathcal{T}| < k $, output all elements of $ \mathcal{T} $.
Samples
Input #1
4 4 3
1 2
2 3
1 4
4 3
Output #1
2
1110
1011
Input #2
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output #2
1
101001
Input #3
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output #3
2
111100
110110
API Response (JSON)
{
  "problem": {
    "name": "F. Berland and the Shortest Paths",
    "description": {
      "content": "There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1005F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "伯兰有 $n$ 座城市。某些城市对之间由道路连接。所有道路都是双向的。每条道路连接两个不同的城市,任意两个城市之间至多有一条道路。城市编号为 $1$ 到 $n$。\n\n已知从首都(编号为 $1$ 的城市)出发,可以通过道路到达任何其他城市。\n\n伯兰总统计划改善国家的道路网络。预算恰好足够修复 $n -1$ 条道路。总统计划选择一个包含 $n -1$ 条道路的集合,使得:\n\n换句话说,所选的 $n -...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G = (V, E) $ be an undirected, connected graph with:  \n- $ V = \\{1, 2, \\dots, n\\} $: set of cities (vertices),  \n- $ E = \\{e_1, e_2, \\dots, e_m\\} $: set of roads (edges), with ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments