QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759274#7513. Palindromic BeadsqlwpcWA 4ms16216kbC++144.9kb2024-11-17 23:49:072024-11-17 23:49:07

Judging History

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

  • [2024-11-17 23:49:07]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:16216kb
  • [2024-11-17 23:49:07]
  • 提交

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;
    //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,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)
{
    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 print(int i,int l,int r)
{
    if (!i) return;
    //printf("node %d [%d,%d] : tre=%d tag=%d\n",i,l,r,tre[i],tag[i]);
    if (l==r) return;
    int mid=(l+r)>>1;
    print(ls[i],l,mid);
    print(rs[i],mid+1,r);
}
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)
{//printf("insert %d [%d,%d] at node=%d,[%d,%d]\n",x,L,R,i,l,r);
    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;
    //printf("FA %d = %d\n",x,FA[x]);
    SegRt[x]=SegRt[fa];
    if (u && curRt[u]==Rt && (dfn[u]<dfn[x] || out[x]<out[u]))
    {
        //print(SegRt[x],1,curN);
        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 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] 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];
            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());
    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: 10068kb

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

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

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

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: 4ms
memory: 12176kb

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:

7

result:

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