// Decline is inevitable,
// Romance will last forever.
// POJ1417
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
using namespace std;
#define mst(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
//#define int long long
const int maxn = 1e4+ 10;
const int maxm = 25;
const int P = 1e9 + 7;
int fa[maxn];
int n;
struct node {
int p, d;
bool operator < (const node &x) const {
return p > x.p;
}
}g[maxn];
int find(int x) {
if(fa[x] == -1) return x;
return fa[x] = find(fa[x]);
}
void solve() {
while(scanf("%d", &n) != EOF) {
mst(fa, -1);
mst(g, 0);
for(int i = 1; i <= n; i++)
scanf("%d%d", &g[i].p, &g[i].d);
sort(g + 1, g + n + 1);
int ans = 0;
for(int i = 1; i <= n; i++) {
int Fa = find(g[i].d);
if(Fa > 0)
ans += g[i].p, fa[Fa] = Fa - 1;
int z = 0;
}
printf("%d\n", ans);
}
}
signed main() {
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int T; scanf("%d", &T); while(T--)
// int T; cin >> T; while(T--)
solve();
return 0;
}
POJ1456 Supermarket ——并查集 + 贪心 ?
最新推荐文章于 2024-08-25 23:21:26 发布