Strange City

4.5

2 votes
Algorithms, Depth First Search, Graphs, Medium
Problem

Anchal lives in a city which has N​ ice cream parlours. The city has N1 connections such that every ice cream parlour can be reached from every other ice cream parlour. Each parlour has only one type of ice cream with cost Ci for each 1iN​. Anchal has Q​ queries with each query specifying a city R​ and two integers X and Y. In every query she wants to reach the same destination ice cream parlour D.Anchal wants to calculate the number of cities from which she can start her journey and reach the destination city D​ along the shortest path between the source and the destination city such that city R lies in its path and the sum of ice creams for these three cities lie between X​ and Y (Both inclusive).

Note : City D , City R and the city from which Anchal starts are pairwise distinct.


INPUT FORMAT

  • First line consists of three space separated integers N , Q and D as described above 
  • Second line consists of N space separated integers with ith integer denoting Ci as described above
  • Next N1 lines each consist of 2 space separated integers U and V denoting there is a bidirectional path from ice cream parlour U to V and vice versa
  • Each of the following Q lines contain three space separated integers IN1 , IN2 and IN3  describing one query. Lets define last as the answer of the previous query (If its first query then last = 0). The parameters of the query can be calculated by : 
    • R=IN1last
    • X=IN2last
    • Y=IN3last

where  is the symbol for logical XOR.


OUTPUT FORMAT

  • For each query, print the answer to it on a separate line


CONSTRAINTS

  • 1N,Q105
  • 1D,RN and RD
  • 1X,Y,Ci1012
  • XY

SUBTASKS

  • For 10 Points : 1N,Q100
  • For 20 Points : 1N,Q1000
  • For 70 Points : Original Constraints
Sample Input
6 5 1
1 5 2 4 6 4 
2 1
3 2
4 3
5 3
6 4
6 5 9
4 5 12
4 6 13
2 6 14
6 2 11
Sample Output
0
1
0
4
4
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first query : R = 6 , X = 5 and Y = 9. Also , D=1. Clearly there is no node from which we can start and reach node 1 via  node 6. Hence ans is 0

Editor Image

?