#P1629. Efficient Postal Delivery
Efficient Postal Delivery
Efficient Postal Delivery
A postman based at the post office located at node \(1\) has \(n-1\) items to deliver, where each item is destined for a distinct node from \(2\) to \(n\). The city has \(m\) one-way roads. The postman can carry only one item at a time. For each delivery, he must travel from the post office to the delivery node and then return to the post office. After delivering all items, he must be at the post office. Determine the minimum total time required to complete all deliveries.
It is assumed that each road takes \(1\) unit of time to traverse. If it is impossible to deliver an item because a destination is unreachable (either going from \(1\) to that node or returning from that node to \(1\)), output \(-1\).
Note: All formulas are in \(\LaTeX\) format.
inputFormat
The first line of input contains two integers \(n\) and \(m\) \((2 \leq n \leq 10^5,\ 1 \leq m \leq 10^5)\), representing the number of nodes and the number of one-way roads respectively.
The next \(m\) lines each contain two integers \(u\) and \(v\) \((1 \leq u, v \leq n)\), indicating a road from node \(u\) to node \(v\).
outputFormat
Output a single integer representing the minimum total time needed to deliver all \(n-1\) items and return to the post office. If it is impossible to complete any delivery, output \(-1\).
sample
4 5
1 2
2 1
1 3
3 4
4 1
8