Path queries

3.5

10 votes
Basic Programming, C++
Problem

You are given an undirected tree G with N nodes. Every node is assigned a value denoted by A[i].

A simple path u1u2u3u4,..,uk is said to be beautiful if the number of pairs of nodes (ui,ui+1) where A[ui] is odd and A[ui+1] is even and number of pairs of nodes (ui,ui+1) where A[ui] is even and A[ui+1] is odd are equal.

Given Q queries of the form:

  • u val: Set the value of node u equal to val, i.e. set A[u]=val and find the number of ordered pairs (u,v) such that the simple path between node u and node v is beautiful.

Note: 

  • Assume 1 based indexing.
  • Queries are interdependent, changes made in previous queries are retained.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • For each test case:
    • The first line contains two space-separated integers N Q denoting the number of nodes in the tree and the number of queries respectively.
    • Next line contains N space-separated integers denoting the array A.
    • Next N1 lines contain two space-separated integers a b, denoting an edge between node a and node b
    • Next Q lines contains the queries.

Output format

For each test case in a new line, print the answer for queries in a space-separated format.

Constraints

1T101N,Q2×1051A[i],val1091uN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first query:

  • Set A[3]=2
  • The simple path between given pair of nodes (u,v) is beautiful.
    • (1, 1), (2, 2), (3, 3), (6, 6), (1, 2), (1, 3), (1, 6), (2, 3), (2, 6), (3, 6), (4, 4), (5, 5), (4, 5)
  • Total 13 such pairs exist.

 

Editor Image

?