Smart City

4.6

5 votes
Advanced Data Structures, Data Structures, Medium, Segment Trees
Problem

You live in a city that contains N houses. Each house is reachable from every other house through a unique path, which implies that there are N1 roads in the city. Each of the N1 road is distinct and has a length denoted by an array L. You want to reach directly from house i to house j (1i,jN and ij) by traversing not more than X units of road length. To reach directly from a house i to house j, you can select exactly one road of length less than or equal to X and destroy that road completely and construct a road between the two houses i and j but the condition is that the every house should be reachable from every other house.

Your task is to solve Q queries (each query is independent from each other ) of the following form:

A B X

where (A,B) denotes the pair of the houses that you want to connect. For each query, determine the number of ways in which you can select an ordered pair of houses (G,H) such that the following condidtions hold:

  • There should be a road between the houses G and H

  • The length of the road between the houses G and H is less than or equal to X

  • After removing the road between G and H and adding a road between A and B , the houses G and H should still be connected through a path in the city

Input format

  • First line: Two space-separated integers N and Q 
  • Next N1 lines: Three space-separated integers UV, and Li denoting a bidirectional path from the house U to V and vice versa of length Li
  • Next Q lines: Three space-separated integers A, B, and X 

Output format

For each query, print the answer on a separate line.

Constraints

2N,Q2105

1A,BN and AB

1Li,X106

Subtasks

  • For 10 points: 2N,Q200
  • For 20 points: 2N,Q2000
  • For 70 points: Original constraints
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first query, there are no possible options hence the answer is 0.

For the second query, only (2,1) is a possible pair and hence the answer is 1.

For the third query, the possible pairs are (2,1) and (3,1). 

Editor Image

?