QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#116715 | #5363. ZYB 的游览计划 | 1kri | 0 | 58ms | 14808kb | C++14 | 2.0kb | 2023-06-29 21:14:08 | 2023-06-29 21:14:11 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int n,m,p[200005];
int u[400005],v[400005],first[200005],nxt[400005];
int f[2005][2005];
int fa[200005],depth[200005],sz[200005],son[200005];
int top[200005],idx,dfn[200005],nfd[200005];
void dfs1(int now,int f,int d){
fa[now]=f,depth[now]=d;
sz[now]=1;
for (int i=first[now];i;i=nxt[i])
if (v[i]!=f){
dfs1(v[i],now,d+1);
sz[now]+=sz[v[i]];
if (son[now]==0||sz[v[i]]>sz[son[now]])son[now]=v[i];
}
return;
}
void dfs2(int now,int f,int t){
top[now]=t;
++idx;
dfn[now]=idx,nfd[idx]=now;
if (son[now]!=0)dfs2(son[now],now,t);
for (int i=first[now];i;i=nxt[i])
if (v[i]!=f&&v[i]!=son[now])dfs2(v[i],now,v[i]);
return;
}
int lca(int a,int b){
if (a==0||b==0)return 0;
while(top[a]!=top[b]){
if (depth[top[a]]<depth[top[b]])swap(a,b);
a=fa[top[a]];
}
if (depth[a]>depth[b])return b;
return a;
}
int dis(int a,int b){
return depth[a]+depth[b]-2*depth[lca(a,b)];
}
namespace DS{
set<int> c;
int sum;
void init(){
c.clear();
c.insert(0),c.insert(n+1);
sum=0;
return;
}
void ins(int x){
int l,r;
set<int>::iterator it=c.lower_bound(x);
r=(*it);
it--;
l=(*it);
sum-=dis(nfd[l],nfd[r]);
sum+=dis(nfd[l],x);
sum+=dis(x,nfd[r]);
c.insert(dfn[x]);
return;
}
void del(int x){
c.erase(x);
int l,r;
set<int>::iterator it=c.lower_bound(x);
r=(*it);
it--;
l=(*it);
sum+=dis(nfd[l],nfd[r]);
sum-=dis(nfd[l],x);
sum-=dis(x,nfd[r]);
return;
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)scanf("%d",&p[i]);
for (int i=1;i<n;i++){
scanf("%d%d",&u[i],&v[i]);
nxt[i]=first[u[i]],first[u[i]]=i;
u[i+n]=v[i],v[i+n]=u[i];
nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n;
}
dfs1(1,0,0);
dfs2(1,0,1);
for (int i=1;i<=n;i++){
DS::init();
for (int j=i;j>=1;j--){
DS::ins(p[j]);
for (int k=1;k<=m;k++)
f[i][k]=max(f[i][k],f[j-1][k-1]+DS::sum);
}
}
cout<<f[n][m]<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 5
Accepted
time: 1ms
memory: 9576kb
input:
5 2 2 4 3 5 1 1 2 2 3 3 4 4 5
output:
14
result:
ok single line: '14'
Test #2:
score: -5
Runtime Error
input:
90752 2 88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...
output:
result:
Subtask #2:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 58ms
memory: 14808kb
input:
760 217 632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...
output:
71240
result:
wrong answer 1st lines differ - expected: '35938', found: '71240'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Runtime Error
Test #29:
score: 0
Runtime Error
input:
14878 6 1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%