Energetic Node(Easy Version) solution codechef- There’s a tree having NN nodes, out of which, some nodes are energetic. Initially, energy of all the nodes is 00. In the i^{th}ith second, every energetic node will make the energy of itself and all its neighbours increase by ii.-

Energetic Node(Easy Version) solution codechef – There’s a tree having N nodes, out of which, some nodes are energetic.

Initially, energy of all the nodes is 0. In the i^{th} second, every energetic node will make the energy of itself and all its neighbours increase by i.

This LeetCode contest is sponsored by Motorola.

The top 500 contestants would be shortlisted as Employee Referrals if they apply for the job opportunities with Motorola Mobility, a Lenovo Company.

Register for the contest in advance! You can fill out the contact information at the registration step.

⭐ Bonus Prizes⭐

  • Contestants ranked 1st will win a Motorola Ace 5G
  • Contestants ranked 2nd will win a Lenovo Smart Clock with wireless charger
  • Contestants ranked 3rd will win a Motorola Bluetooth Speaker Tumbler
  • Contestants ranked 4th ~ 10th will win Amazon $40 eGift card
  • Contestants ranked 11th ~ 20th will win Starbucks $25 eGift card
  • Contestants ranked 66th, 166th, 266th, 666th, 1666th will win a LeetCode BigO NoteBook

Energetic Node(Easy Version) solution codechef

You need to answer Q queries. For the i^{th} query, you are given three integers:

  • X_i T_i K_i : Find the minimum number of seconds after which, there are at least T_i nodes with energy not less than K_i on the path from node 1 to node X_i. If it is not possible to have at least T_i nodes with energy not less than K_i on the path from node 1 to node X_i after any amount of time, print -1 instead.

Input Format

  • The first line contains an integer N — the number of nodes.
  • The second line contains N space-separated integers, the array A denoting whether the nodes are energetic. If A_i = 1, it means that the i^{th} node is energetic, otherwise the i^{th} node is not energetic.
  • The next N-1 lines describe the edges. The i^{th} of these N-1 lines contains two space-separated integers U_i and V_i, denoting an edge between U_i and V_i.
  • The next line contains an integer Q — the number of queries.
  • The next Q lines contain three space-separated integers X_i,T_i,K_i, the query.

Energetic Node(Easy Version) solution codechef

For each query, output on a new line: the minimum number of seconds after which, there are at least T_i nodes with energy not less than K_i on the path from node 1 to node X_i.
If it is not possible to have at least T_i nodes with energy not less than K_i on the path from node 1 to node X_i after any amount of time, print -1 instead.

Energetic Node(Easy Version) solution codechef

  • 1 \leq N,Q \leq 4\cdot 10^4
  • 0 \leq A_i\leq 1
  • 1 \leq U_i, V_i,X_i,T_i \leq N
  • 1 \leq K_i \leq 10^4

Sample 1:

Input

Output

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

Energetic Node(Easy Version) solution codechef

Query 1: No matter how long, there will not be at least 3 nodes having energy not less than 1 on the path from node 1 to node 7. Thus, the answer is -1.

Query 2: In the 1^{st} second, the energy of nodes 1,3, and 6 becomes 2,3, and 2 respectively. Thus, there are at least 3 nodes with energy not less than 2.

Query 3: In the 2^{nd} second, the energy of nodes 1,4, and 7 becomes 6,3, and 0 respectively. Thus, there are at least 2 nodes with energy not less than 3.

Query 4: In the 3^{rd} second, the energy of nodes 1,3, and 5 is 12,18, and 6 respectively. Thus, there are at least 3 nodes with energy not less than 5.

Solution – CLICK HERE

Leave a Comment