Tree and Queries
Practice
0 (0 votes)
Heavy light decomposition
Tree
Medium Hard
Problem
57% Success 54 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree with root node as 1. Every edge in the tree is undirected. Each node has been assigned a value. You have to handle two kinds of queries.

1 X Y : change value of node X to Y

0 X Y : Check whether the path from node X to node Y is dancing. Path is dancing if all values form alternating sequence while travelling from node X to node Y.

Eg. of alternating values sequence: 4 9 6 7 2 100 18.

Input
First line contains a number N and Q as input. Next N-1 lines contains u and v denoting there is an undirected edge between node u and v. Next line contains N integers denoting value of each node

Output
Output for only query type 0 u v whether the path from node u to v is alternating or not . Output should be YES or NO

Constraints
1≤ N ≤ 5*10^4
1≤ u ≤ vN

1≤ q ≤ 10^5
0≤ value of node ≤ 100

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
12 votes
Tags:
Data StructuresHeavy Light DecompositionAdvanced AlgorithmsAlgorithmsGraphs