Helping the Transit
InterviewTime limit1sMemory limit1024 MB
Given N reference points and M one-way streets, find the fewest one-way bridges to add so that every point can reach every other point.
- Level
Medium5 of 10
- Topics
- Graph, DFS, Topological sort
- Solved
- No attempts yet
Problem
The president of Nlogonia decided, by decree, that all the streets of Nlogonia should be one-way. Because no one planned the change properly, people could not get to work or could not return home from work. The result was chaos and riots in many cities.
The president was impeached, and the new administration hired a team of scientists to solve the problem. The team then hired you, an expert in algorithm complexity, to help compute the solutions efficiently.
Each city has reference points, and one-way streets, each connecting two reference points. Find the minimum number of one-way bridges that must be built to make the city fully connected. Each bridge also connects two reference points.
Input
The first line contains two integers and (, ), where is the number of reference points and is the number of streets. Each of the next lines contains two integers and (, ), meaning a street runs from to . Every vehicle on that street must move away from toward .
Output
Print a single line containing the minimum number of bridges needed to make the inhabitants happy.