求助,为什么这样过不去
查看原帖
求助,为什么这样过不去
1121439
TMLY114514楼主2024/7/3 20:32
#include<bits/stdc++.h>
using namespace std;
namespace IO{
#define ll long long
#define ischar(ch) (32<ch&&ch<127)
#define SIZE 1<<18
	char ibuf[SIZE],*is=ibuf,*it=ibuf;
#define getchar() (is==it&&(it=(is=ibuf)+fread(ibuf,1,SIZE,stdin),is==it)?EOF:*is++)
	template<typename Type>
	inline void read(Type &a){
		Type x=0;int y=0,ch=getchar();
		while(!isdigit(ch)&&(ch^EOF))y|=(ch=='-'),ch=getchar();
		while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
		a=y?-x:x;
	}
	inline void read(char &a){
		int ch=getchar();
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		a=ch;
	}
	inline void read(string &a){
		a="";char ch;
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		while(ischar(ch))a+=ch,ch=getchar();
	}
	inline void read(short &a){
		int ch=getchar();
		while(!isdigit(ch)&&(ch^EOF))ch=getchar();
		a=(ch^48);
	}
	template<typename Type>
	inline void read(Type a[]){
		Type *pos=a;
		int ch=getchar();
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		while(ischar(ch))*pos++=ch,ch=getchar();
		*pos=0;
	}
	inline void read(double &a){
		int zh=0,w=0,ch=getchar();
		double xi=0.0,x=1.0;
		while (!isdigit(ch)&&(ch^EOF))w|=ch=='-',ch=getchar();
		while(isdigit(ch))zh=(zh<<1)+(zh<<3)+(ch^48),ch=getchar();
		if(ch^'.')return void(a=w?-zh:zh);
		ch=getchar();
		while(isdigit(ch))x*=10.0,xi+=(ch^48)*1.0/x,ch=getchar();
		a=w?-zh-xi:zh+xi;
	}
	template<typename Type,typename ...Args>
	inline void read(Type &a,Args &...as){read(a);read(as...);}
	char obuf[SIZE],*op=obuf;
	inline void putch(const char &c){
		if(op-obuf==SIZE)fwrite(obuf,1,op-obuf,stdout),op=obuf;
		*op++=c;
	}
	template<typename Type>
	inline void write(Type x){
		if(x<0)putch('-'),x=-x;
		static int s[35],top;top=0;
		do s[top++]=x%10,x/=10;
		while(x);
		while(top)putch(s[--top]+'0');
	}
	int precision;
	inline void write(double x){
		ll n=1,base=10,k=precision;
		while(k)n=(k&1)?n*base:n,base*=base,k>>=1;
		x=round(x*n)/n;
		if(!x){
			putch('0'),putch('.');
			for(int i=1;i<=precision;++i)putch('0');
			return;
		}
		if(x<0)putch('-'),x=-x;
		ll y=(ll)round(x*n)%n;x=(ll)x;
		write((ll)x);putch('.');
		int s[35],top=0;
		for(;top<precision;y/=10)s[++top]=y%10;
		while(top)putch(s[top--]+'0');
	}
	inline void write(char ch){putch(ch);}
	inline void write(const char* s){for(int i=0;i<strlen(s);++i)putch(s[i]);}
	inline void write(string s){for(int i=0;i<s.size();++i)putch(s[i]);}
	template<typename Type,typename ...Args>
	inline void write(Type a,Args ...as){write(a);write(as...);}
	inline void oclear(){fwrite(obuf,1,op-obuf,stdout);}
#undef SIZE
#undef ischar
}
using namespace IO;
#define int long long
#define ull __int128
#define N 100010
const int inf=2e18;
int n,a[N],b[N],c[N];
inline int f(int i,int x){return max(b[i]+x*c[i],1ll);}
inline ull gethight(int i,ull l,ull r){
	if(c[i]>0)return (r-l+1)*(f(i,l)+f(i,r))/2;
	else if(c[i]<0){
		int x=ceil((1-b[i])*1.0/c[i]*1.0);
		if(x<=l)return r-l+1;
		else if(x<=r)return r-x+1+(x-l)*(f(i,l)+f(i,x-1))/2;
		else return (r-l+1)*(f(i,l)+f(i,r))/2;
	}else return b[i]*(r-l+1);
}
int t[N],id[N];
inline int cmp(int x,int y){return t[x]<t[y];}
inline int getcost(int i,int t){
	int l=1,r=n,res;
	while(l<=r){
		int mid=l+r>>1;
		if(gethight(i,mid,t)>=a[i])l=mid+1,res=mid;
		else r=mid-1;
	}
	return res;
}
vector<int>e[N];
#define add(u,v) e[u].push_back(v),e[v].push_back(u)
int vis[N],fa[N],st[N];
inline void dfs(int u,int ft){fa[u]=ft;for(auto v:e[u])if(v^ft)dfs(v,u);}
inline int check(int x){
	for(int i=1;i<=n;++i)t[i]=getcost(i,x),id[i]=i,vis[i]=0;
	sort(id+1,id+n+1,cmp);
	for(int i=1,date=0;i<=n;++i){
		int u=id[i],top=0;
		while(!vis[u])vis[st[++top]=u]=1,u=fa[u];
		for(int j=top;j;--j)if(t[st[j]]<++date)return 0;
		//there
	}
	return 1;
}
signed main(){
	read(n);
	for(int i=1;i<=n;++i)read(a[i],b[i],c[i]);
	for(int i=1,u,v;i<n;++i)read(u,v),add(u,v);
	int l=n,r=1e9,ans;
	vis[0]=1;
	dfs(1,0);
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid))r=mid-1,ans=mid;
		else l=mid+1;
	}
	write(ans);
	oclear();
	return 0;
} 

