Description
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It isnot guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is consideredseparate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
Input
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ <

博客介绍了如何进行图的改革,将双向道路变为单向,以最小化孤立城市数量。通过分析发现,关键是判断图中是否存在环。如果存在环,则可以从环中选择一个起点,避免出现入度为0的城市;若无环,至少需要一个入度为0的城市。博主提出通过遍历图并判断环来解决此问题。
最低0.47元/天 解锁文章
1434

被折叠的 条评论
为什么被折叠?



