#P15943. [JOI Final 2026] JOI 之旅 2 / JOI Tour 2
[JOI Final 2026] JOI 之旅 2 / JOI Tour 2
Problem Description
There are towns in the country of JOI, numbered from to . Also, there are roads in the country of JOI, numbered from to . Road () connects town and town in both directions. It is possible to travel from any town to any other town by using some number of roads.
There is one shop in each town in the country of JOI. In the shop in town (), a souvenir is sold for price .
This year, tours are planned in the country of JOI. The -th tour () starts from town and travels by roads to town without visiting the same town twice. Note that the -th tour visits both towns and . It is guaranteed that . Note that, from the structure of the country of JOI, the sequence of towns visited by a tour is uniquely determined.
You are planning to participate in one of these tours and buy one souvenir in each of exactly two of the towns visited on the tour. Moreover, you want to use up exactly the entire budget prepared for souvenirs, so for each of candidate budgets, you decided to investigate in how many ways this can be done.
Given the roads in the country of JOI, the prices of the souvenirs, the information on the tours, and the candidate budgets , write a program that computes the number of ways to choose a tour and the towns in which to buy souvenirs. More formally, for each (), write a program that computes the number of triples of integers satisfying all of the following conditions.
- .
- .
- The -th tour visits towns and .
- .
Input Format
Read the following data from the standard input.
Output Format
Write lines to the standard output. The -th line () should contain the number of ways to choose a tour and the towns in which to buy souvenirs so that the budget is used up exactly.
8
1 2 3 2 1 2 3 2
2 3
7 8
4 3
1 2
7 3
2 5
6 1
4
1 4
1 6
2 5
3 8
7
1 2 3 4 5 6 16
0
0
4
2
4
1
0
8
8 2 3 6 1 4 1 7
1 2
2 3
3 4
4 5
5 6
6 7
7 8
1
1 8
5
2 4 5 10 15
1
2
3
3
1
Hint
Sample 1
First, the towns visited by each tour are as follows.
- The st tour visits towns .
- The nd tour visits towns .
- The rd tour visits towns .
- The th tour visits towns .
Represent a method of participating in the -th tour and buying souvenirs in towns and by . Then, for each candidate budget, the ways to use up the budget exactly are as follows.
- There are ways to use up budget .
- There are ways to use up budget .
- There are ways to use up budget : , , , .
- There are ways to use up budget : , .
- There are ways to use up budget : , , , .
- There is way to use up budget : .
This input example satisfies the constraints of subtasks .
Sample 2
This input example satisfies the constraints of subtasks .
Constraints
- .
- ().
- ().
- ().
- It is possible to travel from any town to any other town by using some number of roads.
- .
- ().
- ().
- ().
- .
- .
- All input values are integers.
Subtasks
- (3 points) .
- (4 points) (), ().
- (5 points) .
- (6 points) (), ().
- (10 points) .
- (7 points) (), ().
- (12 points) .
- (10 points) (), ().
- (15 points) .
- (11 points) (), ().
- (17 points) No additional constraints.