luogu#P5558. 心上秋

    ID: 4080 远端评测题 1000~1500ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP倍增O2优化矩阵加速树链剖分

心上秋

Background

Windingly passing through others’ lips and teeth.

How many days and nights have I heard your story.

Could it be that this one word, “love,”

Cannot be understood even if one throws away life and death.

I heard that city in the northern land,

Is covered by winter snow all day long.

By the time old friends part forever and walk farther and farther away,

In the blink of an eye, it is already two lifetimes apart.

Who goes from life to death, who worries about gain and loss,

There is not much time left even to look at each other.

The person in the painting secretly—his heart sways and his spirit is lost.

A drop of tears blurs the whole page.

Waving ink to draw eyebrows and eyes, lifting the brush to trace the hair at the temples.

With just a few strokes, it turns out to be like this.

At midnight where no one is, unrolling the scroll to the moon,

Suddenly I understand this longing.

Whose signature wrote the name.

———— “Heartfelt Autumn”

Background

In the first month of the first year of Jingning (33 BC), the night before Zhaojun left for the frontier.

The painter stumbled and crashed as he came to the palace where Zhaojun lived.

I heard that city in the northern land
Is covered by winter snow all day long
By the time old friends part forever and walk farther and farther away
In the blink of an eye, it is already two lifetimes apart
—— “Heartfelt Autumn”

If they could never meet again, the painter thought, he wanted to leave something for Zhaojun.

He wanted to give his paintbrush to Zhaojun.

There are NN rooms in Zhaojun’s palace, connected by N1N-1 roads.

The painter is now in the entrance hall room SS. He vaguely remembers that Zhaojun’s room is number TT.

Outside the window, the wind and rain rage. Inside the palace it flickers between light and dark, and there is not a single shadow of a person.

The painter walks into the dim corridor. On the wall of each corridor, there are several maple leaves painted—these were painted earlier at Zhaojun’s request. Zhaojun said she especially liked autumn, and especially the maple leaves of autumn.

How many past events did we talk about side by side—before we knew it, dusk had arrived.
—— “Heartfelt Autumn”

The corridor is extremely dark. The painter wants to light the candles prepared inside. He remembers Zhaojun had a habit: in each corridor, the number of candles equals the number of maple leaves on the wall. If Zhaojun wants to light the candles in a corridor, she will light them all. At that moment, Zhaojun considers that corridor to be lit up, and she will not light up any corridor whose number of maple leaves is less than that corridor’s.

This should be the last time coming to this place, the painter thought. He wants to follow Zhaojun’s habit and walk to Zhaojun’s room. At the same time, the painter does not want to go back the way he came, so he will not take any corridor he has already walked through.

Now the painter wants to know: when he walks from the palace hall SS to Zhaojun’s room TT, without going back, what is the maximum number of corridors he can light up?

Problem Description

In the first month of the first year of Jingning (33 BC), the night before Zhaojun left for the frontier.

The painter stumbled and crashed as he came to the palace where Zhaojun lived.

I heard that city in the northern land
Is covered by winter snow all day long
By the time old friends part forever and walk farther and farther away
In the blink of an eye, it is already two lifetimes apart
—— “Heartfelt Autumn”

If they could never meet again, the painter thought, he wanted to leave something for Zhaojun.

He wanted to give his paintbrush to Zhaojun.

There are NN rooms in Zhaojun’s palace, connected by N1N-1 roads.

The painter is now in the entrance hall room SS. He vaguely remembers that Zhaojun’s room is number TT.

Outside the window, the wind and rain rage. Inside the palace it flickers between light and dark, and there is not a single shadow of a person.

The painter walks into the dim corridor. On the wall of each corridor, there are several maple leaves painted—these were painted earlier at Zhaojun’s request. Zhaojun said she especially liked autumn, and especially the maple leaves of autumn.

How many past events did we talk about side by side—before we knew it, dusk had arrived.
—— “Heartfelt Autumn”

