codeforces-scrape

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

574B.md (2333B)


      1 Source: https://codeforces.com/problemset/problem/574/B
      2 
      3 B. Bear and Three Musketeers
      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 Do you know a story about the three musketeers? Anyway, you will learn about its origins now.
     22 
     23 Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.
     24 
     25 There are *n* warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.
     26 
     27 Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.
     28 
     29 Input
     30 
     31 The first line contains two space-separated integers, *n* and *m* (3 ≤ *n* ≤ 4000, 0 ≤ *m* ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.
     32 
     33 *i*-th of the following *m* lines contains two space-separated integers *a**i* and *b**i* (1 ≤ *a**i*, *b**i* ≤ *n*, *a**i* ≠ *b**i*). Warriors *a**i* and *b**i* know each other. Each pair of warriors will be listed at most once.
     34 
     35 Output
     36 
     37 If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 5 6  
     45 1 2  
     46 1 3  
     47 2 3  
     48 2 4  
     49 3 4  
     50 4 5
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 2
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 7 4  
     63 2 1  
     64 3 6  
     65 5 1  
     66 1 7
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 -1
     73 ```
     74 
     75 Note
     76 
     77 In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.
     78 
     79 The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.
     80 
     81 In the second sample there is no triple of warriors knowing each other.