codeforces-scrape

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

731C.md (2986B)


      1 Source: https://codeforces.com/problemset/problem/731/C
      2 
      3 C. Socks
      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 Arseniy is already grown-up and independent. His mother decided to leave him alone for *m* days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
     22 
     23 Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's *n* socks is assigned a unique integer from 1 to *n*. Thus, the only thing his mother had to do was to write down two integers *l**i* and *r**i* for each of the days — the indices of socks to wear on the day *i* (obviously, *l**i* stands for the left foot and *r**i* for the right). Each sock is painted in one of *k* colors.
     24 
     25 When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses *k* jars with the paint — one for each of *k* colors.
     26 
     27 Arseniy wants to repaint some of the socks in such a way, that for each of *m* days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.
     28 
     29 The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of *m* days.
     30 
     31 Input
     32 
     33 The first line of input contains three integers *n*, *m* and *k* (2 ≤ *n* ≤ 200 000, 0 ≤ *m* ≤ 200 000, 1 ≤ *k* ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.
     34 
     35 The second line contain *n* integers *c*1, *c*2, ..., *c**n* (1 ≤ *c**i* ≤ *k*) — current colors of Arseniy's socks.
     36 
     37 Each of the following *m* lines contains two integers *l**i* and *r**i* (1 ≤ *l**i*, *r**i* ≤ *n*, *l**i* ≠ *r**i*) — indices of socks which Arseniy should wear during the *i*-th day.
     38 
     39 Output
     40 
     41 Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 3 2 3  
     49 1 2 3  
     50 1 2  
     51 2 3
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 2
     58 ```
     59 
     60 Input
     61 
     62 ```
     63 3 2 2  
     64 1 1 2  
     65 1 2  
     66 2 1
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 0
     73 ```
     74 
     75 Note
     76 
     77 In the first sample, Arseniy can repaint the first and the third socks to the second color.
     78 
     79 In the second sample, there is no need to change any colors.