QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#292251 | #7966. 采矿 | Famiglistmo | TL | 0ms | 0kb | C++14 | 3.9kb | 2023-12-27 22:03:31 | 2023-12-27 22:03:32 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=305;
const ll inf=1e15;
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
int s=0,w=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();};
while(isdigit(ch))s=s*10+(ch^48),ch=getchar();
return s*w;
}
ll f[N][N][N],g[N][N][N],h[N][N];
int fath[N],r[N],p[N],siz[N],rev[N],dfn[N];
vector<int> son[N];
vector<ll> val[N][2];
int n,q,s,tim,cnt;
bool check(int u,int v){
return dfn[u]<=dfn[v]&&dfn[v]<=dfn[u]+siz[u]-1;
}
void dfs(int u){
siz[u]=1,dfn[u]=++tim,rev[tim]=u;
for(auto v:son[u])if(v)dfs(v),siz[u]+=siz[v];
}
void clear(){
for(int i=1;i<=n;++i)
for(int j=0;j<=n;++j)
for(int k=0;k<=n;++k)
f[i][j][k]=-inf;
for(int i=1;i<=n;++i)
for(int j=0;j<=n;++j)
h[i][j]=-inf;
}
void load(){
for(int i=1;i<=n;++i)
for(int j=0;j<=n;++j)
for(int k=0;k<=n;++k)
g[i][j][k]=f[i][j][k];
}
int get(int i,int _,int j){
if(j<0||j>=val[i][_].size())return 0;
return val[i][_][j];
}
bool cmax(ll &a,ll b){ return a<b?a=b,1:0; }
#define JK for(int j=0;j<=siz[son[i][0]];++j)for(int k=0;k<=siz[son[i][1]];++k)
void work(int tp){
if(tp==1){
for(int i=1;i<=n;++i)
JK if(cnt-j-k<n-siz[i])
cmax(h[i][j+k],g[i][j][k]);
for(int _=n;_>=1;--_){
int i=rev[_];
JK if(cnt-j-k<=n-siz[i]){
if(son[i][0]) cmax(f[i][j][k],h[son[i][0]][j]);
if(son[i][1]) cmax(f[i][j][k],h[son[i][1]][k]);
if(cnt-j-k<n-siz[i]) cmax(h[i][j+k],f[i][j][k]);
}
}
}
if(tp==2){
for(int i=1;i<=n;++i) JK {
if(son[i][0]&&j<siz[son[i][0]])
cmax(h[son[i][0]][j],g[i][j][k]);
if(son[i][1]&&k<siz[son[i][1]])
cmax(h[son[i][1]][k],g[i][j][k]);
}
for(int _=1;_<=n;++_){
int i=rev[_];
JK if(j+k<=cnt&&cnt-j-k<=n-siz[i]){
cmax(f[i][j][k],h[i][j+k]);
if(son[i][0]&&j<siz[son[i][0]])
cmax(h[son[i][0]][j],f[i][j][k]);
if(son[i][1]&&k<siz[son[i][1]])
cmax(h[son[i][1]][k],f[i][j][k]);
}
}
}
if(tp==3){
for(int i=1;i<=n;++i) JK
if(cnt-j-k<n-siz[i]) f[i][j][k]=g[i][j][k];
++cnt;
}
if(tp==4){
for(int i=1;i<=n;++i) JK
if(cnt-j-k>0) f[i][j][k]=g[i][j][k];
--cnt;
}
for(int i=1;i<=n;++i) JK if(f[i][j][k]>=0)
f[i][j][k]+=get(son[i][0],0,j)+get(son[i][1],0,k)+get(i,1,cnt-j-k)+r[i];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>q>>s;
for(int i=2;i<=n;++i)
cin>>fath[i],son[fath[i]].push_back(i);
for(int i=1;i<=n;++i)while(son[i].size()<2)son[i].push_back(0);
for(int i=2;i<=n;++i)cin>>r[i];
for(int i=2;i<=n;++i)cin>>p[i];
dfs(1);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)
if(check(i,j))val[i][0].push_back(p[j]);
else val[i][1].push_back(p[j]);
for(int _=0;_<2;++_){
sort(val[i][_].begin(),val[i][_].end(),greater<int>());
val[i][_].insert(val[i][_].begin(),0);
for(int j=1;j<(int)val[i][_].size();++j)
val[i][_][j]+=val[i][_][j-1];
}
}
clear();
f[s][0][0]=0;
for(int i=1,tp;i<=q;++i)
load(),clear(),cin>>tp,work(tp);
ll ans=-inf;
for(int i=1;i<=n;++i)
for(int j=0;j<=n;++j)
for(int k=0;k<=n;++k)
ans=max(ans,f[i][j][k]);
if(ans<0) cout<<"No solution.\n";
else cout<<ans<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
300 600 175 1 2 1 4 4 2 7 8 9 8 3 5 12 10 6 6 13 15 9 11 11 13 15 22 14 26 27 12 5 16 10 24 23 16 33 34 21 22 37 39 17 39 40 20 36 28 40 33 48 29 26 46 46 18 37 32 20 38 54 45 19 30 52 27 18 60 41 57 30 50 48 47 65 17 35 14 55 24 78 80 71 81 28 3 21 19 63 35 75 75 73 92 89 36 81 50 34 93 85 43 42 78...