#include<bits/stdc++.h>
using namespace std;
namespace IO{
#define ll long long
#define ischar(ch) (32<ch&&ch<127)
#define SIZE 1<<18
	char ibuf[SIZE],*is=ibuf,*it=ibuf;
#define getchar() (is==it&&(it=(is=ibuf)+fread(ibuf,1,SIZE,stdin),is==it)?EOF:*is++)
	template<typename Type>
	inline void read(Type &a){
		Type x=0;int y=0,ch=getchar();
		while(!isdigit(ch)&&(ch^EOF))y|=(ch=='-'),ch=getchar();
		while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
		a=y?-x:x;
	}
	inline void read(char &a){
		int ch=getchar();
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		a=ch;
	}
	inline void read(string &a){
		a="";char ch;
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		while(ischar(ch))a+=ch,ch=getchar();
	}
	inline void read(short &a){
		int ch=getchar();
		while(!isdigit(ch)&&(ch^EOF))ch=getchar();
		a=(ch^48);
	}
	template<typename Type>
	inline void read(Type a[]){
		Type *pos=a;
		int ch=getchar();
		while(!ischar(ch)&&(ch^EOF))ch=getchar();
		while(ischar(ch))*pos++=ch,ch=getchar();
		*pos=0;
	}
	inline void read(double &a){
		int zh=0,w=0,ch=getchar();
		double xi=0.0,x=1.0;
		while (!isdigit(ch)&&(ch^EOF))w|=ch=='-',ch=getchar();
		while(isdigit(ch))zh=(zh<<1)+(zh<<3)+(ch^48),ch=getchar();
		if(ch^'.')return void(a=w?-zh:zh);
		ch=getchar();
		while(isdigit(ch))x*=10.0,xi+=(ch^48)*1.0/x,ch=getchar();
		a=w?-zh-xi:zh+xi;
	}
	template<typename Type,typename ...Args>
	inline void read(Type &a,Args &...as){read(a);read(as...);}
	char obuf[SIZE],*op=obuf;
	inline void putch(const char &c){
		if(op-obuf==SIZE)fwrite(obuf,1,op-obuf,stdout),op=obuf;
		*op++=c;
	}
	template<typename Type>
	inline void write(Type x){
		if(x<0)putch('-'),x=-x;
		static int s[35],top;top=0;
		do s[top++]=x%10,x/=10;
		while(x);
		while(top)putch(s[--top]+'0');
	}
	int precision;
	inline void write(double x){
		ll n=1,base=10,k=precision;
		while(k)n=(k&1)?n*base:n,base*=base,k>>=1;
		x=round(x*n)/n;
		if(!x){
			putch('0'),putch('.');
			for(int i=1;i<=precision;++i)putch('0');
			return;
		}
		if(x<0)putch('-'),x=-x;
		ll y=(ll)round(x*n)%n;x=(ll)x;
		write((ll)x);putch('.');
		int s[35],top=0;
		for(;top<precision;y/=10)s[++top]=y%10;
		while(top)putch(s[top--]+'0');
	}
	inline void write(char ch){putch(ch);}
	inline void write(const char* s){for(int i=0;i<strlen(s);++i)putch(s[i]);}
	inline void write(string s){for(int i=0;i<s.size();++i)putch(s[i]);}
	template<typename Type,typename ...Args>
	inline void write(Type a,Args ...as){write(a);write(as...);}
	inline void oclear(){fwrite(obuf,1,op-obuf,stdout);}
#undef SIZE
#undef ischar
}
using namespace IO;
#define int long long
#define ull __int128
#define N 100010
const int inf=2e18;
int n,a[N],b[N],c[N];
inline int f(int i,int x){return max(b[i]+x*c[i],1ll);}
inline ull gethight(int i,ull l,ull r){
	if(c[i]>0)return (r-l+1)*(f(i,l)+f(i,r))/2;
	else if(c[i]<0){
		int x=ceil((1-b[i])*1.0/c[i]*1.0);
		if(x<=l)return r-l+1;
		else if(x<=r)return r-x+1+(x-l)*(f(i,l)+f(i,x-1))/2;
		else return (r-l+1)*(f(i,l)+f(i,r))/2;
	}else return b[i]*(r-l+1);
}
int t[N],id[N];
inline int cmp(int x,int y){return t[x]<t[y];}
inline int getcost(int i,int t){
	int l=1,r=n,res;
	while(l<=r){
		int mid=l+r>>1;
		if(gethight(i,mid,t)>=a[i])l=mid+1,res=mid;
		else r=mid-1;
	}
	return res;
}
vector<int>e[N];
#define add(u,v) e[u].push_back(v),e[v].push_back(u)
int vis[N],fa[N],st[N];
inline void dfs(int u,int ft){fa[u]=ft;for(auto v:e[u])if(v^ft)dfs(v,u);}
inline int check(int x){
	for(int i=1;i<=n;++i)t[i]=getcost(i,x),id[i]=i,vis[i]=0;
	sort(id+1,id+n+1,cmp);
	for(int i=1,date=0;i<=n;++i){
		int u=id[i],top=0;
		while(!vis[u])vis[st[++top]=u]=1,u=fa[u];
		for(int j=top;j;--j)if(gethight(st[j],++date,x)<a[st[j]])return 0;
		//这样就能过
	}
	return 1;
}
signed main(){
	read(n);
	for(int i=1;i<=n;++i)read(a[i],b[i],c[i]);
	for(int i=1,u,v;i<n;++i)read(u,v),add(u,v);
	int l=n,r=1e9,ans;
	vis[0]=1;
	dfs(1,0);
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid))r=mid-1,ans=mid;
		else l=mid+1;
	}
	write(ans);
	oclear();
	return 0;
} 
2024/7/3 20:32
加载中...