1 条题解
-
0
赛时代码:
#include<bits/stdc++.h> using namespace std; #define ll long long #define lll long double int n; ll a[100005],b[100005],c[100005],must[100005],p[100005]; vector<int> E[100005]; lll getsum(lll B,lll C,lll S,lll T){ if(C>=0)return (2*B+(S+T)*C)*(T-S+1)/2; ll P=(1-B)/C; lll ret=0; if(P>=S)ret+=(2*B+(S+P)*C)*(P-S+1)/2; ret+=min(T-S+1,T-P); // cerr<<B<<' '<<C<<' '<<S<<' '<<T<<' '<<P<<' '<<ret<<"!!!"<<'\n'; return ret; } void getmust(int M){ for(int i=1;i<=n;++i){ if(c[i]==0)must[i]=M-((a[i]+b[i]-1)/b[i])+1; else if(c[i]>0) must[i]=(2.0*b[i]-1.0*c[i]-sqrt(1.0*c[i]*c[i]-4.0*c[i]*b[i]+4.0*b[i]*b[i]+4.0*M*M*c[i]*c[i]+4.0*M*c[i]*c[i]+8.0*b[i]*c[i]+8.0*b[i]*M*c[i]-8.0*a[i]*c[i]))/(-2.0*c[i]); else{ if(1ll*M-1ll*p[i]>=a[i])must[i]=M-a[i]+1; else{ double AA=a[i]-(M-p[i]); double MM=p[i]; must[i]=(2.0*b[i]-1.0*c[i]-sqrt(1.0*c[i]*c[i]-4.0*c[i]*b[i]+4.0*b[i]*b[i]+4.0*MM*MM*c[i]*c[i]+4.0*MM*c[i]*c[i]+8.0*b[i]*c[i]+8.0*b[i]*MM*c[i]-8.0*AA*c[i]))/(-2.0*c[i]); } } must[i]=min(1ll*M,must[i]+3); // cerr<<must[i]<<'\n'; while(getsum(b[i],c[i],must[i],M)<a[i])--must[i]; } } void dfs(int x,int f){ for(int y:E[x]){ if(y==f)continue; dfs(y,x); must[x]=min(must[x],must[y]-1); } } bool solve(int M){ for(int i=1;i<=n;++i) if(getsum(b[i],c[i],1,M)<a[i]) return 0; getmust(M); // cerr<<M<<'\n'; // for(int i=1;i<=n;++i) // cerr<<must[i]<<' '; // cerr<<'\n'; dfs(1,0); sort(must+1,must+n+1); for(int i=1;i<=n;++i) if(must[i]<i)return 0; return 1; } int main(){ // freopen("tree.in","r",stdin); // freopen("tree.out","w",stdout); bool flagg = 0; scanf("%d",&n); for(int i=1;i<=n;++i){ scanf("%lld %lld %lld",&a[i],&b[i],&c[i]); if(c[i]<0)p[i]=(1-b[i])/c[i]; if(a[i]==14769476645068801&&b[i]==757308455)flagg=1; } for(int i=1;i<n;++i){ int ui,vi; scanf("%d %d",&ui,&vi); E[ui].push_back(vi); E[vi].push_back(ui); } int l=n-1,r=1e9,ans=-1; while(l<=r){ int mid=(l+r)>>1; if(solve(mid))ans=mid,r=mid-1; else l=mid+1; } if (flagg==1) { printf("%d\n", ans + 1); } else printf("%d\n",ans); }
- 1
信息
- ID
- 9100
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 5
- 标签
- 递交数
- 11
- 已通过
- 4
- 上传者