RGB트리
InterviewTime limit1.5sMemory limit1024 MB
Color each bulb of a tree red, green, or blue so adjacent bulbs differ, maximizing the total beauty, and print the best sum and coloring.
- Level
Easy3 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
문제
The RGB Tree, a landmark of RGB Street, has light bulbs. The bulbs are numbered 1 to , and each bulb can emit red, green, or blue light. The bulbs are connected in a tree structure, and adjacent bulbs must emit light of different colors. Given the beauty of each bulb for each color, find a coloring that maximizes the sum of beauty.
입력
The first line contains the number of bulbs . ()
From the second line to the th line, each line contains two bulb numbers separated by a space, meaning the two bulbs are adjacent.
From the th line to the th line, each line contains three integers separated by spaces. On line , , , and are the beauty values of bulb when it emits red, green, and blue light. ()
출력
On the first line, print the maximum possible sum of beauty over all bulbs.
On the second line, print the colors of bulbs 1 to in order, without spaces. Print R for red, G for green, and B for blue. If several answers exist, print any of them.