QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 1024 MB 満点: 100

#3758. 2019

統計

Bobo has a tree with $n$ vertices, labeled $1, 2, \dots, n$. The tree has $(n - 1)$ edges, where the $i$-th edge connects $a_i$ and $b_i$ with a weight $c_i$. Find the number of pairs $(u, v)$ such that $u < v$ and the sum of weights on the path between $u$ and $v$ is a multiple of $2019$.

Input

The input contains multiple test cases. Process until the end of the file.

Each test case starts with an integer $n$.

The next $(n - 1)$ lines each contain three integers $a_i$, $b_i$, and $c_i$.

  • $n \leq 2 \times 10^4$
  • $1 \leq a_i, b_i \leq n$
  • $0 \leq c_i < 2019$
  • The sum of $n$ over all test cases does not exceed $10^5$.

Output

For each test case, output an integer representing the required count.

Examples

Input 1

4
1 2 1
1 3 2018
1 4 1
4
1 2 0
1 3 0
1 4 0
3
1 2 1
2 3 1

Output 1

2
6
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.