Highways of the Future
InterviewTime limit6sMemory limit2048 MB
Given a directed graph of sectors and one-way highways, find the fewest new directed highways needed so every sector can reach every other sector.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Topological sort
- Solved
- No attempts yet
Problem
Midgard is a city of mana energy and the center of the world's economy. Under President Shinda, chair of the Shinda Electric Power Company, the great city has enjoyed an age of prosperity. Recently, however, reports say that a group of bandits called Snowfall has been attacking and shutting down its mana reactors. To prepare for another attack, President Shinda has assigned you the role of chief engineer in a restructuring plan.
The city consists of sectors, and each sector has a mana reactor: an enormous facility that extracts energy from deep within the earth and transforms it into electricity that powers the whole sector. Currently, there are highways between sectors. Each highway can transport electric power from one sector to another, and it has only one direction. The president has instructed you to build new highways between sectors so that, no matter which reactors are shut down, the entire city still has electrical power as long as at least reactor is functioning.
Each reactor has unlimited capacity for mana energy and can supply any number of sectors as long as it is directly or indirectly connected to them. Building a highway is expensive, so the president has also instructed you to build as few highways as possible while still meeting the previous condition.
Input
The input consists of:
- A line containing two integers () and (), the number of sectors in Midgard and the number of existing highways.
- Then follow lines containing two integers each, and (), which indicate a one-directional highway from sector to sector .
Output
Output the minimum number of highways that must be added so that any reactor can power every sector.