Given an undirected tree and f vertex sets, each a simple path, decide whether the family of paths is laminar.
Hard9TreeDFSPrefix sumSortingNo attempts yetTime limit2sMemory limit512 MBWhile studying combinatorial optimization, Lucas came across laminar set families. A family F of subsets of a set Ω is laminar if F contains no empty set and, for any two distinct sets A,B∈F, one of A⊂B, B⊂A, A∩B=∅ holds.
Lucas turns everything he learns into a contest problem. He likes recognition problems that ask whether a given structure satisfies some combinatorial property, and he believes a good problem has a tree in it. So he combined the two ideas into the following problem.
You are given an undirected tree on n vertices and a family F={F1,…,Ff}. The set Fi consists of every vertex on the simple path between vertices ai and bi of the tree. Here Ω=V, and Fi⊆V for every i. Decide whether the family F is laminar.
The same set may be given more than once. The laminar condition applies only to pairs of distinct sets, so repeated sets never break it.
The first line contains two integers n and f, the number of vertices in the tree and the number of elements in the family F (1≤n,f≤100000).
The next n−1 lines describe the tree. The i-th of them contains two integers ui and vi, the vertices joined by the i-th edge (1≤ui,vi≤n, ui=vi).
The next f lines describe the sets of the family. The i-th of them contains two integers ai and bi (1≤ai,bi≤n), meaning that Fi consists of every vertex on the simple path between ai and bi, including both endpoints.
Print Yes on a single line if the given family is laminar, and No otherwise.