Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(並查集+分組揹包)

D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Just to remind, girls in Arpa's land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.

Input

The first line contains integers nm and w (1  ≤  n  ≤  10001 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), meaning that Hoses xiand yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output

Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.

Examples
input
3 1 5
3 2 5
2 4 2
1 2
output
6
input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
output
7
Note

In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.

題目大意:有一個能夠承受w重量的劇場,有n個人,每個人有wi的體重和bi的顏值,現在有m組關係,每組的人要麼全部都參加要不不參加或者只參加一個人,求出這個劇場顏值最高多少

解題思路:對於每組這種情況,需要做的是利用並查集分組,然後對於每組記下相應的下標,之後用分組揹包去做

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

struct people
{
	LL weight,beautiful;
}a[5005];
LL dp[1005];
vector<int> tu[1005];
int check[1005];
int n,m,w;
int find(int x)
{
	if(check[x]==x)
		return x;
	check[x]=find(check[x]);
	return check[x];
}
void join(int x,int y)
{
	int t1=find(x);
	int t2=find(y);
	if(t1!=t2)
		check[t1]=t2;
}
int main()
{
	int i,j,k,x,y;
	cin>>n>>m>>w;
	for(i=1;i<=n;i++)
	{
		check[i]=i;
		cin>>a[i].weight;
	}
	for(i=1;i<=n;i++)
		cin>>a[i].beautiful;
	for(i=1;i<=m;i++)
	{
		cin>>x>>y;
		join(x,y);
	}
	for(i=1;i<=n;i++)
			tu[find(check[i])].push_back(i);
	
	memset(dp,0,sizeof(dp));
	for(i=1;i<=n;i++)
	{
		if(check[i]!=i)
			continue;
		for(j=w;j>=0;j--)
		{
			LL cntw=0,cntb=0;
			for(k=0;k<tu[i].size();k++)
			{
				cntw+=a[tu[i][k]].weight;
				cntb+=a[tu[i][k]].beautiful;
				if(j>=a[tu[i][k]].weight)
				{
					dp[j]=max(dp[j],dp[j-a[tu[i][k]].weight]+a[tu[i][k]].beautiful);
				}
			}
			if(j>=cntw)
				dp[j]=max(dp[j],dp[j-cntw]+cntb);
		}
	}
	LL MAX=0;
	for(i=1;i<=w;i++)
		MAX=max(MAX,dp[i]);
	cout<<MAX<<endl;
	return 0;
}


發佈了149 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章