Tree Trips
Practice
5 (3 votes)
Breadth First search
Brute force
Algorithms
Depth first search
Graphs
Disjoint set union
Bitmask
Problem
84% Success 232 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Bob is going to make \(m\) trips on a tree. During each trip he goes from a node, \(x_{i}\), to another node, \(y_{i}\), using the simple path between the two nodes. 

Alice is jealous of Bob, and doesn't want him to be able to go on any of the \(m\) trips. Before Bob wakes up, Alice plans to remove some edges in the tree, in a way that Bob wouldn't be able to make any of the \(m\) trips

Please, help Alice find out the minimum number of edges she should remove from the tree, so Bob cannot make any of the trips. 

 

INPUT FORMAT

The first line of input contains two integers, \(n\) \((2 <= n <= 15)\) and \(m\) \((1 <= m <= \frac{n(n - 1)}{2})\) - denoting the number of nodes in the tree, and the number of trips Bob wants to make. 

The next \(n - 1\) lines each contain two integers, \(u_{i} (1 <= u_{i} <= n)\) and \(v_{i} (1 <= v_{i} <= n)\) (\(u_{i} < v_{i}\)) - denoting the edges in the tree. 

The last \(m\) lines each contain two integers, \(x_{i} (1 <= x_{i} <= n)\) and \(y_{i} (1 <= y_{i} <= n)\) \((x_{i} \neq y_{i})\) - denoting start and end nodes of the trips Bob wants to make. 

 

OUTPUT FORMAT

The minimum number of edges Alice should remove from the tree to make the trips impossible. 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
AlgorithmsApprovedDepth First SearchGrammar-VerifiedGraphsMathMediumRecruit
Points:30
6 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium
Points:30
16 votes
Tags:
AlgorithmsApprovedGraphsMediumOpenTrees