codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

330B.md (2818B)


      1 Source: https://codeforces.com/problemset/problem/330/B
      2 
      3 B. Road Construction
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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.
     24 
     25 Input
     26 
     27 The first line consists of two integers *n* and *m* ![](https://espresso.codeforces.com/94f406a06721e89d57a14b6dfade59c6341e93e8.png).
     28 
     29 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*.
     30 
     31 It is guaranteed that every pair of cities will appear at most once in the input.
     32 
     33 Output
     34 
     35 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*.
     36 
     37 If there are several solutions, you may print any of them.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 4 1  
     45 1 3
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 3  
     52 1 2  
     53 4 2  
     54 2 3
     55 ```
     56 
     57 Note
     58 
     59 This is one possible solution of the example:
     60 
     61  ![](https://espresso.codeforces.com/733d8c3eb9daddddea01eb45bc5e9f432c31ee45.png) 
     62 
     63 These are examples of wrong solutions:
     64 
     65  ![](https://espresso.codeforces.com/1f6a6f67a98ba7144b22a4396f530c0512ce6083.png)  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. ![](https://espresso.codeforces.com/1682a2451ec68ed1d2f2fe67485812eff63d2055.png)  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. ![](https://espresso.codeforces.com/95923f7b5de3838c63610bd3b9015403913cbad7.png)  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.