Tree of Numbers

0

0 votes
Tree, Dynamic Programming, Algorithms, Medium-Hard
Problem

You are given a tree of n nodes, some of the nodes contain numbers and others don't. Your task is to compute the number of ways to complete the labeling of the tree rooted at 1, such that the following condition is satisfied "for any subtree x all elements of the subtree must divide its root".

Input:

First line contains a single integer n, denoting the number of nodes in the tree.
Next Line contains n integers, each number describes the content of a node in order (e.g: first number describes content of node 1, second describes content of node 2, ... etc). If a number is 0, it means that the content is unknown.
Next n1 lines contain each two numbers (u,v), meaning that there is an edge between node u and node v.

Output:

Ouput a single integer denoting the number of ways to fill the tree without breaking the constraint modulo 109+7;

Constraints:

  • 1n105.
  • All numbers are non negative and less than or equal 10^9.
  • The root's content is always known.
Sample Input
3
2 0 0
1 2
2 3
Sample Output
3
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The possible labelling are:

  1. 2 2 2
  2. 2 2 1
  3. 2 1 1
Editor Image

?