#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define reg register
#define IOS ios::sync_with_stdio(false)
int n, m, mon;
int c[MAXN];
int d[MAXN];
int par[MAXN];
int dp[MAXN];
void init() {
for (int i = 1; i <= n; i++) {
par[i] = i;
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
int main() {
scanf("%d %d %d", &n, &m, &mon);
init();
for (int i = 1; i <= n; i++) {
scanf("%d %d", d + i, c + i);
}
int t1, t2;
for (int i = 1; i <= m; i++) {
scanf("%d %d", &t1, &t2);
par[find(t1)] = find(t2);
}
for (int i = 1; i <= n; i++) {
if (par[i] != i) {
d[find(i)] += d[i];
d[i] = 0;
c[find(i)] += c[i];
c[i] = 0;
}
}
for (int i = 1; i <= n; i++) {
if (d[i] == 0) {
continue;
}
for (int j = mon; j >= d[i]; j--) {
dp[j] = max(dp[j], dp[j - d[i]] + c[i]);
}
}
printf("%d", dp[mon]);
return 0;
}
P1455 搭配购买(dp+并查集)
最新推荐文章于 2024-08-19 11:12:00 发布