E. Alternating Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given a tree with nn nodes numbered from 11 to nn. Each node ii has an associated value ViVi.
If the simple path from u1u1 to umum consists of mm nodes namely u1→u2→u3→…um−1→umu1→u2→u3→…um−1→um, then its alternating function A(u1,um)A(u1,um) is defined as A(u1,um)=m∑i=1(−1)i+1⋅VuiA(u1,um)=∑i=1m(−1)i+1⋅Vui. A path can also have 00edges, i.e. u1