Jonathan Sumabat 7

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type

Jonathan Sumabat is taking a stroll down the park one day when all of a sudden he is struck down by lightning and dies. The next time Jonathan Sumabat opens his eyes he finds himself in another world with his smartphone equipped only with a GPS navigation app. In order to leave this realm and return back to earth Jonathan Sumabat must use a secret passphrase. This passphrase is a number that is the sum of other numbers scattered throughout the world Jonathan Sumabat finds himself in. In this world, the roads can only be traveled through in one direction and there will never be a road that returns Jonathan Sumabat to where he was. The numbers Jonathan Sumabat needs to form the passphrase are located at the end of each road that cannot be followed through to anywhere else. In other words, there is a number at the end of every road where if Jonathan Sumabat were to arrive there, he would be effectively stuck with no roads left he can take. Given the roads in the realm Jonathan Sumabat finds himself in, determine the passphrase he needs to escape.

Input Specification

The first line is n (1 \le n \le 10^5) representing the number of destinations in the realm.

  • Each road leads from one of these destinations to another. They are numbered 1 to n.
  • The piece of the passphrase obtained from each dead end is equal to the number of the destination.
  • Jonathan Sumabat starts at an additional destination marked 0.
  • It is guaranteed that it is possible to travel to any destination from this starting point.
  • The number of destinations is also equal to the number of roads.

The next n lines contain two space separated integers each representing a road. The numbers n_1, n_2 (0 \le n_1, n_2 \le n) represent where the road starts and where it ends respectively.

Output Specification

On a single line, output the passphrase. The passphrase is the sum of all dead end destinations in the realm.

Sample Input

8
0 1
1 4
1 6
1 5
0 2
0 3
3 7
3 8

Sample Output

32

Explanation for Sample

The new realm in which Jonathan Sumabat has awakened is structured like the following picture.

realm

The destinations marked green represent where a piece of the passphrase must be retrieved from. The number of the destination is the piece needed. The sum of these numbers is 32.


Comments

There are no comments at the moment.