QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#758699#7513. Palindromic BeadsqlwpcWA 3ms14160kbC++144.3kb2024-11-17 19:23:152024-11-17 19:23:16

Judging History

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

  • [2024-11-17 19:23:16]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14160kb
  • [2024-11-17 19:23:15]
  • 提交

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],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;
    //printf("dfn %d = [%d,%d]\n",x,dfn[x],out[x]);
}
void dfsSubRt(int x, int fa, int S, int T)
{
    if (x!=Rt) Rtst[x]=S,Rted[x]=T;
    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)
{
    if (!i) {
        i=++cc;
        tre[i]=tre[F];ls[i]=rs[i]=tag[i]=0;
    }
    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], i);
        col(rs[i], tag[i], 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);
    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;
    //printf("FA %d = %d\n",x,FA[x]);
    if (u && curRt[u]==Rt && (dfn[u]<dfn[x] || out[x]<out[u]))
    {
        int curlen = query(SegRt[fa],1,curN,dfn[u]) + 2;
        if (FA[x]!=u) curlen = std::max(curlen, 3);
        ans=std::max(ans, curlen);
        //printf("query %d = %d\n",x,curlen);
        if (out[u]>=dfn[x])
        {
            if (u==Rt) L1=2, R1=Rtst[x]-1, L2=Rted[x]+1, R2=out[Rt];
            else L1=1, R1=dfn[u]-1, L2=out[u]+1, R2=out[Rt];
            //printf("x=%d u=%d add L1 = [%d,%d] L2=[%d,%d]\n",x,u,L1,R1,L2,R2);
            if (L1<=R1) insert(SegRt[x], 1, out[Rt], SegRt[fa], L1, R1, curlen);
            if (L2<=R2) insert(SegRt[x], 1, out[Rt], SegRt[x] , L2, R2, curlen);
        }else
        {
            L1=dfn[u];R1=out[u];
            insert(SegRt[x], 1, out[Rt], SegRt[fa], L1, R1, curlen);
        }
    }else SegRt[x]=SegRt[fa];
    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\n",x);
    vis[x]=true;
    FA[x]=0;
    cc=tm=0;
    dfs2(x,0);
    dfsSubRt(x,0,0,0);
    SegRt[x]=0;
    dfs3(x, 0);
    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()
{
    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());
    dfs1(1,0);
    solve(Rt);
    printf("%d",ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12172kb

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: 0ms
memory: 11968kb

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

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

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: -100
Wrong Answer
time: 3ms
memory: 12372kb

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:

123

result:

wrong answer 1st lines differ - expected: '5', found: '123'