QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#300780 | #6298. Coloring | kid_magic | RE | 0ms | 0kb | C++14 | 2.7kb | 2024-01-08 19:53:14 | 2024-01-08 19:53:15 |
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=5005;
int n,s;
int a[MAXN];
int w[MAXN];
int p[MAXN];
vector<int>g[MAXN];
int Rd[MAXN];
int vis[MAXN];
int Lorp[MAXN];
int cnt_lorp;
void Find_lorp(int x)
{
if(vis[x])
{
return;
}
vis[x]=1;
Lorp[++cnt_lorp]=x;
Find_lorp(a[x]);
}
bool fxw=0;
int oex[MAXN];
long long dp[MAXN][MAXN],Pre[MAXN][MAXN];
void dfs(int x,int f)
{
for(int i=0;i<g[x].size();i++)
{
int v=g[x][i];
if(v==f)
{
continue;
}
if(Rd[v])
{
continue;
}
dfs(v,x);
}
for(int i=0;i<=n;i++)
{
dp[x][i]=(long long)w[x]*((i&1))-((long long)i*p[x]);
for(int j=0;j<g[x].size();j++)
{
int v=g[x][j];
if(v==f)
{
continue;
}
if(Rd[v])
{
continue;
}
dp[x][i]+=Pre[v][i];
}
}
if(x==s)
{
dp[x][0]=-2e18;
}
Pre[x][0]=dp[x][0];
for(int i=1;i<=n;i++)
{
Pre[x][i]=max(Pre[x][i-1],dp[x][i]);
}
if(s==x)
{
fxw=1;
}
}
int Nox[MAXN];
long long F[MAXN][3];
int main()
{
freopen("date.in","r",stdin);
freopen("date.out","w",stdout);
scanf("%d %d",&n,&s);
oex[s]=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
g[a[i]].push_back(i);
Rd[a[i]]++;
}
queue<int>Q;
for(int i=1;i<=n;i++)
{
if(!Rd[i])
{
Q.push(i);
}
}
while(Q.size())
{
int tmp=Q.front();
Q.pop();
Rd[a[tmp]]--;
if(!Rd[a[tmp]])
{
Q.push(a[tmp]);
}
}
for(int i=1;i<=n;i++)
{
if(Rd[i]&&(!vis[i]))
{
//cerr<<i<<endl;
cnt_lorp=0;
Find_lorp(i);
for(int j=1;j<=cnt_lorp;j++)
{
//cerr<<Lorp[j]<<endl;
dfs(Lorp[j],0);
}
if(fxw)
{
if(vis[s])
{
int Sta;
for(int j=1;j<=cnt_lorp;j++)
{
if(Lorp[j]==s)
{
Sta=j;
}
}
for(int j=1;j<=cnt_lorp;j++)
{
Nox[j]=Lorp[(Sta+j-2)%cnt_lorp+1];
}
for(int j=1;j<=cnt_lorp;j++)
{
Lorp[j]=Nox[j];
//printf("%d ",Lorp[j]);
}
long long Res=-2e18;
for(int x=1;x<=n;x++)
{
F[1][0]=dp[Lorp[1]][x+1];
F[1][1]=F[1][2]=-2e18;
for(int j=2;j<=cnt_lorp;j++)
{
F[j][0]=F[j][1]=F[j][2]=-2e18;
F[j][0]=F[j-1][0]+dp[Lorp[j]][x+1];
F[j][1]=max(F[j-1][0],F[j-1][1])+dp[Lorp[j]][x];
F[j][2]=max(F[j-1][0],max(F[j-1][2],F[j-1][1]))+dp[Lorp[j]][x-1];
}
Res=max(Res,max(F[cnt_lorp][0],max(F[cnt_lorp][1],F[cnt_lorp][2])));
}
printf("%lld\n",Res);
}
else
{
printf("%lld\n",max(dp[s][0],dp[s][1]));
}
return 0;
}
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
3 1 -1 -1 2 1 0 0 3 1 2