The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#define M 10020
using namespace std;
int n;
int par[M];
void init(int n)
{
for(int i = 0; i < M; i++)
par[i] = i;
}
struct node
{
int pi;
int di;
};
node super[M];
int fond(int x)//找根
{
return x == par[x] ? x : fond(par[x]);
}
bool cmp(node x, node y)
{
return x.pi > y.pi;
}
int main()
{
while(~scanf("%d",&n))
{
init(n);
int sum = 0;
for( int i = 0; i < n; i++)
scanf("%d%d",&super[i].pi,&super[i].di);
sort(super,super+n,cmp);
for( int i = 0; i < n; i++)
{
int temp = fond(par[super[i].di]);
if(temp > 0)
{
sum += super[i].pi;
par[temp] = temp -1;
}
}
printf("%d\n",sum);
}
return 0;
}