#P2634. [国家集训队] 聪聪可可

    ID: 1651 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp点分治WC/CTSC/集训队

[国家集训队] 聪聪可可

Description

Congcong and Keke are brothers who often fight over trivial things, such as when only one ice pop is left at home and they both want it, or they both want to use the computer (but there is only one). Normally they would resolve it with rock-paper-scissors, but they are bored of that simple game.

Their father, annoyed by their arguments, invented a new game: he draws nn “points” on paper and uses n1n - 1 “edges” to connect them so that the nn “points” are connected (this is a tree). Each “edge” has a number on it. Then Congcong and Keke each randomly choose one point (they cannot see the tree when choosing). If the sum of the numbers on all edges along the path between the two chosen points is a multiple of 33, Congcong wins; otherwise, Keke wins.

Congcong likes thinking about problems. After each game, he carefully studies the tree and wants to know his winning probability for this graph. Please help compute this value to verify Congcong’s answer.

Input Format

The first line contains a positive integer nn. The next n1n - 1 lines each contain 33 integers x,y,wx, y, w, indicating there is an edge between node xx and node yy with number ww.

Output Format

Output the probability as an irreducible fraction in the form a/b, where aa and bb are coprime. If the probability is 11, output 1/1.

5
1 2 1
1 3 2
1 4 1
2 5 3
13/25

Hint

Sample Explanation:

There are 1313 ordered pairs: (1,1)(1, 1), (2,2)(2, 2), (2,3)(2, 3), (2,5)(2, 5), (3,2)(3, 2), (3,3)(3, 3), (3,4)(3, 4), (3,5)(3, 5), (4,3)(4, 3), (4,4)(4, 4), (5,2)(5, 2), (5,3)(5, 3), (5,5)(5, 5).

Constraints:

For 100%100\% of the testdata, n2×104n \leq 2 \times 10^4.

Translated by ChatGPT 5