[Figure] On Playground

Description

According to press reports, Orz leader in the Central Pacific built a big amusement park, which has many small islands, there is only one rides on each island, there are some undersea tunnel connection between the island and the island, and some do not, a person rides on a open only once, happy to spend money proportional to the value obtained. At first, you may choose to be dropped into any of the island. When you want to leave the amusement park, you can call up the aircraft to pick, but can not be dropped again.
fhn very rich, he wanted to get the maximum value at an amusement park happy.
The czm is relatively poor, but his desire to play most of the rides.

Input

The first line: n (n representatives islands) (n <= 200)
the n-th row, respectively for the use of 1- island island rides on the money spent n
e (e representatives harbor tunnels)
Subsequently the e-line, each line number 2, expressed undersea tunnel connection between two islands

Output

The first line number, czm up to visit amusement park.
The second line, resulting in a happy way to get around fhn value.

Sample Input

5
3
4
5
8
10
5
1 2
1 3
2 5
3 4
4 5

Sample Output

5
30


Problem-solving ideas

Have to start playing after wa n times, finally thought of connected components is undirected graph

Find the largest connected component, two variables, a number of facilities record, a record value of happiness


#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct DT{
	int to,next;
}e[50010];
int head[300],n,num,Gun,v[300],t,s[300],m,sum,Max;
queue<int> f;//刚学queue的蒟蒻
void BFS(int x){
	f.push(x);
	v[x]=1,t=1,sum=s[x];
	while(!f.empty()){
		for(int i=head[f.front()];i;i=e[i].next)
		    if(!v[e[i].to]){
		    	sum+=s[e[i].to];//快乐值
		    	v[e[i].to]=1;
		    	++t;//设施数
		    	f.push(e[i].to);
		    }
		f.pop();
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	    scanf("%d",&s[i]);
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int x,y;
    	scanf("%d%d",&x,&y);
		e[++num].to=y,e[num].next=head[x],head[x]=num;
		e[++num].to=x,e[num].next=head[y],head[y]=num;
	}
    for(int i=1;i<=n;i++){//不一定从1走是最优
		if(!v[i]){//用v记录遍历过没有
			BFS(i);
			Gun=max(Gun,t);//设施数
			Max=max(Max,sum);//快乐值
		}
	}
	printf("%d\n%d",Gun,Max);
}
Published 45 original articles · won praise 0 · Views 361

Guess you like

Origin blog.csdn.net/qq_39940018/article/details/103600820