Mother Flippin' Tree

5

2 votes
Advanced Data Structures, Depth First Search, Lazy Propagation, Trees
Problem

You are given a tree consisting of n nodes, rooted at node 1. For each node i from 1...n,  value val[i] is given. (val[i] is 0 or 1)

You need to answer q queries of 2 types:

  1. flip x – flip all the values in subtree of node x.

  2. val x – print current value of node x. (val[x])

(Note: subtree of any node, also includes the node itself)

(flip value of x -> perform val[x] = !val[x])

 

INPUT:

  • The first line contains an integer n. (1  105) - number of nodes.
  • Next n – 1 lines contain 2 space separated integers u and v, depicting edge b/w node u and node v. (1  u, v  n)
  • Next line contains space seperated integers - val[1], val[2], ..., val[n]. (0  val[i]  1)
  • Next line contains an integer q. (1  q 105) - number of queries.
  • Next q lines contain a string and an integer separated by space, describing a query.

 

OUTPUT:

  • For every query of type "val" print correspoding result, followed by single space.
Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?