0% found this document useful (0 votes)
30 views2 pages

Magic Forest

The document describes a magic forest problem where the goal is to calculate the number of different magical walks in the forest modulo 10^9 + 7. A magical walk visits meadows by traversing trails, where each trail has a magical value and the walk is magical if the magical values of consecutive trails are consecutive integers. The input specifies the number of meadows and trails in the forest along with the trail connections and values. The output is the number of different magical walks modulo 10^9 + 7.

Uploaded by

Mihail Livadaru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Magic Forest

The document describes a magic forest problem where the goal is to calculate the number of different magical walks in the forest modulo 10^9 + 7. A magical walk visits meadows by traversing trails, where each trail has a magical value and the walk is magical if the magical values of consecutive trails are consecutive integers. The input specifies the number of meadows and trails in the forest along with the trail connections and values. The output is the number of different magical walks modulo 10^9 + 7.

Uploaded by

Mihail Livadaru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Magic Forest

Input file: standard input


Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes

The forest contains N meadows, numbered from 1 to N . There are M trails, numbered from 1 to M .
Trail i connects meadows ai and bi , and has a magical value ci . A walk starts at a meadow and visits a
number of meadows by traversing trails, arriving at a meadow. The walk might visit the same meadow
multiple times. The length of the walk is the number of traversed trails.
A walk of length k is a magical walk, if the magical values of the traversed trails in order are m1 , m2 , . . . , mk ,
and:

• k ≥ 1,
• mi + 1 = mi+1 for all 1 ≤ i ≤ k − 1.

Two magical walks are different if the sequences of traversed trails are different.
Write a program that calculates the number of different magical walks modulo 109 + 7!

Input
 
N (N −1)
The first line contains the integers N (2 ≤ N ≤ 5 ∗ 105 ) and M (1 ≤ M ≤ min 2 , 106 ).

The following M lines contains three integers ai , bi and ci (1 ≤ ai 6= bi ≤ N for each trail; 1 ≤ ci ≤ 109
for each trail) representing a trail between trees ai and bi with a magical value of ci .
Any pair of meadows is connected by at most one trail.
For tests worth 7 points: ai = i and bi = i + 1 for all trails and M = N − 1.
For tests worth 9 points: ci ≤ 3 for all trails.
For tests worth 14 points: N ≤ 22 and M ≤ 22.
For tests worth 20 points: N ≤ 1000 and M ≤ 5000.
For tests worth 50 points: No additional limitations.

Output
You need to write a single line with an integer: the number of magical walks modulo 109 + 7.

Examples
standard input standard output
4 4 10
1 2 1
2 3 2
3 4 3
1 3 2
4 3 5
1 3 3
3 4 2
3 2 1
3 3 6
1 2 1
2 3 2
3 1 3

Page 1 of 2
Note
In the first sample case the 10 magical walks are:

• length of 1: 1 − 2, 2 − 3, 3 − 1, 3 − 4,

• length of 2: 1 − 2 − 3, 2 − 1 − 3, 2 − 3 − 4, 1 − 3 − 4,

• length of 3: 1 − 2 − 3 − 4, 2 − 1 − 3 − 4.

2
1
2
1
2 3
3
4

In the second sample case there are 3 magical walks of length 1, and 2 of length 2.

1
3
3 2 4
1
2

In the third sample case there are 3 magical walks of length 1, and 2 of length 2 and 1 of length 3.

2
1 2
1 3
3

Page 2 of 2

You might also like