Traffic (Small)
InterviewTime limit2sMemory limit512 MB
Given a tree and Q tickets, count how many tickets use each edge along the unique path, then report the edge with the largest count (smallest station pair on ties).
- Level
Medium5 of 10
- Topics
- Tree, Prefix sum, Linked list, DFS
- Solved
- No attempts yet
Problem
The city state CINERIS has N train stations, and the rails that connect them form a tree, so exactly one route runs between any two different stations. Jeongmin, who rules the country, wants to know which rail carried the most people. Q tickets have been sold so far, and a ticket records only its departure station and its arrival station.
Everyone in CINERIS travels along the shortest route. A tree holds exactly one route between two stations, so one ticket passes each rail on the route from its departure station to its arrival station exactly once. A ticket whose departure and arrival stations are the same passes no rail. From the Q tickets, find the rail that carried the most people.
Input
The first line contains N and Q separated by a space.
Each of the next N - 1 lines contains two integers a and b, meaning a two way rail connects station a and station b.
Each of the next Q lines contains the departure station c and the arrival station d of one ticket, meaning the buyer left station c and arrived at station d.
, , , and the rails always form a tree.
Output
Print the rail that carried the most people and how many people passed it, as a b c on one line. It means c people passed the rail connecting station a and station b. Write the two station numbers in increasing order so that . If several rails carry the maximum, print the one whose pair (a, b) is smallest in lexicographic order.