## Editorial for DMOPC '20 Contest 2 P3 - Roadrollification

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

#### Subtask 1

Bruteforce the answer by trying every edge and doing graph traversals or topological sorts to see if roadrollifying that edge yields the best answer.

**Time complexity:**

#### Subtask 2

Perform two topological sorts: one directed along the given edge directions, and one with reversed edge directions. During the topological sorts, keep track of how many people are able to enter into this node from any other node (i.e. for any node that the current node can access, do , where at the beginning). Let denote this sum for the directed topological sort at node and denote the same for the reversed topological sort.

We now note that if there was no need to bidirectionalize an edge, the answer would be given by .

The number of connection increases that result from the roadrollification of an edge is equal to . (This can be understood simply as [the number of people that can flow into that don't flow in from ] [the number of people that can reach excluding any that are already in and its subtree]).

Now the answer is given simply by .

**Time complexity:**

## Comments