SP14943 DRTREE - Dynamically-Rooted Tree

Description

You are given a rooted tree with N nodes, numbered from 1 to N. Initially node 1 is the root. Each node i has a weight W\[i\]. You have to perform two types of operations: **"S a"** - Find the sum of weights of node **a**'s sub-tree nodes (including node **a**). **"R a"** - Change root of the tree to **a**.

Input Format

**Line 1:** N (1

Output Format

For each operation of type 'S', output the operations result in a separate line.