Summer Trip
来源:互联网 发布:淘宝里怎么找我的店铺 编辑:程序博客网 时间:2024/05/16 09:37
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
题意:给你n个人编号从1~n,每个人有一定的价钱,然后有m个关系,有关系的人可以分到一组,问你能分几组,并且把每组的价钱输出来
思路:并查集,只要每个人的父节点一样就可以分到一组
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;#define N 10010int father[N];int vis[10100];int a[100010];int s[10010];int we[10110],ni[10100];int ha[100010];int getfather(int x)//找父节点{ while(x!=father[x]) { x=father[x]; } return x;}void un(int x,int y)//更新父节点{ int x1=getfather(x); int y1=getfather(y); if(x1!=y1) father[x1]=y1;}bool same(int x,int y)//判断父节点是否相同{ return getfather(x)==getfather(y);}int main(){ int g=0; int t; scanf("%d",&t); int n,m; while(t--) { memset(ha,0,sizeof(ha)); memset(s,0,sizeof(s)); memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); ++g; memset(father,0,sizeof(father)); for(int i=1; i<=n; i++) { father[i]=i; } for(int i=1; i<=n; i++) { scanf("%d",&a[i]); } for(int i=1; i<=m; i++) { scanf("%d%d",&we[i],&ni[i]); un(we[i],ni[i]); } for(int i=1;i<=n;i++) { ha[getfather(i)]+=a[i]; } int p=0; for(int i=1;i<=n;i++) { if(ha[i]!=0) { s[p++]=ha[i]; } } printf("Case %d: %d\n",g,p); sort(s,s+p); for(int i=0; i<p; i++) { if(s[i]&&i<n) printf("%d ",s[i]); if(s[i]&&i==n) printf("%d\n",s[i]); } }}
- Summer Trip
- SPOJ-IAPCR2F Summer Trip(dfs/并查集)
- Trip report: Summer ISO C++ standards meeting (Toronto)
- Trip
- Trip
- summer trainning...
- Summer Vocation
- Indian Summer
- Summer Intern
- segmentProject-summer
- summer.px
- HK Trip
- Road trip
- TRIP BOOK
- The trip
- The Trip
- Walmart trip
- Great Trip
- 翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2
- 身在事中,念在事外,
- Poj2155 Matrix 树状数组
- 【CodeForces 707A】Brain's Photos(水)
- 洗牌!打乱一组有序数
- Summer Trip
- 冒泡法则
- C二叉树基础
- 软件测试实验——安装并使用junit、hamcrest和eclemma进行简单测试
- android自定义view --视差动画
- 对hashCode 和 equals的理解
- 花20分钟写的-大白话讲解如何给github上项目贡献代码
- hdu 1056
- 计算机硬件