Semnan's Divison


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Jeff and Sehri decided to divide Semnan among themselves. for simplicity let's assume Semnan is a tree consisting of nn nodes. A tree with nn nodes is a undirected connected graph with with n1n - 1 edges. We also define an array FF of length NN of integers between 1 to 100,000. Amin gives each of Semnan's tree nodes to either Jeff and Sehri, after Amin divides Semnan each of Jeff or Sehri will have a forest, where forest is a disjoint union of trees. if Jeff's forests consists of kk connected components and each of them have c1c_1, c2c_2, ..., ckc_k number of nodes, his happiness is i=1kFCi\sum_{i=1}^{k} F_{C_i}

Sehri's happiness is calculated in the same way.

and after that, the amount of happiness of Amin will be the XOR of happiness of Jeff and Sehri. Amin wonders what is the sum of his happiness over all 2n2^n possible divisons of Semnan? Help Amin with this problem.

input🔗

the first line of input consists of the number nn the number of nodes in Semnan. the next line will be n1n - 1 numbers p2p_2, p3p_3, ..., pnp_n where pip_i is an edge between node number i and pip_i. then in the next line there are N numbers denoting array F where the i-th number is FiF_i.

1n261 \le n \le 26 1pii+11 \le p_i \le i + 1 1Fi100,0001 \le F_i \le 100,000

output🔗

output should consist of a single number the sum of Amin's happiness over all possible 2n2^n divisions of Semnan.

example🔗

sample input 1🔗

3
1 1
10 5 15
Plain text

sample output 1🔗

150
Plain text
  • If Jeff gets no nodes Jeff's happiness would be zero but Sehri would get all 3 nodes then Sehri's forest would consist of one component of size 3 then Sehri's happiness would be equal to F3=15F_3=15 since 15xor0=1515 xor 0 = 15 Amin's happiness would be equal to 15.
  • If Jeff gets one node, Jeff's forest would consist of one connected component of size 1 so Jeff's happiness would be equal to F1=10F_1=10 but if the node Jeff gets is node number 1 Sehri's forest would consist of 2 connected components of size one so Sehri's happiness would be F1+F1=20F_1+F_1=20 and Amin's happiness would be 10xor20=3010 xor 20 = 30 but if Jeff gets is the node 2 or 3 Sehri's forest would consist of one connected component of size 2 so Sehri's happiness would be F2=5F_2=5 and Amin's happiness would be equal to 10xor5=1510 xor 5 = 15.
  • If Jeff gets two or three nodes it is similar to the previous cases so we have 15+30+15+15+30+15+15+15=15015+30+15+15+30+15+15+15=150

sample input 2🔗

5
1 1 2 2
10 2 15 3 1
Plain text

sample output 2🔗

566
Plain text
ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.