Jeju Island Tour
Time limit1sMemory limit128 MB
Pick two vertex-disjoint directed paths in a DAG so the total number of vertices on both paths is as large as possible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Topological sort
- Solved
- No attempts yet
Problem
Many tourists visit Jeju Island. The island has intersections and one way roads. Each road runs from one intersection to another, and several roads may connect the same pair of intersections.
The roads on Jeju Island are laid out in a very strange shape. Whichever intersection you start from, you can never follow the roads and get back to the intersection you started at. (So how do the people who live there get to work?)
Two groups are planning a trip around the island. Each group starts at one intersection and travels along the roads. The two groups get along badly, so they must not pass through the same intersection. You have to pick two routes and , where () starts at intersection and finishes at intersection , and the two routes must not share a single intersection. The starting and finishing points must not overlap either. A route containing only one intersection is allowed ().
Write a program that maximizes the total number of intersections on the two routes.
Input
The first line contains the number of test cases ().
The first line of each test case contains the number of intersections and the number of roads (, ). The intersections are numbered from to . Each of the next lines contains two integers and describing one road, meaning there is a one way road from intersection to intersection .
Output
For each test case, print on its own line the largest possible total number of intersections on two routes that satisfy the conditions.