Under construction

Still being built. Things may change

Antifreeze

Time limit2sMemory limit1024 MB

Summary
Given a weighted tree with heater rooms, answer whether two heater rooms can be linked by a chain of heater rooms where every gap is at most T.
Level

Medium7 of 10

Topics
Tree, Union-find, Shortest path
Solved
No attempts yet

Problem

"In air so cold it stole my breath..."

The main building of Gyeonggi Science High School is a tree of NN classrooms connected by N−1N-1 corridors. The corridor between classroom UiU_i and classroom ViV_i has length Wi mW_i\ \text{m}. (1≤i≤N−11 \leq i \leq N-1) Normally every classroom has a heater running, but at this school only some classrooms do. (This is really true!!) AiA_i is 00 if the heater in classroom ii is off, and 11 if it is on. (1≤i≤N1 \leq i \leq N) At least two classrooms have a working heater.

You start in a classroom with a working heater and want to reach another classroom with a working heater. Your temperature starts at TT and drops by 11 every second. Your speed is 1 m/s1\ \text{m/s}, so you can think of your temperature dropping by the distance you walk. If the temperature becomes negative, you freeze to death. When you reach a classroom with a working heater, your temperature returns to TT. Reaching a classroom with a working heater at the exact moment the temperature is 00 is allowed.

You must answer QQ questions. The ii-th question is: can you travel from classroom XiX_i to classroom YiY_i without freezing? Both XiX_i and YiY_i are classrooms with a working heater.

Input

The first line contains the number of classrooms NN and the starting temperature TT, separated by a space.

The second line contains NN integers AiA_i (1≤i≤N1 \leq i \leq N), separated by spaces.

The next N−1N-1 lines describe the corridors. Line ii (1≤i≤N−11 \leq i \leq N-1) contains UiU_i, ViV_i, and WiW_i, separated by spaces.

The next line contains the number of questions QQ.

The next QQ lines describe the questions. Line ii (1≤i≤Q1 \leq i \leq Q) contains XiX_i and YiY_i, separated by a space.

Output

Print QQ lines. On line ii, print 11 if you can reach YiY_i from XiX_i without freezing, and 00 otherwise.

Constraints

  • 2≤N≤100,0002 \leq N \leq 100{,}000
  • 1≤T≤1091 \leq T \leq 10^9
  • Ai∈{0,1}A_i \in \{0, 1\} (1≤i≤N1 \leq i \leq N). At least two classrooms have a working heater.
  • 1≤Ui,Vi≤N1 \leq U_i, V_i \leq N, Ui≠ViU_i \neq V_i (1≤i≤N−11 \leq i \leq N-1)
  • The graph given by the input is a tree.
  • 1≤Wi≤1091 \leq W_i \leq 10^9 (1≤i≤N1 \leq i \leq N)
  • The distance between any two classrooms does not exceed 109 m10^9\ \text{m}.
  • 1≤Q≤100,0001 \leq Q \leq 100{,}000
  • 1≤Xi,Yi≤N1 \leq X_i, Y_i \leq N, Xi≠YiX_i \neq Y_i, AXi=AYi=1A_{X_i} = A_{Y_i} = 1 (1≤i≤Q1 \leq i \leq Q)
  • All numbers in the input are integers.

Examples1

  1. Example 1

    Input
    7 5
    1 0 1 0 1 1 1
    1 2 3
    2 3 2
    2 7 4
    2 4 1
    4 5 1
    4 6 4
    2
    1 6
    3 7
    
    Expected output
    1
    0