아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Islands Tour

시간 제한1초메모리 제한1024 MB

요약
각 정점의 나가는 간선이 최대 하나인 방향 그래프에서 같은 섬을 두 번 방문하지 않는 최장 경로의 길이를 구한다.
난이도

보통10점 중 5점

유형
그래프, DFS, 시뮬레이션
정답자
아직 제출이 없습니다

문제

There are beautiful islands connected with zip-lines. A tourist can go from one island to another island sliding through a zip-line that connects the islands. Sliding through a zip-line above sunset sea, a tourist can see breathtaking sceneries of nature with twinkling lights over the sunlit sea waters. These islands are fantastic attractions among tourists. Each island is full of flowers of numerous colors. Travelling from an arbitrary island, a tourist called Optimizer wants to visit as many distinct islands as possible.

The islands are represented as a directed graph G(V,E)G(V, E). A zip-line from an island vv to another island ww is represented as a directed edge (v,w)∈E(v, w) ∈ E. We assume that each island has at most one outgoing zip-line, that is, for each vertex v∈Vv ∈ V, we have at most one outgoing edge.

For example, the figure below shows an example of the islands represented as a directed graph.

The dotted path in the following graph denotes a longest tour that visits as many distinct islands as possible.

Given a directed graph G(V,E)G(V, E) that represents the islands and their connections using zip-lines, write a program to output the maximum number of islands that can be visited by Optimizer. Note that Optimizer can start from an arbitrary island and cannot visit the same island twice or more.

입력

Your program is to read from standard input. The input starts with a line containing two integers, mm and nn (0≤m≤n≤1,000,0000 ≤ m ≤ n ≤ 1\\,000\\,000), where mm is the number of zip-line connections (edges) and nn is the number of islands (vertices). The islands are numbered from 00 to n−1n - 1. In the following mm lines, the ii-th line contains two integers v_iv\_i and w_iw\_i that represent a directed edge (v_i,w_i)(v\_i , w\_i) from v_iv\_i to w_iw\_i. We assume that each vertex has at most one outgoing edge.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the maximum number of distinct islands that can be visited by riding zip-lines starting from an arbitrary island.

예제4

  1. 예제 1

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

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

    입력
    2 2
    1 0
    0 1
    
    예상 출력
    2
    
  4. 예제 4

    입력
    0 4
    
    예상 출력
    1