F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Little W and Contest

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2323    Accepted Submission(s): 770


Problem Description
There are $n$ members in our ACM club. Little W wants to select three persons from our club to form a new team taking part in provincial ACM contests, as it is known by all of us that any ACM contest requires a normal team to have three members.

Little W has divided our club members into two role groups. The first group contains only readers who dedicate themselves to reading problems during contests, though sometimes they may also prepare drinking and food for the team. For the sake of measurement, we define the power of a reader as $1$. The second part contains only coders who code and test programs all the time, and similarly, we define the power of a coder as $2$.

Little W thinks it will be a tremendous disaster when a team has two readers because in that case, the total power of this team is less than $5$ and thus it has a high risk to fail the contest. To avoid that, Little W thinks a new team must have at least two coders.

Additionally, Little W defines the relationship between club members with transitivity. That is, for every three members $A$, $B$, and $C$, if $A$ is familiar with $B$, and $B$ is familiar with $C$, then $A$ will be familiar with $C$ through $B$ instantly. Based on the definition, it is forbidden for the team to have any two members familiar with each other.

At first, no member of our club is familiar with any other, and then Little W will repeatedly make an introduction between two members who are currently strangers to each other until each member is familiar with all the others. During this process, there will be exactly $(n - 1)$ introductions.

Now, for $i = 1, 2, \ldots, n$, Little W wants you to count the combinations of three club members that can form a new team after the first $(i - 1)$ introductions have been made. However, the numbers of combinations may be quite gigantic, so you just need to report each number in modulo $(10^9 + 7)$.
 

Input
There are several test cases.

The first line contains an integer $T$ ($1 \leq T \leq 10$), denoting the number of test cases. Then follow all the test cases.

For each test case, the first line contains an integer $n$ $(1 \leq n \leq 10^5)$, denoting the number of members in this club.

The second line contains $n$ integers consisting of only $1$ and $2$, where the $i$-th integer represents the power of the $i$-th member.

The next $(n - 1)$ lines describe all introductions in chronological order of occurrence, where each line contains two integers $u$ and $v$ $(1 \leq u, v \leq n, u \neq v)$, representing an introduction between the $u$-th member and the $v$-th member, who are currently strangers to each other.

It is guaranteed that the sum of $n$ is no larger than $10^6$.
 

Output
For each test case, output $n$ lines, where the $i$-th line contains an integer, denoting the number of combinations of three club members, in modulo $(10^9 + 7)$, that can form a new team after the first $(i - 1)$ introductions have been made.
 

Sample Input
1 5 2 2 2 1 1 4 5 1 4 2 1 3 2
 

Sample Output
7 7 3 0 0
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 08:19:30, Gzip enabled