Rainbow Roads
Time limit2sMemory limit512 MB
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 nodes, numbered from 1 to . Every edge has one of colors. A path in the tree is a rainbow path if every two adjacent edges on it have different colors. A node is good if every simple path that has 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 ().
Each of the next lines contains three integers , , and separated by spaces (, ). They describe an edge of color that connects node and node .
The given edges always form a tree.
Output
On the first line, print , the number of good nodes.
On each of the next 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 the edges and 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.