Adrian the Wonder Child
Time limit2sMemory limit2048 MB
Given a labeled tree, find the longest path that has at most k consecutive equal edge labels after flipping at most m edges.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Two pointers
- Solved
- No attempts yet
Problem
Adrian the Wonder Child started coding for his new project a while ago. Each day he pushed exactly one commit to his Git repository, so the repository now forms a tree with vertices.
Statement
Adi has good days and bad days. (Usually one good day is followed by ten bad days, but that fact does not matter for this problem.) For every vertex except the root (the initial commit), he labels the edge to its parent with or . The label is if he pushed that commit on a bad day and if he pushed it on a good day.
A path between two vertices is -alternating if it has at most consecutive edges with the same label. Adi can change his mind about any day, which flips the label of the edge between that commit's vertex and its parent.
Given integers and , find the length of the longest -alternating path that can be obtained by flipping the labels of at most edges.
Input
The first line contains three integers , , and (, , ).
Each of the next lines contains three integers , , and (, ), meaning there is an edge between and with label . The given edges form a tree.
Output
Print a single number: the size of the longest -alternating path that can be obtained by flipping the labels of at most edges.