#P15935. [TOPC 2021] Garden Park

[TOPC 2021] Garden Park

题目描述

In the Garden park, there are nn places of interest (numbered from 1 to nn) and n1n - 1 trails (numbered from 1 to n1n - 1) connecting the places of interest. For every i{1,2,,n1}i \in \{1, 2, \dots, n - 1\}, trail ii has two ends at place aia_i and place bib_i, and the trail does not pass any place of interest except its ends. Moreover, the trails do not have any intersection except the ends.

To protect the garden, visitors may only walk along the trails (in any direction) and inside the places of interest. For any pair of places of interest (x,y)(x, y) where xyx \neq y, there exists a sequence of trails s1,s2,,sks_1, s_2, \dots, s_k satisfying the following conditions.

  • Place xx is an end of trail s1s_1.
  • Place yy is an end of trail sks_k.
  • For 1i<k1 \leq i < k, trail sis_i and trail si+1s_{i+1} have a common end.
  • If place zz is the common end of trails sis_i and si+1s_{i+1} for some i{1,,k1}i \in \{1, \dots, k - 1\}, then zz cannot be a common end of any other pairs of trails in s1,,sks_1, \dots, s_k.

In other words, a visitor move from xx to yy by walking along the trails s1,s2,,sks_1, s_2, \dots, s_k without visiting a place of interest twice. Such a sequence is called a simple path from xx to yy.

The administration division of the park plans to host an event in the park. It puts labels on the trails. For trail tt, the label on tt is an integer (t)\ell(t), and a visitor can learn (t)\ell(t) by walking through trail tt. A simple path s1,s2,,sks_1, s_2, \dots, s_k from xx to yy is with strictly increasing labels if (s1)<(s2)<<(sk)\ell(s_1) < \ell(s_2) < \cdots < \ell(s_k). By reporting mm distinct simple paths with strictly increasing labels to the administration division, a visitor may win mm free tickets for future visits.

Your friend George just visited the park, and learned all labels on the trails. He wants to win free tickets for future visits with you. Please write a program to compute the number of distinct simple paths with strictly increasing labels in the garden park.

输入格式

The first line contains one integer nn. The (i+1)(i + 1)-th line contains three integers ai,bi,cia_i, b_i, c_i. Trail ii connects place aia_i and bib_i, and the label (i)\ell(i) on trail ii is cic_i.

输出格式

Output the number of distinct simple paths with strictly increasing labels in the garden park.

3
1 2 3
2 3 7
5
5
1 2 2
2 3 2
1 4 5
5 4 5
9

提示

  • 1n2×1051 \leq n \leq 2 \times 10^5
  • 1ain1 \leq a_i \leq n for i{1,2,,n}i \in \{1, 2, \dots, n\}.
  • 1bin1 \leq b_i \leq n for i{1,2,,n}i \in \{1, 2, \dots, n\}.
  • 0ci1090 \leq c_i \leq 10^9 for i{1,2,,n}i \in \{1, 2, \dots, n\}.
  • aibia_i \neq b_i for i{1,2,,n}i \in \{1, 2, \dots, n\}.