Travel Problem[SZU_K28]

Description
After SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingChengShan for fun. QCS has many spot as the picture following, each spot has its unique value for SzuHope, they will get the value just once if and only if they had been there. From one spot will exists some roads to other spots, it means that one road link two spots without direction. SzuHope can choose any spot to travel at beginning, but then they can go to next spot only by roads. Can you help them make the travel’s total value biggest?


Input
There are less than 100 test cases. For each case, the first line has two numbers M,N.describe the number of spots and roads(1<=N<=1000, 0<=M<=N*N) , the spots are numbered with 1,2,3…N; the second line has N numbers describe the value xi of each spot(0<xi<1000); the next M line, each line has two numbers U,V(1<= U,V<=N) means U,V is connected. 0 0 for end.


Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the result.


Sample Input
0 1
5
1 3
1 3 2
1 3
0 0

Sample Output
Case 1:
5
Case 2:
3

 

#include<stdio.h>
#include<string.h>
int s[1025],father[1025],w[1025];
int getfather(int v)
{
	if (father[v]==v) return father[v];
	father[v]=getfather(father[v]);
	return father[v];
}
void merge(int u,int v)
{
	u=getfather(u);
	v=getfather(v);
	father[u]=v;
}
int main()
{
	int N,M,i,cas=0;
	while (scanf("%d%d",&M,&N)!=EOF && M+N)
	{
		cas++;
		printf("Case %d:\n",cas);
		for (i=1;i<=N;i++) scanf("%d",&w[i]);
		for (i=1;i<=N;i++) father[i]=i;
		for (i=1;i<=M;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			merge(u,v);
		}
		for (i=1;i<=N;i++) father[i]=getfather(i);
		memset(s,0,sizeof(s));
		for (i=1;i<=N;i++) s[father[i]]+=w[i];
		int Max=0;
		for (i=1;i<=N;i++)
			if (s[i]>Max) Max=s[i];
		printf("%d\n",Max);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/dramstadt/p/3225704.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值