Under construction

Still being built. Things may change

Adrian the Wonder Child

Time limit2sMemory limit2048 MB

Summary
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 nn 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 00 or 11. The label is 00 if he pushed that commit on a bad day and 11 if he pushed it on a good day.

A path between two vertices is kk-alternating if it has at most kk 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 mm and kk, find the length of the longest kk-alternating path that can be obtained by flipping the labels of at most mm edges.

Input

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

Each of the next n−1n - 1 lines contains three integers xx, yy, and cc (1≤x,y≤n1 \leq x, y \leq n, c∈{0,1}c \in \{0, 1\}), meaning there is an edge between xx and yy with label cc. The given edges form a tree.

Output

Print a single number: the size of the longest kk-alternating path that can be obtained by flipping the labels of at most mm edges.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    7