QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#277886 | #7513. Palindromic Beads | Yu_Jie | WA | 798ms | 250860kb | C++14 | 3.1kb | 2023-12-07 08:06:05 | 2023-12-07 08:06:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=200005,H=18,M=36100005;
int n,a[N],c[N],d[N],fa[N][H],dfn[N],dfm[N],idx;
vector<int> e[N];
struct node {
int x,y,z,d;
bool operator <(const node &o) const {return d>o.d;}
} b[N];
struct sgt {
int idx,rt[4*N];
struct node {int lc,rc,mx;} tr[M];
void modify2(int x,int v,int &p,int l=1,int r=n) {
if(!p) p=++idx;
tr[p].mx=max(tr[p].mx,v);
if(l==r) return ;
int mid=l+r>>1;
if(x<=mid) modify2(x,v,tr[p].lc,l,mid);
else modify2(x,v,tr[p].rc,mid+1,r);
}
void modify(int x,int y,int v,int p=1,int l=1,int r=n) {
modify2(y,v,rt[p]);
if(l==r) return ;
int mid=l+r>>1;
if(x<=mid) modify(x,y,v,p<<1,l,mid);
else modify(x,y,v,p<<1|1,mid+1,r);
}
int query2(int L,int R,int p,int l=1,int r=n) {
if(!p) return 0;
if(L<=l&&r<=R) return tr[p].mx;
int mid=l+r>>1,res=0;
if(L<=mid) res=query2(L,R,tr[p].lc,l,mid);
if(R>mid) res=max(res,query2(L,R,tr[p].rc,mid+1,r));
return res;
}
int query(int L,int R,int L2,int R2,int p=1,int l=1,int r=n) {
if(L<=l&&r<=R) return query2(L2,R2,rt[p]);
int mid=l+r>>1,res=0;
if(L<=mid) res=query(L,R,L2,R2,p<<1,l,mid);
if(R>mid) res=max(res,query(L,R,L2,R2,p<<1|1,mid+1,r));
return res;
}
} tr;
int read() {
int x=0,f=1; char c=getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-48;
return x*f;
}
void dfs(int x,int Fa) {
dfn[x]=++idx; d[x]=d[Fa]+1; fa[x][0]=Fa;
for(int i=1;1<<i<d[x];i++) fa[x][i]=fa[fa[x][i-1]][i-1];
for(int y:e[x]) if(y!=Fa) dfs(y,x);
dfm[x]=idx;
}
int lca(int x,int y) {
if(d[x]<d[y]) swap(x,y);
if(d[x]>d[y]) for(int c=d[x]-d[y],i=__lg(c);~i;i--) if(c>>i&1) x=fa[x][i];
if(x==y) return x;
for(int i=__lg(d[x]);~i;i--) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int main() {
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<n;i++) {
int x=read(),y=read();
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1,0);
for(int i=1;i<=n;i++) (b[a[i]].x?b[a[i]].y:b[a[i]].x)=i;
for(int i=1;i<=n;i++) if(b[i].x&&b[i].y) {
b[i].z=lca(b[i].x,b[i].y);
b[i].d=d[b[i].x]+d[b[i].y]-2*d[b[i].z];
}
sort(b+1,b+n+1);
int ans=1;
for(int i=1;i<=n&&b[i].d;i++) {
int x=b[i].x,y=b[i].y,z,res=0;
if(dfn[x]>dfn[y]) swap(x,y);
if(b[i].z!=x) res=tr.query(dfn[x],dfm[x],dfn[y],dfm[y])+2;
else {
for(int j:e[x]) if(dfn[j]<=dfn[y]&&dfn[y]<=dfm[j]) {z=j; break;}
if(dfn[z]>1) res=max({res,tr.query(dfn[y],dfm[y],1,dfn[z]-1)+2,tr.query(1,dfn[z]-1,dfn[y],dfm[y])+2});
if(dfm[z]<n) res=max({res,tr.query(dfn[y],dfm[y],dfm[z]+1,n)+2,tr.query(dfm[z]+1,n,dfn[y],dfm[y])+2});
}
tr.modify(dfn[x],dfn[y],res);
ans=max(ans,res+(b[i].d>1));
}
printf("%d\n",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 8372kb
input:
4 1 1 2 2 1 2 2 3 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 3ms
memory: 8308kb
input:
5 1 3 2 2 1 1 2 2 3 3 4 4 5
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 8260kb
input:
6 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 8308kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 9168kb
input:
2000 845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...
output:
5
result:
ok single line: '5'
Test #6:
score: 0
Accepted
time: 427ms
memory: 241804kb
input:
200000 48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...
output:
5
result:
ok single line: '5'
Test #7:
score: 0
Accepted
time: 422ms
memory: 241852kb
input:
200000 13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 447ms
memory: 242384kb
input:
200000 38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...
output:
45
result:
ok single line: '45'
Test #9:
score: 0
Accepted
time: 499ms
memory: 243420kb
input:
200000 36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...
output:
135
result:
ok single line: '135'
Test #10:
score: 0
Accepted
time: 650ms
memory: 246668kb
input:
200000 13528 65006 30352 8565 36687 6748 5507 44320 7189 17847 46996 82728 102722 4727 36914 74228 21460 87970 11733 47170 67282 104558 66436 64504 57055 88619 42995 25569 101298 90984 76491 51994 62257 103424 8221 69668 99170 6808 29043 73058 5277 26614 23654 25152 64939 38418 78518 5330 37531 4305...
output:
425
result:
ok single line: '425'
Test #11:
score: -100
Wrong Answer
time: 798ms
memory: 250860kb
input:
200000 92279 5566 62695 50240 45387 51097 57743 94873 53220 29260 72584 38043 86335 33441 12946 30267 12932 18258 4560 8896 64393 39608 53183 34285 36518 18501 51940 8658 101018 48522 21336 104735 25785 73132 33489 81905 94563 18128 87872 24765 54563 57218 61869 50458 75919 63764 48155 4489 35212 44...
output:
835
result:
wrong answer 1st lines differ - expected: '839', found: '835'