Mutual Training for Wannafly Union #6 E - 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
我比赛的时候用的dfs一直错,后来才知道为什么
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<utility>
typedef long long ll;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;
const double e=2.718281828459 ;
const double esp=1e-9;
using namespace std;
int pre[M];
int a[M];
int findset(int a)	//带路径压缩
{
    int r=a;
	while(pre[r] !=r)
	{
		r = pre[r];
	}
	int i=a,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}

void union_nodes(int a, int b)     
{
	int a1 = findset(a);
	int b1 = findset(b);
	if(a1 != b1)		
	{
		pre[a1] = b1;		
	}
}
int main()
{
    int t,n,m,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        ++cnt;
        vector<int>ans,ss;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            pre[i]=i;//初始化
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            union_nodes(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(pre[i]==i) ss.push_back(i);
        }
        for(int i=0;i<ss.size();i++)
        {
            int sum=0;
            for(int j=1;j<=n;j++)
            {
                if(findset(j)==ss[i])
                    sum+=a[j];
            }
            ans.push_back(sum);
        }
        sort(ans.begin(),ans.end());
        int nn=ans.size();
        printf("Case %d: %d\n",cnt,nn);
        for(int i=0;i<nn;i++)
        printf("%d%c",ans[i],i==nn-1 ?'\n':' ');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值