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:
",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
",Max);
	}
	return 0;
}

 

原文地址:https://www.cnblogs.com/dramstadt/p/3225704.html