$Vijos P1250$

背包?
跑完并查集 分组背包完事

#include <bits/stdc++.h>
#define rep(i,j,n) for(register int i=j;i<=n;i++)
#define Rep(i,j,n) for(register int i=j;i>=n;i--)
#define low(x) x&(-x)
using namespace std ;
typedef long long LL ;
const int inf = INT_MAX >> 1 ;
inline LL In() { LL res(0) , f(1) ; register char c ;
#define gc c = getchar()
	while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
	return res * f ;
#undef gc
}

int n , m , k ;
const int N = 1000 + 5 ;
int p[N] , w[N] ;
int fa[N] ;
int cnt[N] ;
int V[N][N] ;
int W[N][N] ;
int dp[N] ;

inline int find(int x) {
	return fa[x] == x ? fa[x] : fa[x] =find(fa[x]) ;
}
inline void merge(int x,int y) {
	fa[find(x)] = find(y) ;
}

inline void Ot() {
	memset(V,0,sizeof(V)) ;
	memset(W,0,sizeof(W)) ;
	n = In() , m = In() , k = In() ;
	rep(i,1,n) fa[i] = i ;
	rep(i,1,n) p[i] = In() , w[i] = In() ;
	rep(u,1,k) merge(In() , In()) ;
	rep(i,1,n) {
		int q = find(i) ;
		V[q][++cnt[q]] = p[i] ;
		W[q][cnt[q]] = w[i] ;
	}
	rep(i,1,n) Rep(j,m,0) rep(u,1,cnt[i])
	W[i][u] <= j ? dp[j] = max(dp[j] , dp[j-W[i][u]]+V[i][u]) : 0 ;
	cout << dp[m] << endl ;
}
signed main() {
	return Ot() , 0 ;
}
posted @ 2019-04-01 19:05  Isaunoya  阅读(162)  评论(0编辑  收藏  举报
TOP