Having nearly completed their logging scheme, The Logging Company now wants to ship all logs collected at their logging station to some sawmills in the region. However, shipping logs with trucks costs money which can be better spent elsewhere (like in cutting more trees). To this end, The Company has devised a scheme where they will utilize the region's unique geography and abundant river networks for free shipping.

The geography of the land consists of some lakes and some rivers connecting them. Curiously, the latitudes of all the lakes are unique, and they are numbered from to from north to south. Even more strange are the facts that all the rivers between lakes only flow *south*, and there may be multiple rivers between the same two lakes. But thanks to this, logs the Company collects from up north can be moved south to their sawmills, for free!

The Logging Company has strategically built their collection point and sawmills next to lakes, the collection point being next to the lake numbered and one sawmill next to each lake with no river flowing south from it.

The Company has built more than one sawmill because free transportation comes with a degree of unpredictability. At any lake which doesn't have a sawmill next to it, a log may randomly flow down one river that leads south, with all possible southbound rivers being chosen with uniform probability. Knowing this, The Company would like to know how many resources to allocate to each sawmill, based on the probability of a log reaching that sawmill. For this, they've hired you to calculate the probability of a log from the collection point arriving at each sawmill.

#### Input Specification

First line: two space-separated integers and .

Next lines: two space separated integers and , representing a river flowing south from lake to lake . There will always be at least one river flowing south from lake .

#### Output Specification

For each sawmill, output the probability of a log reaching it as a real number . Your answer will be accepted as correct if it is within an absolute or relative error of no more than .

#### Sample Input 1

```
6 5
1 2
2 3
2 4
4 5
4 6
```

#### Sample Output 1

```
0.5
0.25
0.25
```

#### Explanation of Output for Sample Input 1

The given scheme can be represented by the following image.

A log starting at has a 1/2 chance of going into the sawmill at lake 3, and a 1/4 chance of reaching sawmills at lake 5 or 6.

#### Sample Input 2

```
5 4
1 2
1 3
2 4
3 4
```

#### Sample Output 2

```
1
0
```

#### Explanation of Output for Sample Input 2

The lake network may be disconnected. In Sample Input 2, lake 5 has no incoming rivers and no outgoing rivers. However, there is a sawmill on lake 5. No wonder the Logging Company is losing money!

## Comments

Can some please check my submission: https://dmoj.ca/src/3269159. It uses the same approach as the editorial but only ACs half the cases.

You don't need to toposort. Also, your toposort is wrong since you aren't starting with all nodes with zero indegree

Thanks!

This problem helped me better understand floating-point precision!

could someone please check my code? I feel like my solution is correct but I only AC 2 test cases. Also I checked the editorial and my solution is the same as the proposed solution. I must have implemented something wrong

Floats don't have enough accuracy. Changing your dp vector from float to double will make your code AC.

Thanks for the help!

Hello,

I'm having trouble optimizing my code. It already seems to resemble the editorial's algorithm. Can anyone please offer a suggestion?

Thanks

Hello,

The code that you have commented out in your submission is optimized. Make an ArrayList for every lake at the start, instead of checking if it is null and then making one when getting input. When printing output for sawmills, check if ArrayList size is 0 instead of if the ArrayList is null.

Please visit the DMOJ Discord for further help on problems.

Hope this helps

Do the sawmills have to be in any specific order of output?

yes, output the probability of a log reaching the th sawmill on the th line

Why does my code raise a Value Error? It runs the sample cases just fine, but IRs in several of the test cases.

An integer takes up 4 bytes (on C++ and Java). This means an array of (1e6)^2 will take up 4 TB, which is way over the memory limit. On Python, an integer is as much as 24 bytes.

The problems just keep on coming...

For anyone solving this problem using C++ in the future, if you aren't already, it'll probably be useful to use scanf() AND printf() for this problem. RIP my 1 hour.

This question takes an incredibly long time to judge :<

The problem uses a generator to generate the input cases on the fly. It takes some time for the generator to run for all 10 cases.

Is the output in order of increasing destination lake number?

Yes.

If there are lakes 1 2 and 3, then lake 1 has 2 connections to lake 2 but 1 connection to lake 1, then does lake 2 have 2/3 chance or 1/2 chance.

sorry I meant have lake 1 have 1 connection to lake 3

2/3 assuming connections 1-->2, 1-->2, 1--3