http://ybt.ssoier.cn:8088/problem_show.php?pid=1387
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct cloud{ 4 int c; 5 int d; 6 int fa; 7 }; 8 cloud f[10010]; 9 cloud a[10010]; 10 int n, m, w, c, d, u, v; 11 int find_root(int x){ 12 if(f[x].fa != x) 13 f[x].fa = find_root(f[x].fa); 14 return f[x].fa; 15 } 16 int ans[10010]; 17 int main() 18 { 19 scanf("%d%d%d",&n, &m, &w); 20 for(int i = 1; i <= n; i++){ 21 scanf("%d%d",&c, &d); 22 f[i].fa=i; 23 f[i].c=c; 24 f[i].d=d; 25 } 26 for(int i = 1; i <= m; i++){ 27 scanf("%d%d", &u, &v); 28 int uu = find_root(u); 29 int vv = find_root(v); 30 if(uu != vv) { 31 f[uu].fa = vv; 32 f[vv].c += f[uu].c; 33 f[vv].d += f[uu].d; 34 } 35 } 36 int cnt=0; 37 for(int i = 1; i <= n; i++){//把各个集合存放到新的数组a里 38 if(f[i].fa == i){ 39 a[++cnt].c = f[i].c; 40 a[cnt].d = f[i].d; 41 } 42 } 43 for(int i=1; i <= cnt; i++){//01背包求解 44 for(int j=w; j>=a[i].c; j--){ 45 ans[j]=max(ans[j], ans[j-a[i].c]+a[i].d); 46 } 47 } 48 printf("%d", ans[w]); 49 return 0; 50 }