DMPG '18 S4 - Mimi and Prize

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem types

A tree is a connected graph with N nodes and N1 edges. An interesting property of trees is that there exists exactly 1 path between any two nodes.

As the top CS student in her year, Mimi's ICS teacher awards her a tree at graduation. The tree's nodes are labelled 1,2,,N, and the ith node has a value, Ai. However, having scored only half a percentage point lower than her, you decide to contest this prize!

The teacher arranges a code-off on this tree: you are to determine the number of ordered pairs u,v such that the values on the path match the parity of the index. Specifically, if you took the path starting from u and ending at v and wrote it into an array with Au as the first element and Av as the last, then the jth value of this array must be congruent to jmod2, for every j from 1 to the size of the array. Note that this array is 1-indexed.

Can you solve this problem and claim the title of top ICS student?

Constraints

For all subtasks:

1Ai109

Subtask 1 [10%]

1N500

Subtask 2 [10%]

1N2000

Subtask 3 [80%]

1N200000

Input Specification

The first line of input will contain N, the number of nodes in the tree.
The next line of input will contain N space separated integers, A1,A2,,AN.
The next N1 lines of input will each contain a pair of integers, ai bi, indicating that there is an edge between ai and bi.

Output Specification

A single integer, the number of ordered pairs which satisfy the given condition.

Sample Input

Copy
4
1 2 3 4
1 2
2 3
3 4

Sample Output

Copy
8

Comments


  • -1
    AndyZhang68  commented on July 21, 2024, 2:24 p.m.

    Memory may be a bit tight for some Python solutions, so you may have to optimize memory storage for traversals and/or storing the graph