codeforces-scrape

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

107A.md (2811B)


      1 Source: https://codeforces.com/problemset/problem/107/A
      2 
      3 A. Dorm Water Supply
      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 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 The German University in Cairo (GUC) dorm houses are numbered from 1 to *n*. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).
     22 
     23 For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.
     24 
     25 In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
     26 
     27 Input
     28 
     29 The first line contains two space-separated integers *n* and *p* (1 ≤ *n* ≤ 1000, 0 ≤ *p* ≤ *n*) — the number of houses and the number of pipes correspondingly.
     30 
     31 Then *p* lines follow — the description of *p* pipes. The *i*-th line contains three integers *a**i* *b**i* *d**i*, indicating a pipe of diameter *d**i* going from house *a**i* to house *b**i* (1 ≤ *a**i*, *b**i* ≤ *n*, *a**i* ≠ *b**i*, 1 ≤ *d**i* ≤ 106).
     32 
     33 It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
     34 
     35 Output
     36 
     37 Print integer *t* in the first line — the number of tank-tap pairs of houses.
     38 
     39 For the next *t* lines, print 3 integers per line, separated by spaces: *tank**i*, *tap**i*, and *diameter**i*, where *tank**i* ≠ *tap**i* (1 ≤ *i* ≤ *t*). Here *tank**i* and *tap**i* are indexes of tank and tap houses respectively, and *diameter**i* is the maximum amount of water that can be conveyed. All the *t* lines should be ordered (increasingly) by *tank**i*.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 3 2  
     47 1 2 10  
     48 2 3 20
     49 ```
     50 
     51 Output
     52 
     53 ```
     54 1  
     55 1 3 10
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 3 3  
     62 1 2 20  
     63 2 3 10  
     64 3 1 5
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 0
     71 ```
     72 
     73 Input
     74 
     75 ```
     76 4 2  
     77 1 2 60  
     78 3 4 50
     79 ```
     80 
     81 Output
     82 
     83 ```
     84 2  
     85 1 2 60  
     86 3 4 50
     87 ```