codeforces-scrape

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

1559D1.md (2248B)


      1 Source: https://codeforces.com/problemset/problem/1559/D1
      2 
      3 D1. Mocha and Diana (Easy Version)
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if all versions of the problem are solved.
     22 
     23 A forest is an undirected graph without cycles (not necessarily connected).
     24 
     25 Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $1$ to $n$, and they would like to add edges to their forests such that:
     26 
     27 * After adding edges, both of their graphs are still forests.
     28 * They add the same edges. That is, if an edge $(u, v)$ is added to Mocha's forest, then an edge $(u, v)$ is added to Diana's forest, and vice versa.
     29 
     30 Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.
     31 
     32 Input
     33 
     34 The first line contains three integers $n$, $m_1$ and $m_2$ ($1 \le n \le 1000$, $0 \le m_1, m_2 \lt n$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.
     35 
     36 Each of the next $m_1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) — the edges in Mocha's forest.
     37 
     38 Each of the next $m_2$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) — the edges in Diana's forest.
     39 
     40 Output
     41 
     42 The first line contains only one integer $h$, the maximum number of edges Mocha and Diana can add (in each forest).
     43 
     44 Each of the next $h$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) — the edge you add each time.
     45 
     46 If there are multiple correct answers, you can print any one of them.
     47 
     48 Examples
     49 
     50 Input
     51 
     52 ```
     53 3 2 2
     54 1 2
     55 2 3
     56 1 2
     57 1 3
     58 ```
     59 
     60 Output
     61 
     62 ```
     63 0
     64 ```
     65 
     66 Input
     67 
     68 ```
     69 5 3 2
     70 5 4
     71 2 1
     72 4 3
     73 4 3
     74 1 4
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 1
     81 2 4
     82 ```
     83 
     84 Input
     85 
     86 ```
     87 8 1 2
     88 1 7
     89 2 6
     90 1 5
     91 ```
     92 
     93 Output
     94 
     95 ```
     96 5
     97 5 2
     98 2 3
     99 3 4
    100 4 7
    101 6 8
    102 ```
    103 
    104 Note
    105 
    106 In the first example, we cannot add any edge.
    107 
    108 In the second example, the initial forests are as follows.
    109 
    110 ![](https://espresso.codeforces.com/45bf5e3cb797d3297f192b72d636672e36927915.png)
    111 
    112 We can add an edge $(2, 4)$.
    113 
    114 ![](https://espresso.codeforces.com/6381c3c8fea706ce0dd2912abf0d052da6f55f46.png)