codeforces-scrape

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

1209D.md (1942B)


      1 Source: https://codeforces.com/problemset/problem/1209/D
      2 
      3 D. Cow and Snacks
      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 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
     22 
     23 There are $n$ snacks flavors, numbered with integers $1, 2, \ldots, n$. Bessie has $n$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
     24 
     25 * First, Bessie will line up the guests in some way.
     26 * Then in this order, guests will approach the snacks one by one.
     27 * Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
     28 
     29 Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
     30 
     31 Input
     32 
     33 The first line contains integers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le 10^5$), the number of snacks and the number of guests.
     34 
     35 The $i$-th of the following $k$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), favorite snack flavors of the $i$-th guest.
     36 
     37 Output
     38 
     39 Output one integer, the smallest possible number of sad guests.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 5 4
     47 1 2
     48 4 3
     49 1 4
     50 3 4
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 1
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 6 5
     63 2 3
     64 2 1
     65 3 4
     66 6 5
     67 4 5
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 0
     74 ```
     75 
     76 Note
     77 
     78 In the first example, Bessie can order the guests like this: $3, 1, 2, 4$. Guest $3$ goes first and eats snacks $1$ and $4$. Then the guest $1$ goes and eats the snack $2$ only, because the snack $1$ has already been eaten. Similarly, the guest $2$ goes up and eats the snack $3$ only. All the snacks are gone, so the guest $4$ will be sad.
     79 
     80 In the second example, one optimal ordering is $2, 1, 3, 5, 4$. All the guests will be satisfied.