# D. Cycle Free Flow SOLUTIONS

## Cycle Free Flow SOLUTION Codeforces

You are given an undirected associated weighted chart of n hubs which contains no cycles. For q various sets of hubs, we might want you to locate the most extreme stream from the principal hub in the pair to the second hub in the pair. See the definition for max stream beneath in the event that you don’t have the foggiest idea what it implies.

Likewise, if you don’t mind answer these questions on the web. Initially, I was going to drive you to do this by scrambling stuff, however that would make this issue harder to peruse and additionally irritating. Along these lines, honestly, kindly answer the main question before perusing the subsequent inquiry, and whatnot. All things considered, this is simply practice for you to show signs of improvement, isn’t that so?

The most extreme stream in a diagram between two hubs an and b can be characterized as follows: You may pick any way from a to b with the end goal that each edge on that way has a positive weight. At that point you may deduct 1 from the heaviness of each edge on that way, and add 1 to your answer. Continue doing this the same number of times as you might want. The “most extreme stream” between the two hubs is the best that your answer might be, in the event that you pick your ways ideally at each progression.

Information

The principal line will contain a two whole numbers: n and m, the quantity of hubs and the quantity of edges in the diagram. The following m lines will each contain three whole numbers: the endpoints u and v of an undirected edge, and the underlying load of that edge w.

The following line will contain a solitary number q: the quantity of inquiries you should reply.

Every one of the following q lines will contain two numbers: an and b, the two hubs you should discover the maximum stream between.

2≤n,m,≤3∗105

1≤a,b,≤n a≠b

1≤w≤109

u≠v

Extra requirement on input: the given chart is associated and has no cycles, self-circles, or multi-edges.

Yield

For each inquiry, kindly print a solitary whole number: the maximum stream between hubs an and hubs b for that question.

Models

inputCopy

2 1

1 2 2768

1 2

2 1

outputCopy

2768

2768

inputCopy

3 2

3 2 4814

2 1 1832

2 1

1 2

3 1

outputCopy

1832

1832

1832

inputCopy

5 4

4 2 10348

1 4 2690

5 4 9807

3 4 8008

5 4

1 5

5 4

5 4

1 5

outputCopy

9807

2690

9807

9807

2690

inputCopy

5 4

1 3 2653

4 1 322

5 1 8657

2 4 4896

4 2

2 5

2 5

1 3

4 5

outputCopy

4896

322

322

2653

322