QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759353#7513. Palindromic BeadsqlwpcWA 889ms227952kbC++145.0kb2024-11-18 02:32:102024-11-18 02:32:14

Judging History

你现在查看的是最新测评结果

  • [2024-11-18 02:32:14]
  • 评测
  • 测评结果:WA
  • 用时:889ms
  • 内存:227952kb
  • [2024-11-18 02:32:10]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
const int N = 200300;
const int mod = 998244353;
using ll = long long;
const int INF = 0x3f3f3f3f;
int read(){
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
const int M = 2*N;
struct node{
    int to,next;
}q[M];
int head[N],ss,sz[N],mxs[N],Rt,curN, curRt[N],cursub[N],dfn[N],out[N],tm;bool vis[N];
int Rtst[N],Rted[N],FA[N];
void addedge(int x,int y)
{
    q[++ss]=(node){y,head[x]};head[x]=ss;
    q[++ss]=(node){x,head[y]};head[y]=ss;
}
void dfs1(int x,int fa)
{
    sz[x]=mxs[x]=1;
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (t==fa||vis[t]) continue;
        dfs1(t,x);
        sz[x]+=sz[t];
        mxs[x]=std::max(mxs[x], sz[t]);
    }
    mxs[x]=std::max(mxs[x], curN-sz[x]);
    if (mxs[x]<mxs[Rt]) Rt=x;   
}
void dfs2(int x,int fa)
{
    dfn[x]=++tm;
    curRt[x]=Rt;
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (t==fa||vis[t]) continue;
        FA[t]=x;
        dfs2(t,x);
    }
    out[x]=tm;
}
void dfsSubRt(int x, int fa, int S, int T)
{
    if (x!=Rt) Rtst[x]=S,Rted[x]=T,cursub[x]=S;
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (t==fa||vis[t]) continue;
        dfsSubRt(t,x,x==Rt?dfn[t]:S, x==Rt?out[t]:T);
    }
}
const int SegN=N*80;
int SegRt[N],cc,ls[SegN],rs[SegN],tag[SegN],tre[SegN];
void col(int &i, int x, int F)
{
    i=++cc;
    tre[i]=tre[F];ls[i]=ls[F];rs[i]=rs[F];tag[i]=tag[F];;
    tre[i]=std::max(tre[i],x);
    tag[i]=std::max(tag[i],x);
}
void pushdown(int i)
{
    if (tag[i])
    {
        col(ls[i], tag[i], ls[i]);
        col(rs[i], tag[i], rs[i]);
        tag[i]=0;
    }
}
void insert(int &i,int l,int r,int F,int L,int R,int x)
{
    i=++cc;
    tre[i]=tre[F];ls[i]=ls[F];rs[i]=rs[F];tag[i]=tag[F];
    if (L<=l&&r<=R) {col(i,x,i);return;}
    pushdown(i);pushdown(F);
    int mid=(l+r)>>1;
    if (L<=mid) insert(ls[i],l,mid,ls[F],L,R,x);
    if (mid<R) insert(rs[i],mid+1,r,rs[F],L,R,x);
    tre[i]=std::max(tre[ls[i]],tre[rs[i]]);
}
int query(int i,int l,int r,int x)
{
    if (!i) return 0;
    if (l==r) return tre[i];
    pushdown(i);
    int mid=(l+r)>>1;
    if (x<=mid) return query(ls[i],l,mid,x);
    else return query(rs[i],mid+1,r,x);
}
int ans=1;int c[N],ton[N],cp[N];
void dfs3(int x,int fa)
{
    int u=cp[x];
    int L1=0,R1=0;
    int L2=0,R2=0;
    SegRt[x]=SegRt[fa];
    if (u && curRt[u]==Rt && (dfn[u]<dfn[x] || out[x]<dfn[u]))
    {
        int curlen = query(SegRt[x],1,curN,dfn[u]) + 2;
        if (FA[x]!=u) curlen = std::max(curlen, 3);
        //if (cursub[x]!=cursub[u]) 
        ans=std::max(ans, curlen);
        //printf("query x=%d at u=%d dfn=%d = %d\n",x,u,dfn[u],curlen);
        if (dfn[u]<=dfn[x] && out[u]>=out[x])
        {
            if (u==Rt) L1=2, R1=Rtst[x]-1, L2=Rted[x]+1, R2=out[Rt];
            else 
            {
                int t=0;
                for (int j=head[u];j;j=q[j].next)
                {
                    t=q[j].to;
                    if (t!=fa&& dfn[t]<=dfn[x]&&out[t]>=out[x])
                        break;
                }
                L1=1, R1=dfn[t]-1, L2=out[t]+1, R2=out[Rt];
            }
            //printf("x=%d u=%d add L1 = [%d,%d] L2=[%d,%d] len=%d\n",x,u,L1,R1,L2,R2,curlen);
            if (L1<=R1) insert(SegRt[x], 1, out[Rt], SegRt[x], L1, R1, curlen);
            if (L2<=R2) insert(SegRt[x], 1, out[Rt], SegRt[x], L2, R2, curlen);
        }else// if (cursub[x]!=cursub[u])
        {
            L1=dfn[u];R1=out[u];
            //printf("x=%d u=%d add L1 = [%d,%d] len=%d\n",x,u,L1,R1,curlen);
            insert(SegRt[x], 1, out[Rt], SegRt[x], L1, R1, curlen);
        }
    }
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (t==fa||vis[t]) continue;
        dfs3(t, x);
    }
}
void solve(int x)
{//printf("Rt=%d curN=%d\n",x,curN);
    vis[x]=true;
    FA[x]=0;
    cc=tm=0;
    cursub[x]=0;
    dfs2(x,0);
    dfsSubRt(x,0,0,0);
    SegRt[x]=0;
    dfs3(x, 0);
    //return;
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (vis[t]) continue;
        Rt=0;curN=sz[t];
        dfs1(t, x);
        solve(Rt);
    }
}
int main()
{
    // freopen("B.in","r",stdin);
    // freopen("B.out","w",stdout);
    int n=read();
    curN=n;mxs[0]=INF;
    for (int i=1;i<=n;++i) c[i]=read();
    for (int i=1;i<=n;++i)
    {
        cp[i] = ton[c[i]];
        ton[c[i]]=i;
    }
    for (int i=1;i<=n;++i)
        if (!cp[i]&&ton[c[i]]!=i) cp[i]=ton[c[i]];
    for (int i=1;i<n;++i) addedge(read(),read());
    // Rt=1;
    // dfs2(1,0);
    // dfsSubRt(1,0,0,0);
    // dfs3(1,0);
    dfs1(1,0);
    dfs1(Rt,0);
    solve(Rt);
    printf("%d",ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11996kb

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: 1ms
memory: 11884kb

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: 1ms
memory: 9984kb

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: 2ms
memory: 16076kb

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: 12320kb

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: 422ms
memory: 84300kb

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: 457ms
memory: 86028kb

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: 564ms
memory: 121124kb

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: 623ms
memory: 152576kb

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: 889ms
memory: 227952kb

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: 0
Accepted
time: 865ms
memory: 190988kb

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:

839

result:

ok single line: '839'

Test #12:

score: 0
Accepted
time: 421ms
memory: 94524kb

input:

200000
96438 84772 88103 81186 60908 116093 94738 28602 35022 91108 60687 75572 100094 50553 58445 117024 96154 53539 23185 112280 90369 32413 95244 11077 91008 109781 6404 2285 3544 111712 49414 10399 113626 81435 11321 52557 17023 113260 14225 66464 61352 98403 36521 110038 57172 42868 68512 69031...

output:

31

result:

ok single line: '31'

Test #13:

score: -100
Wrong Answer
time: 525ms
memory: 124852kb

input:

200000
50750 86282 92049 114579 8296 28675 45880 47381 71400 43379 111535 32316 37104 35968 100241 6914 81284 48969 62890 63486 107557 80178 76322 31515 24682 85646 12681 106054 5167 50339 39004 16152 112081 10605 66750 51623 96332 77287 75452 50609 1549 1652 45229 73171 10015 66323 90164 97491 1007...

output:

8082

result:

wrong answer 1st lines differ - expected: '8108', found: '8082'