The corridor is extremely dark. The painter wants to light the candles prepared inside. He remembers Zhaojun had a habit: in each corridor, the number of candles equals the number of maple leaves on the wall. If Zhaojun wants to light the candles in a corridor, she will light them all. At that moment, Zhaojun considers that corridor to be lit up, and she will not light up any corridor whose number of maple leaves is less than that corridor’s.

This should be the last time coming to this place, the painter thought. He wants to follow Zhaojun’s habit and walk to Zhaojun’s room. At the same time, the painter does not want to go back the way he came, so he will not take any corridor he has already walked through.

Now the painter wants to know: when he walks from the palace hall SS to Zhaojun’s room TT, without going back, what is the maximum number of corridors he can light up?

Input Format

The first line contains an integer NN, meaning the number of rooms in the palace.

Next there are N1N-1 lines. Each line contains three integers ui,vi,leafiu_{i}, v_{i}, leaf_{i}, meaning there is a corridor between uu and vv, and there are leafileaf_{i} maple leaves painted in that corridor.

Next is an integer MM.

The last MM lines each contain two integers Si,TiS_{i}, T_{i}.

Output Format

Output MM lines. For each query Si,TiS_{i}, T_{i}, output the maximum number of corridors that can be lit.

5
1 2 5
2 3 1
1 4 1
3 5 4
3
2 1
4 2
1 1

1
2
0

7
1 2 1
1 3 5
2 4 1
4 5 4
5 6 1
1 7 1
5
7 5
7 6
2 7
1 1
2 4
4
4
2
0
1

20
1 2 1
1 3 3
2 4 5
1 5 1
5 6 5
1 7 5
1 8 4
7 9 1
8 10 2
1 11 1
2 12 5
3 13 1
3 14 3
3 15 3
10 16 1
5 17 1
12 18 5
7 19 4
7 20 5
10
10 3
17 16
11 9
4 6
16 17
11 16
11 11
13 11
2 1
10 11
2
3
2
3
3
2
0
2
1
2

Hint

Constraints

Test ID N M Special Properties
11 100100 100100 None
22
33 10001000
44 1000010000
55 11
66 1,21,2
77
88 3000030000 100000100000 None
99
1010 300000300000

Special property 11: 1<=leafi<=21<=leaf_{i}<=2.

Special property 22: ui+1=viu_{i}+1=v_{i}.

For all testdata, it is guaranteed that 1<=leafi<=51<=leaf_{i}<=5.

Analysis of Sample 1:

Query 11: From 22 to 11, at most 11 corridor can be lit (212-1).

Query 22: From 44 to 22, at most 22 corridors can be lit (41,124-1, 1-2).

Query 33: Obviously, no corridor can be lit.

Analysis of Sample 2:

)

Query 11: From 77 to 55, 44 corridors can be lit (71,12,24,457-1, 1-2, 2-4, 4-5).

Query 22: From 77 to 66, 44 corridors can be lit (71,12,24,457-1, 1-2, 2-4, 4-5). Not lighting (565-6) is because after lighting (454-5), it is impossible to light a corridor whose number of maple leaves is less than 44. It is easy to see this is optimal. Alternatively, do not light (454-5) and instead light (565-6); this is also optimal.

Query 33: From 22 to 77, 22 corridors can be lit (212-1, 171-7).

Query 44: Do not pass through any corridor.

Query 55: Pass through 11 corridor (242-4).

Where can sorrow be made whole. Autumn rests on the hearts of those who part. Even banana leaves, without rain, still rustle. All say the late-cool weather is good; there is a bright moon, yet one fears climbing the tower.

Let age and affairs end within dreams. Flowers fall empty, smoke and waters flow. The swallows have left to return, while the traveler still lingers. Drooping willows cannot hold back the skirt belt. Long is it, the rope that ties the moving boat.

Translated by ChatGPT 5