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: 214 16
题意:ABC可以互连 互连之后他们一共有的钱就是他们身上加起来的钱,问有几个连在一起并且输出他们身上的钱。
分析:典型的并查集,合并节点之后,再更新总值,最后把互连的值存起来,排个序输出。
代码如下:
#include <bits/stdc++.h> //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define INF 1E4 * 1E9 #define pi acos(-1) #define endl '\n' #define me(x) memset(x,0,sizeof(x)); typedef long long LL; const int maxn=1e3+5; const int maxx=1e6+100; const double EPS=1e-2; const int mod=1000000007; //typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree; /*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/ /*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/ long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);} int fa[maxx]; int a[maxx]; int ans[maxx],ans1[maxx]; int fi(int x) { return fa[x]==x?x:fi(fa[x]); } void unio(int x ,int y) { int p1=fi(x),p2=fi(y); if(p1!=p2) { fa[p1]=p2; } } int check(int x,int y) { int p1=fi(x),p2=fi(y); if(p1==p2) return 1; else return 0; } void init() { for(int i=1;i<=maxx;i++) fa[i]=i; } int main() { int t; cin>>t; for(int cas=1;cas<=t;cas++) { me(ans); me(ans1); init(); int n,m; int cnt=0; cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; int u,v; for(int i=1;i<=m;i++) { cin>>u>>v; unio(u,v); } /* for(int i=1;i<=8;i++) { cout<<fa[i]<<"*******************"<<endl; }*/ for(int i=1;i<=n;i++) ans[fi(i)]+=a[i]; /*for(int i=1;i<=8;i++) cout<<ans[i]<<endl;*/ for(int i=1;i<=n;i++) { if(ans[i]) ans1[cnt++]=ans[i]; } sort(ans1,ans1+cnt); printf("Case %d: %d\n",cas,cnt); for(int i=0;i<cnt;i++) { if(i==0) cout<<ans1[i]; else cout<<" "<<ans1[i]; } cout<<endl; } }