James Street
After a long day of market making, James decides to go outside. However, the layout of his neighbourhood has always confused him, and he is interested in studying a specific property of the streets within it. The neighbourhood consists of intersections labelled
through
, and
two-way roads connecting them, forming a tree structure.
Additionally, each street has a sign with an integer on it. James loves streets, but he also loves palindromes. When he walks on a path between two intersections, he writes down the value he sees on the sign of each street he passes through on the way. He calls the path balanced if he can rearrange the values written into a palindrome — that is, each of them besides possibly one appears an even amount of times on the path. The palindrome is in terms of the numbers themselves as units, not digit-wise.
The tree of intersections and streets is rooted at James' house, located on intersection . For each intersection
, James wants to find the number of distinct pairs of intersections
in the subtree of
(including
) such that the path between
is balanced. If
counts, then
does not count.
Input
The first line contains a single integer
, the number of intersections in James' neighbourhood.
The next lines each contain the details of a bidirectional road, which are
,
, and
, where
and
are the intersections connected by a street and
is the value of the sign on that street. It is guaranteed that the intersections together with the roads connecting them form a valid tree.
Output
Output integers on a single line, where the
th integer represents the number of distinct unordered pairs of intersections in the subtree of the
th intersection with a valid balanced path between them.
Clarifications
- Each path must refer to the unique simple tree path between two intersections
and
. You cannot visit a node twice, or do any other loops.
- You cannot start and end at the same intersection (i.e., an empty path is not a valid balanced path). However, all one-length paths (and many other types) are balanced.
Example
Input 1
6
1 2 2
2 3 2
2 4 1
1 5 1
5 6 1
Output 1
11 2 0 0 1 0
Firstly, all the leaves obviously have 0 possible paths in the subtree. Then, has a single
length path.
has two direct children, so there are
paths of length
. The pairs in the subtree of
are enumerated as such, where we list the intersections and their palindromic path values:
Input 2
10
1 2 2
2 3 3
2 4 4
1 5 2
5 6 1
5 7 3
3 8 1
4 9 2
7 10 2
Output 2
20 4 1 1 3 0 1 0 0 0
Comments