水<并查集,但是WA好久>

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
就是简单的并查集,但是要注意pre[c]=pre[d],这里,一定是要根节点连过去!!注意了!!!!

代码:

#include <iostream>
#include <queue>
#include <stdio.h>
#include<algorithm>
#include <string.h>
using namespace std;
const long long INF=1e15+7;
int map1[1050][1050];
int pre[1050];
int n;
int cost[1050];
int walked[1050];
int sum1[1050];
int sum2[1050];
void init(){
    memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
        pre[i]=i;
}

int find1(int x){
    int a=x;
    while(a!=pre[a]){
      //  a=x;
        a=pre[a];
       // pre[a]=x;
    }
    return a;
}

void un(int a,int b){
    int c=find1(a);
    int d=find1(b);
    //cout << c << "  " <<d  <<endl;
    if(c!=d){
        pre[c]=pre[d];
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    int i,j,k,t1,t2,t3;
    int l1,l2,l3,f1,f2,f3;
    int T,m;
    cin >> T;
    int t=T;
    while(T--){
    cin >> n >> m;
    init();
    memset(walked,0,sizeof(walked));
    memset(cost,0,sizeof(cost));
    for(i=1;i<=n;i++){
        scanf("%d",&f1);
        cost[i]=f1;
    }
    for(i=1;i<=m;i++){
        scanf("%d %d",&f1,&f2);
        //cout << f1 << " " << f2<<endl;
        un(f1,f2);
    }
    int b;
    f3=0;
    //memset(sum1,0,sizeof(sum1))
    memset(sum1,0,sizeof(sum1));
    for(i=1;i<=n;i++){
        b=find1(i);
        //cout << b << "  ╪сак" << cost[i] << endl;
       sum1[b]+=cost[i];
    }
    memset(sum2,0,sizeof(sum2));
    for(i=1;i<=n;i++){
        if(sum1[i]!=0){
            sum2[f3++]+=sum1[i];
        }
    }
    sort(sum2,sum2+f3);
    printf("Case %d: %d\n",t-T,f3);
    for(i=0;i<f3;i++)
        if(i==0)
        cout << sum2[i];
        else
        cout << " " <<sum2[i];

        cout << endl;
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值