#P1156D. 0-1-Tree
0-1-Tree
Description
You are given a tree (an undirected connected acyclic graph) consisting of vertices and edges. A number is written on each edge, each number is either (let's call such edges -edges) or (those are -edges).
Let's call an ordered pair of vertices () valid if, while traversing the simple path from to , we never go through a -edge after going through a -edge. Your task is to calculate the number of valid pairs in the tree.
The first line contains one integer () — the number of vertices in the tree.
Then lines follow, each denoting an edge of the tree. Each edge is represented by three integers , and (, , ) — the vertices connected by this edge and the number written on it, respectively.
It is guaranteed that the given edges form a tree.
Print one integer — the number of valid pairs of vertices.
Input
The first line contains one integer () — the number of vertices in the tree.
Then lines follow, each denoting an edge of the tree. Each edge is represented by three integers , and (, , ) — the vertices connected by this edge and the number written on it, respectively.
It is guaranteed that the given edges form a tree.
Output
Print one integer — the number of valid pairs of vertices.
Note
The picture corresponding to the first example: