QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#79763 | #4815. Flower's Land | huzhaoyang | WA | 1ms | 4508kb | C++14 | 1.5kb | 2023-02-20 21:15:26 | 2023-02-20 21:15:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=40005,M=3005;
int n,m,x,y,rt,a[N],vis[N],dfn[N],sz[N];
ll ans[N],fp[N][M],fs[N][M];
vector<int>e[N];
void get_sz(int k,int fa){
sz[k]=1;
for(int i:e[k])
if ((!vis[i])&&(i!=fa))get_sz(i,k),sz[k]+=sz[i];
}
void get_rt(int k,int fa,int s){
int mx=s-sz[k];
for(int i:e[k])
if ((!vis[i])&&(i!=fa))get_rt(i,k,s),mx=max(mx,sz[i]);
if (mx<=(s>>1))rt=k;
}
void get_dfn(int k,int fa){
dfn[++dfn[0]]=k,sz[k]=1;
for(int i:e[k])
if ((!vis[i])&&(i!=fa))get_dfn(i,k),sz[k]+=sz[i];
}
void dfs(int k){
get_sz(k,0);
if (sz[k]<m)return;
get_rt(k,0,sz[k]),k=rt;
dfn[0]=0,get_dfn(k,0);
for(int i=1;i<=dfn[0]+1;i++){
memset(fp[i],0,sizeof(fp[i]));
memset(fs[i],0,sizeof(fs[i]));
}
for(int i=1;i<=dfn[0];i++){
int I=dfn[i];
for(int j=0;j<m;j++)fp[i+1][j+1]=max(fp[i+1][j+1],fp[i][j]+a[I]);
for(int j=0;j<=m;j++)fp[i+sz[I]][j]=max(fp[i+sz[I]][j],fp[i][j]);
}
for(int i=dfn[0];i;i--){
int I=dfn[i];
for(int j=0;j<m;j++)fs[i][j]=max(fs[i][j],fs[i+1][j+1]+a[I]);
for(int j=0;j<=m;j++)fs[i][j]=max(fs[i][j],fs[i+sz[I]][j]);
}
for(int i=1;i<=dfn[0];i++){
int I=dfn[i];
for(int j=0;j<m;j++)ans[I]=max(ans[I],fp[i][j]+fs[i+1][m-j-1]);
}
vis[k]=1;
for(int i:e[k])
if (!vis[i])dfs(i);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 4508kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
result:
wrong answer Answer contains longer sequence [length = 5], but output contains 0 elements