当前位置: 首页 > 工具软件 > go-summer > 使用案例 >

Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

齐思淼
2023-12-01

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;
}


 类似资料: