IAPCR2F - Summer Trip

In this hot summer AIUB students decided to go on a trip to Cox’s Bazaar sea beach. Every student has some amount of money to spend in this trip. Each student belongs to some group. If student A knows student B, B knows C then A, B and C belong to the same group. In order to utilize the budget properly students hired a travel agency to get an optimal travel plan. They submitted the information about all the student’s money and relations among them. Now the agency wants to figure out the number of groups and the total budget of each group. Total budget of a group is the summation of all the student’s money in that group.

Input

The first line contains an integer T (1<=T<=50), denoting the number of test cases.

Each case starts with two integers N M. N (1<=N<=1000), denotes the number of students and M (0<=M<=(N*(N-1)/2)), denotes the number of relationships. In the next line N integers are given. The ith (1<=i<=N) integer ai (1<=ai<=100) denotes the money ith student has. Next M lines each contains two integers u v, (1<=u,v<=N and u != v) denoting that u knows v and vice versa.

Output

For each test case output “Case X: K” where X is the case number starting from 1 and K is the number of groups. In the next line output K integers, the total budget of all the groups in ascending order of budget.

Example

Input:
1
5 3
5 7 4 6 8
1 2
1 3
4 5

Output:
Case 1: 2
14 16

Added by:imranziad
Date:2016-06-03
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Intra AIUB Programming Contest R2

hide comments
2020-09-17 20:05:17
Basic DFS problem;)
2016-08-19 10:08:39
Think simple graphs!
2016-07-12 22:49:45 geeta
Not clearing the map costed 1WA :(
100 AC :)
2016-06-08 14:39:38 Siddharth Singh
My First DSU Problem ;)

Last edit: 2016-06-08 14:39:49
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.