This page is still under construction.

Parts of this page are still being built. What you see may change.

Rainbow Roads

Time limit2sMemory limit512 MB

Summary
Given a tree whose edges are colored, find every node v such that all simple paths starting at v have no two consecutive edges of the same color.
Level

Hard8 of 10

Topics
Tree, DFS, Graph, Implementation
Solved
No attempts yet

Problem

You are given a tree with nn nodes, numbered from 1 to nn. Every edge has one of nn colors. A path in the tree is a rainbow path if every two adjacent edges on it have different colors. A node vv is good if every simple path that has vv as one of its endpoints is a rainbow path.

Find all the good nodes of the given tree.

A simple path is a path that repeats no vertex and no edge.

Input

The first line contains one integer nn (1≤n≤50 0001 \le n \le 50\,000).

Each of the next n−1n - 1 lines contains three integers aia_i, bib_i, and cic_i separated by spaces (1≤ai,bi,ci≤n1 \le a_i, b_i, c_i \le n, ai≠bia_i \ne b_i). They describe an edge of color cic_i that connects node aia_i and node bib_i.

The given edges always form a tree.

Output

On the first line, print kk, the number of good nodes.

On each of the next kk lines, print the index of one good node, in increasing order.

Hint

The rainbow condition applies only to consecutive edges of a path. On a path v1,v2,v3,v4v_1, v_2, v_3, v_4 the edges (v1,v2)(v_1, v_2) and (v3,v4)(v_3, v_4) may share a color, because they are not adjacent. A path with at most one edge has no adjacent pair of edges, so it is always a rainbow path.

Examples4

  1. Example 1

    Input
    8
    1 3 1
    2 3 1
    3 4 3
    4 5 4
    5 6 3
    6 7 2
    6 8 2
    
    Expected output
    4
    3
    4
    5
    6
    
  2. Example 2

    Input
    8
    1 2 2
    1 3 1
    2 4 3
    2 7 1
    3 5 2
    5 6 2
    7 8 1
    
    Expected output
    0
    
  3. Example 3

    Input
    9
    1 2 2
    1 3 1
    1 4 5
    1 5 5
    2 6 3
    3 7 3
    4 8 1
    5 9 2
    
    Expected output
    5
    1
    2
    3
    6
    7
    
  4. Example 4

    Input
    10
    9 2 1
    9 3 1
    9 4 2
    9 5 2
    9 1 3
    9 6 4
    1 8 5
    1 10 5
    6 7 9
    
    Expected output
    4
    1
    6
    7
    9