Planning a Trip
Time limit2sMemory limit128 MB
Given a directed graph, find the maximum number of distinct cities visitable on a walk from S to T, allowing revisits of cities and edges.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
Taehui wants to get away from work and go on vacation. After some research, Taehui chooses cities to visit. At first, it seemed that flights would be enough to plan the trip, but not every pair of cities is connected by a flight.
There are flight routes in total, and each route can be used in only one direction. Taehui will start in city and finish the trip in city .
Given the cities and flight routes, find the maximum number of distinct cities that can be visited during a trip from city to city . A city is counted only once even if it is visited multiple times. Cities may be revisited any number of times, and the same flight route may also be used multiple times.
Input
The first line contains four integers , , , and .
Each of the next lines contains two distinct integers and describing one flight route. This means there is a route from city to city .
Output
Print the maximum number of distinct cities that can be visited. If it is impossible to travel from city to city , print 0.