90ptsWA求调,回复必关
查看原帖
90ptsWA求调,回复必关
305735
Jean_Gunnhildr楼主2024/11/26 20:17
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e4+5;

int n,m,ma,u,v;//n件物品,m条关系,ma为我有的钱
int dp[N],fa[N],fc[N],fv[N];
//fc存代价,fv村价值

inline int f(int x){//路径压缩并查集
	if(fa[x]==x) return x;
	int tmp=f(fa[x]);
	fc[tmp]+=fc[fa[x]];
	fv[tmp]+=fv[fa[x]];
	fa[x]=tmp;
	return tmp;
}
bool vis[N];
vector< pair<int,int> > obj;

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m>>ma;
	for(int i=1;i<=n;i++) cin>>fc[i]>>fv[i];
	for(int i=1;i<=n;i++) fa[i]=i; 
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		int f_a=f(u);
		int f_b=f(v);
		if(f_a==f_b) continue;
		else{
			fa[f_a]=f_b;
			fc[f_b]+=fc[f_a];
			fv[f_b]+=fv[f_a];
		}
	}
	for(int i=1;i<=n;i++){
		int fi=f(i);
		if(vis[fi]) continue;
		vis[fi]=true;
		obj.push_back({fc[fi],fv[fi]});
	}
	for(pair<int,int> i:obj){
		for(int j=ma;j>=i.first;j--){
			dp[j]=max(dp[j],dp[j-i.first]+i.second);
		}
	}
	cout<<dp[ma];
	return 0;
}
/*
20 10 100
31 42
14 58
27 3
19 66
38 76
27 93
37 82
45 78
28 79
25 80
8 85
44 49
12 79
35 7
45 2
4 77
12 94
3 29
26 71
46 92
17 1
8 19
14 12
4 11
12 15
19 10
4 7
17 14
3 6
17 13

read 335
expected 418
*/
2024/11/26 20:17
加载中...