Highways of the Future

시간 제한6초메모리 제한2048 MB

요약
일부 구역의 원자로가 꺼져도 남은 원자로가 모든 구역에 전력을 공급하도록 추가할 최소 방향 간선 수를 구한다.
난이도

어려움10점 중 8점

유형
그래프, 최소 신장 트리, 그리디, 위상 정렬
정답자
아직 제출이 없습니다

문제

Midgard is a city of mana energy and center of the world's economy. With its development steered by President Shinda, chair of the Shinda electric power company, the great city has undergone an age of great prosperity. Recently, however, there have been reports that a group of bandits called Snowfall has been attacking and shutting down its mana reactors. In preparation for another attack, President Shinda has assigned you the role of chief engineer in a restructuring plan.

The city consists of nn sectors, each of which has a mana reactor: an enormous facility which extracts energy from deep within the earth and transforms it into electricity powering the whole sector. Currently, there are mm highways between sectors. Each highway can be used to transport electric power from sector to sector and has only one direction. The president has instructed you to build new highways between sectors, such that no matter which reactors get shut down, the entire city will still have electrical power as long as there is at least 11 reactor functioning.

Each reactor has an unlimited capacity for mana energy and can supply any number of sectors as long as it is directly or indirectly connected to them. Additionally, due to the sheer cost of building a highway, the president has instructed you to build as few highways as possible, while still satisfying his previous condition.

입력

The input consists of:

  • A line containing two integers nn (1≤n≤1051\leq n\leq 10^5) and mm (0≤m≤2⋅1050\leq m\leq 2\cdot 10^5), representing the number of sectors in Midgard and the number of existing highways, respectively.
  • Then follow mm lines containing two integers each, xx and yy (1≤x,y≤n1 \leq x,y \leq n), which indicate the presence of a one-directional highway from sector xx to sector yy.

출력

Output the minimum number of highways that have to be added to have any reactor be able to power every sector.

예제3

  1. 예제 1

    입력
    3 2
    1 2
    1 3
    
    예상 출력
    2
    
  2. 예제 2

    입력
    8 9
    1 3
    5 1
    2 5
    2 6
    3 4
    3 5
    5 6
    8 5
    3 7
    
    예상 출력
    3
    
  3. 예제 3

    입력
    9 9
    1 3
    5 1
    2 5
    2 6
    3 4
    3 5
    8 5
    7 3
    3 7
    
    예상 출력
    3