背包?

跑完并查集 分组背包完事

#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 ;
}

随机推荐

  1. FTP配置

    常用的匿名FTP配置项   anonymous_enable=YES           是否允许匿名用户访问   anon_umask=022                匿名用户所上传文件的权限 ...

  2. 阿里云安装nodejs

    cd进入root目录下: cd /root 下载node.js安装包 wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.x ...

  3. graph.h

    #ifndef _GRAPH_#define _GRAPH_#include<stdio.h>#include<stdlib.h>#include<string.h> ...

  4. [luoguP2760] 科技庄园(背包DP)

    传送门 每次拿完还得回去... 数据中有两个需要注意的地方: 存在桃树上有桃子但是摘 0 次的情况 题目中要求体力不能为0,因此就算到达了重点体力也不能为0,所以实际上允许使用的体力为 a - 1 把 ...

  5. UVAL - 6755 - Swyper Keyboard

    先上题目: https://icpcarchive.ecs.baylor.edu/external/67/6755.pdf 题目复制起来比较麻烦. 题意:定义一种操作:给出一个字符串,然后手指就按照给 ...

  6. C语言试题(标准答案)

    C语言试题(标准答案) 一.单选题(每小题2分,共20小题40分) 1.1-1.5    B D A C B                1.6-1.10    C A D B C 1.11-1.1 ...

  7. [51Nod1089] 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   回文串是指aba.abba.cccbccc.aaaa这种左右对称 ...

  8. SiteMesh2-sitemesh.xml的其它映射器的用法

    在sitemesh.xml中做常用的三个映射器,总结如下: 映射器元素的顺序确定优先级.良好的应用程序应使用以下顺序, Parameter query = ParameterDecoratorMapp ...

  9. Linux: 通过命令行上传文件到ftp服务器

    url -T fie-name ftp://server-address --user user:password

  10. WinMain和MFC的差别

    API(Application Programming Interface):开放给应用程序调用的系统功能. 一个Windows Application(SDK): WinMain ReristerC ...