Xor tree
Practice
4.5 (8 votes)
Graphs
Algorithms
Depth first search
C++
Problem
28% Success 1256 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given an undirected tree with N nodes. Every node is initially assigned a value equal to 0.
Perform Q queries that are given on the tree as follows:
- u v w: For all the nodes present on a simple path between nodes u and v take the xor of node value with w.
Task
Find the sum of values assigned to all the nodes after performing Q queries.
Input
- First line contains an integer T, denoting the number of test cases.
- For each test case:
- The first line contains two space separated integer N, Q.
- The next N - 1 lines contain two space-separated integers u, v denoting an edge between node u and node v.
- The next Q lines contains three space-separated integers denoting the query.
Output
For each test case in a new line, print the sum of values assigned to each node after performing Q queries.
Constraints
\(1 ≤ T ≤ 5\\ 1 ≤ N, Q ≤ 10^5\\ 0 ≤ w ≤ 10^9\)
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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
DatastructuresGraphsAlgorithmsDepth First Search
Points:30
6 votes
Tags:
Data StructuresDepth First SearchAlgorithmsDisjoint Set UnionGraphs
Points:30
8 votes
Tags:
AlgorithmsApprovedCombinatoricsDepth First SearchDynamic ProgrammingGraphsMathMediumReadyRecruit
Editorial
Login to unlock the editorial