[JSOI2016] Best Group

Portal

This question is obviously a fraction of planning it
so we first half, then we can consider how c h e c k check
we find that this last recommendation relations can be imagined as a tree
if a point wants to be elected, then we need his father was chosen
so that a relatively simple tree dp \operatorname{dp}
f [ in ] [ j ] \operatorname{f}[u][j] indicates to u in for the sub-tree root node has been selected. j j maximum value of the individual
f [ u ] [ j ] = max { f [ u ] [ j k ] + f [ v ] [ k ] } \operatorname{f}[u][j]=\max\{\operatorname{f}[u][j-k]+\operatorname{f}[v][k]\}
initial value is
f [ u ] [ 0 ] = 0 , f [ u ] [ 1 ] = p u δ s u \operatorname{f}[u][0]=0,\operatorname{f}[u][1]=p_u-\delta\cdot s_u
among them δ \delta is the second value

# include <cstdio>
# include <algorithm>
# include <cstring>
# include <cmath>
# include <climits>
# include <iostream>
# include <string>
# include <queue>
# include <stack>
# include <vector>
# include <set>
# include <map>
# include <cstdlib>
# include <ctime>
using namespace std;

# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)

typedef long long ll;
const int N=2505;
const int mod=1e9+7;
const double eps=1e-5;
template <typename T> void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
	x*=f;
}

int K,n;
int s[N],p[N],fa[N];
int head[N],cnt;
int siz[N];
double f[N][N];
double l,r=10000.0;

struct Edge{
	int to,next,w;	
}e[N<<1];

void add(int x,int y){
	e[++cnt]=(Edge){y,head[x]},head[x]=cnt;	
}

void dfs(int u,int fa,double delta){
	siz[u]=1;
	f[u][0]=0;
	f[u][1]=(double)p[u]-delta*s[u];
	for(int i=head[u];~i;i=e[i].next){
		int v=e[i].to;
		if(v==fa)continue;
		dfs(v,u,delta);
		for(int j=min(siz[u],K+1);j>=1;j--)
			for(int k=0;k<=siz[v]&&j+k<=K+1;k++)
				f[u][j+k]=max(f[u][j+k],f[u][j]+f[v][k]);
		siz[u]+=siz[v];
	}
}

bool check(double x){
	Rep(i,0,n)
		Rep(j,0,n)
			f[i][j]=-1e9;
	dfs(0,-1,x);
	return f[0][K+1]>0;	
}

int main()
{
	memset(head,-1,sizeof(head));
	read(K),read(n);
	Rep(i,1,n)read(s[i]),read(p[i]),read(fa[i]),add(fa[i],i),add(i,fa[i]);
	while(r-l>eps){
		double mid=(l+r)/2;
		if(check(mid))l=mid;
		else r=mid;	
	}
	printf("%.3lf\n",l);
	return 0;
}
Published 45 original articles · won praise 52 · views 9636

Guess you like

Origin blog.csdn.net/devout_/article/details/104327578