codeforces-scrape

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

893C.md (2599B)


      1 Source: https://codeforces.com/problemset/problem/893/C
      2 
      3 C. Rumor
      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 Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
     22 
     23 Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
     24 
     25 Vova knows that there are *n* characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; *i*-th character wants *c**i* gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
     26 
     27 The quest is finished when all *n* characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
     28 
     29 Take a look at the notes if you think you haven't understood the problem completely.
     30 
     31 Input
     32 
     33 The first line contains two integer numbers *n* and *m* (1 ≤ *n* ≤ 105, 0 ≤ *m* ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
     34 
     35 The second line contains *n* integer numbers *c**i* (0 ≤ *c**i* ≤ 109) — the amount of gold *i*-th character asks to start spreading the rumor.
     36 
     37 Then *m* lines follow, each containing a pair of numbers (*x**i*, *y**i*) which represent that characters *x**i* and *y**i* are friends (1 ≤ *x**i*, *y**i* ≤ *n*, *x**i* ≠ *y**i*). It is guaranteed that each pair is listed at most once.
     38 
     39 Output
     40 
     41 Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 5 2  
     49 2 5 3 4 8  
     50 1 4  
     51 4 5
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 10
     58 ```
     59 
     60 Input
     61 
     62 ```
     63 10 0  
     64 1 2 3 4 5 6 7 8 9 10
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 55
     71 ```
     72 
     73 Input
     74 
     75 ```
     76 10 5  
     77 1 6 2 7 3 8 4 9 5 10  
     78 1 2  
     79 3 4  
     80 5 6  
     81 7 8  
     82 9 10
     83 ```
     84 
     85 Output
     86 
     87 ```
     88 15
     89 ```
     90 
     91 Note
     92 
     93 In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
     94 
     95 In the second example Vova has to bribe everyone.
     96 
     97 In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.