并查集详解
https://blog.csdn.net/sjystone/article/details/115406797
搭配购买 https://www.acwing.com/problem/content/1254/
并查集和01背包问题结合
将一组云放入一个集合中,在每个集合的跟节点维护该集合的价钱和价值,即体积和重量
用01背包解法遍历每一个跟节点
#include <iostream>
using namespace std;
const int N = 10010;
int f[N], w[N], v[N], n, m, vol, p[N];
int find(int x)
{
if ( x != p[x] ) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n >> m >> vol;
for ( int i = 1; i <= n; i ++ )
{
cin >> v[i] >> w[i];
p[i] = i;
}
while ( m -- )
{
int a, b;
cin >> a >> b;
int pa = find(a), pb = find(b);
if ( pa != pb )
{
v[pb] += v[pa];
w[pb] += w[pa];
p[pa] = pb;
}
}
for ( int i = 1; i <= n; i ++ )
if ( i == p[i] )
for ( int j = vol; j >= v[i]; j -- )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[vol];
return 0;
}