萌新 WA 90 pts 求调,悬一关
查看原帖
萌新 WA 90 pts 求调,悬一关
1088663
zzzyyyyhhhhh楼主2024/10/21 09:06
#include<bits/stdc++.h>
using namespace std;
#define int __int128
const int N = 1e5+100;
int n;
struct treee
{
	int a,b,c;
}tree[N];
vector<int> a[N];
int f[N];
struct node
{
	int x,y;
	bool operator<(const node z)const
	{
		return y>z.y;
	}	
};
priority_queue<node> q;
int get(int a,int b,int c,int x,int d)
{
	// cout<<a<<' '<<b<<' '<<c<<' '<<x<<' '<<d<<'\n';
	if(c==0)return (x-d+1)*b;
	if(c>=0)
	{
		return (b+d*c+b+x*c)*(x-d+1)/2;
	}
	else
	{
		int tmp=-b/c;
		if(tmp<d)return (x-d+1);
		return (tmp-d+1)*(b+d*c+b+c*tmp)/2+x-tmp+1;
	}
}
void dfs(int x,int fa)
{
	f[x]=fa;
	for(auto i:a[x])
	{
		if(i==fa)continue;
		dfs(i,x);
	}
	return;
}
int get(int a,int b,int c,int x)
{
	// cout<<x<<'\n';
	int l=1,r=x,mid,ans=-1;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(get(a,b,c,x,mid)>=a)
		{
			ans=mid;
			l=mid+1;
		}
		else
		{
			r=mid-1;
		}
	}
	return ans;
}
bool v[N];
bool check(int x)
{
	// cout<<x<<'\n';
	for(int i=1;i<=n;i++)v[i]=0;
	while(!q.empty())q.pop();
	int tmp;
	// cout<<get(12,1,1,5,1)<<' ';
	if(get(tree[1].a,tree[1].b,tree[1].c,x)==-1)return 0;
	for(int i=2;i<=n;i++)
	{
		tmp=get(tree[i].a,tree[i].b,tree[i].c,x);
		// cout<<i<<' '<<tmp<<'\n';
		if(tmp==-1)return 0; 
		q.push({i,tmp-1});
	}
	// cout<<"FU\n";
	int now=1;
	v[1]=1;
	while(!q.empty())
	{
		tmp=q.top().x;
		while(!v[tmp])v[tmp]=1,now++,tmp=f[tmp];
		if(now>q.top().y)return 0;
		q.pop();
	}
	return 1;
}
void r(int &);
void w(int);
signed main()
{
	// freopen("tree3.in","r",stdin);
	r(n);
	int x,y,z,mi=1e18,mx=0;
	for(int i=1;i<=n;i++)
	{
		r(x),r(y),r(z);
		tree[i]={x,y,z};
		mx=max(mx,x);
		mi=min(mi,x/(y+z*z));
	}
	for(int i=1;i<n;i++)
	{
		 r(x),r(y);
		 a[x].push_back(y);
		 a[y].push_back(x);
	}
	dfs(1,0);
	// cout<<"ok\n";
	int l=1,r=1e10,mid,ans=0;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(check(mid))
		{
			r=mid-1;
			ans=mid;
		}
		else l=mid+1;
	}
	// for(int i=1;i<=1e18;i++)
	// {
	// 	if(check(i))
	// 	{
	// 		ans=i;
	// 		break;
	// 	}
	// }
	// for(int i=ans;i<=ans+1000;i++)
	// {
	// 	cout<<check(i)<<' ';
	// }
	// cout<<check(25526156)<<' '<<check(25526155)<<' '<<check(25526157)<<'\n';
	w(ans);
}
void r(int &x)
{
	x=0;
	char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))
	{
		x*=10;
		x+=c^48;
		c=getchar();
	}
}
void w(int x)
{
	if(x<10)
	{
		putchar(x+'0');
		return;
	}
	w(x/10);
	putchar(x%10+'0');
}
2024/10/21 09:06
加载中...