假期训练——CodeForces - 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses DP+dfs


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 xiand yi (1 ≤ xi, yi ≤ nxi ≠ yi), meaning that Hoses xi and 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.

Example
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.


这道题是0-1背包和dfs的结合,首先用DFS找出在一个group的所有成员,,然后把每个组看为一个人

人的个数就是组的个数,在动态规划时,,第n行,为第n组所有情况的最优解,,(以组为单位或单个成员)

然后其他与0-1背包类似




#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod  1e10+7
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  vector<int> vi ;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}



int n,m,k;
ll w[2005];
ll b[2005];
ll dp[2][100005];

vector<int> aa[1005];

set<int> xx;
set<int>::iterator it;
vector<int> mp[1005];
int  f[2005];
void dfs(int sx);
int ct=1;
int x,y;
int main()
{
	ios::sync_with_stdio(false);
	cin >> n>>m>>k;
	rep(i,1,n)
	{
		cin >> w[i];
	}
	rep(i,1,n)
		cin >> b[i];
	rep(i,1,m)
	{
		cin>>x>>y;
		mp[x].push_back(y);
		mp[y].push_back(x);
	}
	
	for(int i=1;i<=n;i++)
	{
		if(f[i]==0)
		{
			dfs(i);
			ct ++;
		}
		
	}
	/*for(int i=1;i<=n;i++)
		cout << f[i]<<" ";
	cout <<endl;*/
	for(int kk = 1;kk<=ct;kk++)
	{
		ll sumw = 0;
		ll sumb = 0;
		for(int i=1;i<=n;i++)
		{
			if(f[i]==kk)
			{
				aa[kk].push_back(i);
				sumw += w[i];
				sumb += b[i];
 			}
		}
		w[++n]=sumw;
		b[n]  =sumb;
		aa[kk].push_back(n);
	}
	
	
	/*for(int i=1;i<=ct;i++)
	{
		for(int j=0;j<aa[i].size();j++)
		{
			cout << aa[i][j]<<" "; 
		}
		cout << endl;
	}*/
	
	for(int i=1;i<=ct;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[i%2][j] =dp[(i-1)%2][j];
			for(int ii=0;ii<aa[i].size();ii++)
			{
				if(j-w[aa[i][ii]]>=0)
				{	
					if(dp[i%2][j]<dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]])
					{
						dp[i%2][j] =dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]];	
					} 	
				}
			}
		}
	}
	
	

	cout<<dp[ct%2][k]<<endl;
	
	
	
	
	
	return 0;
} 



void dfs(int sx)
{
	f[sx] = ct;
	for(int i=0;i<mp[sx].size();i++)
	{
		int nex = mp[sx][i];
		if(f[nex]==0)
		{
			dfs(nex);	
		}	
	}	
} 



CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值