输入样例:
5 3 10
3 10
3 10
3 10
5 100
10 1
1 3
3 2
4 2
输出样例:
1
#include <bits/stdc++.h>
#define bug cout << "***************" << endl
#define fuck(x) cout << #x << " -> " << x << endl
#define endl '\n'
#define int long long
using namespace std;
constexpr int N = 1e6 + 10, inf = 0x3f3f3f3f;
int n, m, vol;
int p[N];
int v[N];
int w[N];
int dp[N];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
void solve()
{
cin >> n >> m >> vol;
// 初始化
for (int i = 1; i <= n; i++)
{
p[i] = i;
}
for (int i = 1; i <= n; i++)
{
cin >> v[i] >> w[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] = p[pb];
}
}
for (int i = 1; i <= n; i++)
{
if (p[i] == i)
for (int j = vol; j >= v[i]; j--) // 枚举背包重量,从后向前。
{
if (j >= v[i]) // 如果可以放得下;
dp[j] = max(dp[j], dp[j - v[i]] + w[i]); // dp[j]表示的是重量为j时背包价值最大值;
}
}
cout << dp[vol] << endl;
}
signed main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T = 1;
while (T--)
{
solve();
}
return 0;
}