Squares and Lanterns
Time limit2sMemory limit1024 MB
Given a tree where each square has a lantern range, find which squares can serve as a center where every leaf-to-center path has the same lit-lantern total while all squares meet their minimums.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
In 15th-century Barcelona there were squares. Some squares are connected by two-way roads. There are roads in total, and from every square you can reach every other square along these roads, so the squares form a tree with vertices.
Square has lanterns. Callum will find it easier to fight the Templars if the city is better lit, so he wants to switch on some lanterns in some squares. Square is lit well enough only if at least lanterns are on there.
Callum calls a square peripheral if it is connected by exactly one road to another square.
Callum calls a square bright if there is a way to switch on some lanterns in each square so that every square is lit well enough, and the number of lit lanterns on the path from to every peripheral square other than is the same.
The number of lit lanterns on a path is the total number of lit lanterns on all squares of that path.
Help Callum determine which squares are bright.
Input
The first line contains a natural number (the number of squares), with .
Each of the next lines contains two natural numbers and (, ), the numbers of the squares connected by the road.
Each of the next lines contains two non-negative integers and (), the minimum and maximum number of lanterns that can be switched on in square .
It is guaranteed that the squares form a tree.
Output
Print numbers on one line. Print 1 if square is bright, and 0 otherwise.