-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAPCR2F.c
66 lines (65 loc) · 1.1 KB
/
IAPCR2F.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define MAX 10003
int parent[MAX];
int cost[MAX];
int findset(int i)
{
while(i != parent[i])
{
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void unionset(int a, int b)
{
int pa = findset(a);
int pb = findset(b);
if(pa != pb)
parent[pa] = pb;
}
int main()
{
int t;
cin>>t;
int c = 1;
while(t--)
{
int n;
int m;
int i;
int u;
int v;
int p;
map<int,long long int> mp;
vector<long long int> vt;
map<int,long long int>::iterator it;
vector<long long int>::iterator is;
memset(parent,-1,sizeof(parent));
cin>>n>>m;
for(i=1;i<=n;++i)
{
cin>>cost[i];
parent[i] = i;
}
for(i=0;i<m;++i)
{
cin>>u>>v;
unionset(u,v);
}
for(i=1;i<=n;++i)
{
p = findset(i);
mp[p] += cost[i];
}
cout<<"Case "<<c<<": "<<mp.size()<<endl;
c++;
for(it=mp.begin();it!=mp.end();++it)
vt.push_back(it->second);
sort(vt.begin(),vt.end());
for(is=vt.begin();is!=vt.end();++is)
cout<<*is<<" ";
cout<<endl;
}
}