#LCA0000055. Misha, Grisha và tàu điện ngầm (Misha, Grisha and Underground)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Misha, Grisha và tàu điện ngầm (Misha, Grisha and Underground)
Misha, Grisha and Underground
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A metro network consists of stations connected as a tree. Each day, three given stations are assigned the roles of Misha's start , Grisha's start , and their common destination .
Misha travels along the shortest path from to and marks every station visited, endpoints included. Grisha then travels from to and counts marked stations on his path. Choose an assignment of the three stations that maximizes this count. The three provided stations need not be distinct.
Input
- The first line contains .
- The next line contains integers , defining an edge between each and .
- The next lines each contain three stations .
Output
Print the maximum possible number of marked stations Grisha can visit for each day.
Subtasks
General constraints: ; ; các cạnh tạo thành cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
3 2
1 1
1 2 3
2 3 3
Output
2
3
Explanation
The edges are and . For , assign : Misha marks and Grisha's route sees two marks. For , assign ; both follow , giving three marked stations.