QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#609098 | #5363. ZYB 的游览计划 | Idtwtei | 0 | 0ms | 10268kb | C++14 | 1.7kb | 2024-10-04 10:38:44 | 2024-10-04 10:38:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
const int N=3e4+100,B=15,INF=1e9;
inline int chkmax(int &x,int y){ return y>x?x=y,1:0; }
#define rd read()
#define gc getchar()
inline int read(){
int x=0,f=0; char c=gc;
for(;c<'0'||c>'9';c=gc) f|=(c=='-');
for(;c>='0'&&c<='9';c=gc) x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
int n,m,a[N];
vector<int> G[N];
int dfn[N],d_c=0,st[N][B+5],dep[N];
inline int get(int x,int y){ return dfn[x]<dfn[y]?x:y; }
void dfs0(int u,int fu){
st[dfn[++d_c]=u][0]=fu,dep[u]=dep[fu]+1;
for(int v:G[u]) if(v!=fu) dfs0(v,u);
}
inline void init(){ for(int k=1;k<=B;++k) for(int i=1;i+(1<<k)-1<=n;++i) st[i][k]=get(st[i][k-1],st[i+(1<<(k-1))][k-1]); }
inline int flca(int x,int y){ if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y); int k=__lg(y-(++x)+1); return get(st[x][k],st[y+1-(1<<k)][k]); }
inline int fdis(int x,int y){ return dep[x]+dep[y]-2*dep[flca(x,y)]; }
int f[N][100],p[N][100];
int idx[N],i_c=0;
int sol(int l,int r){
i_c=0; for(int i=l;i<=r;++i) idx[++i_c]=a[i]; idx[++i_c]=1;
sort(idx+1,idx+i_c+1,[&](int x,int y){ return dfn[x]<dfn[y]; });
int res=fdis(idx[i_c],idx[1]);
for(int i=1;i<i_c;++i) res+=fdis(idx[i],idx[i+1]);
return res;
}
signed main(){
n=rd,m=rd;
for(int i=1;i<=n;++i) a[i]=rd;
for(int i=1,x,y;i<n;++i) x=rd,y=rd,G[x].pb(y),G[y].pb(x);
dfs0(1,0),init();
for(int i=0;i<=n;++i) for(int j=0;j<=m;++j) f[i][j]=-INF;
f[0][0]=0;
for(int i=1;i<=n;++i){
for(int j=0;j<=m;++j){
for(int k=0;k<i;++k){
if(chkmax(f[i][j+1],f[k][j]+sol(k+1,i))) p[i][j+1]=k;
}
}
}
cout<<f[n][m]<<"\n";
return 0;
}
详细
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 5
Accepted
time: 0ms
memory: 10268kb
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: 0
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
Time Limit Exceeded
Test #10:
score: 0
Time Limit Exceeded
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:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #29:
score: 0
Time Limit Exceeded
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%