Adrian the Wonder Child

시간 제한2초메모리 제한2048 MB

요약
0과 1로 표시된 간선을 가진 트리에서 최대 m개의 간선 표시를 바꿔, 같은 값이 연속으로 k개 이하인 가장 긴 경로의 길이를 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 그리디, 동적 계획법
정답자
아직 제출이 없습니다

문제

Adrian the Wonder Child started coding for his new project a while ago. Each day he pushed exactly one commit on his Git repository, and now it looks like a tree with nn vertices.

As we all know, Adi has good and bad days (usually one good day followed by ten bad days, but that fact is less important for this problem). So for every node of the tree except the root (initial commit), he labeled the edge to the parent with either 00 or 11 depending on whether he pushed the corresponding commit on a bad day or on a good day.

A path between two nodes in the tree is considered kk-alternating if it has at most kk consecutive edges with the same value.

Looking back on what he did in a particular day, Adi can change his mind on whether it was good or bad; thus, he can flip the label of the edge between the corresponding node and its parent.

Given integers mm and kk, Adi asks himself what is the longest kk-alternating path in the tree that can be obtained by flipping the labels of at most mm edges.

입력

The first line of the input contains three integers: nn, kk, and mm (3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5, 2≤k<n2 \leq k < n and 0≤m<n0 \leq m < n).

Each of the following n−1n - 1 lines contains three integers: xx, yy, and cc (1≤x,y≤n1 \leq x, y \leq n and c∈0,1c \in \\{0, 1\\}) meaning that there is an edge between nodes xx and yy with label cc.

It is guaranteed that the given edges form a tree.

출력

Output a single number representing the size of the longest kk-alternating path obtained by flipping the label of at most mm edges.

예제1

  1. 예제 1

    입력
    9 3 2
    1 2 0
    2 3 1
    3 4 1
    4 5 1
    1 6 1
    1 7 0
    7 8 0
    8 9 1
    
    예상 출력
    7