UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#188365#3317. 行走(walk)gaojieming1002837ms21432kbC++112.5kb2023-10-03 08:55:102023-10-03 12:48:27

answer

#include<bits/stdc++.h>
#define ll long long
#define il inline
#define pn putchar('\n')
#define maxint 2147483647
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define maxn 200005
#define mod 998244353
#define int ll
using namespace std;
struct Gragh
{
    int v,nex;
}e[maxn<<1];
int n,top,root,cnt,ans;
string S;
bool vis[maxn];
int c[maxn],maxx[maxn],ro[maxn<<1],si[maxn],a[maxn],f[10],g[10];
il void upd(int &x,int y)
{
    x+=y;
    if(x>=mod)x-=mod;
    if(x<0)x+=mod;
}
il void build_gra(int u,int v)
{
    e[++top]=(Gragh){v,c[u]},c[u]=top;
}
il void gs(int pos,int fa=-1)
{
    si[pos]=1;
    for(int i=c[pos];i;i=e[i].nex)
    {
        int v=e[i].v;
        if(vis[v]||v==fa)continue;
        gs(v,pos);
        si[pos]+=si[v];
    }
}
il void ga(int pos,int fa,int sum)
{
    maxx[pos]=0;
    for(int i=c[pos];i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==fa||vis[v])continue;
        maxx[pos]=max(maxx[pos],si[v]);
        ga(v,pos,sum);
    }
    maxx[pos]=max(maxx[pos],sum-si[pos]);
    if(maxx[pos]<maxx[root])
        root=pos;
}
il void gr(int pos)
{
    gs(pos);
    vis[pos]=1;
    for(int i=c[pos];i;i=e[i].nex)
    {
        int v=e[i].v;
        if(vis[v])continue;
        root=0;
        ga(v,pos,si[v]);
        ro[i]=root;
        gr(root);
    }
}
il void gd(int pos,int fa,int ss)
{
    ss|=1<<a[pos];
    g[ss]++;
    if(ss==7)upd(ans,1);
    for(int i=c[pos];i;i=e[i].nex)
    {
        int v=e[i].v;
        if(vis[v]||v==fa)continue;
        gd(v,pos,ss);
    }
}
il void dfs(int pos)
{
    vis[pos]=1;
    for(int i=c[pos];i;i=e[i].nex)
    {
        int v=e[i].v;
        if(vis[v])continue;
        cnt=0;
        gd(v,pos,1<<a[pos]);
        for(int j=1;j<=7;j++)
            for(int k=1;k<=7;k++)
                if((j|k)==7)
                    upd(ans,f[j]*g[k]%mod);
        for(int j=1;j<=7;j++)
            upd(f[j],g[j]),g[j]=0;
    }
    for(int i=1;i<=7;i++)
        f[i]=0;
    for(int i=c[pos];i;i=e[i].nex)
        if(ro[i])
            dfs(ro[i]);
}
signed main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    scanf("%lld",&n);
    cin>>S;
    for(int i=0;i<n;i++)
        a[i+1]=S[i]-'a';
    build_gra(0,1);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%lld%lld",&u,&v);
        build_gra(u,v),build_gra(v,u);
    }
    maxx[0]=maxint;
    gr(0);
    root=ro[1];
    memset(vis,0,sizeof vis);
    dfs(root);
    printf("%lld",ans);
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1680kb

input:

2000
ababbcaaacccaabbaaacaabbbbcbcaaaacaaaaacaacbaaaaaaacbcaaacabbcaaacbbcbaabacbacbacbcbaacaacbbcab...

output:

1984050

result:

ok single line: '1984050'

Test #2:

score: 10
Accepted
time: 3ms
memory: 1684kb

input:

2000
accbccbcaacbabbbcbcccbacbccabbccccbccccaccccccbcaccabbcbbbcbccaaccbcccacabbaacbbcabccbaaccbabbb...

output:

1982714

result:

ok single line: '1982714'

Test #3:

score: 10
Accepted
time: 3ms
memory: 1680kb

input:

2000
cccacccbabaacaaacccbbbabcabacabbcbcabacbbaaccaabbcabbbaabbaaababbbabcbcabbccbabacbbcbbbbbbaccbb...

output:

1983256

result:

ok single line: '1983256'

Test #4:

score: 10
Accepted
time: 183ms
memory: 21428kb

input:

200000
aabaaccabcbbabacbcacbaaaccbbbacacacacabbbabcbaabbabcbbbcbbbacababababcaccabcaacbccbaabbbcccbb...

output:

34314841

result:

ok single line: '34314841'

Test #5:

score: 10
Accepted
time: 162ms
memory: 21432kb

input:

200000
bcaaabccbccbaaacabccaaacabcccbccabacbbbbccbacaacccacbaabcabccbcabcbaabaabababbbbcbacacbbaabba...

output:

34315707

result:

ok single line: '34315707'

Test #6:

score: 10
Accepted
time: 167ms
memory: 21432kb

input:

200000
babcaaccaabccbbccbbabcbaccbababbcaacbaccccccabacacbcccabbccbcccacaaabccbbbabbabbcccbaaabcaaaa...

output:

34309683

result:

ok single line: '34309683'

Test #7:

score: 10
Accepted
time: 686ms
memory: 17276kb

input:

200000
abbaaaabbabaabaaabbbaababbabaabaabababaabaababbaaaaababbabaabbabbbbbbbbbababbbaabaaabaabbbbab...

output:

199998

result:

ok single line: '199998'

Test #8:

score: 10
Accepted
time: 581ms
memory: 17280kb

input:

200000
babababbbbbbababaaabaaabaaababbbbabbaaaabaaaabbbbbbaaaaaabbabbabaabaabbaabbbbabbbaabbaababaab...

output:

799971

result:

ok single line: '799971'

Test #9:

score: 10
Accepted
time: 506ms
memory: 17280kb

input:

200000
aabccccabaabcbabcbaabbaccaaaccbcbbbbaccabbcaabbaababbbbacacbcccabcbbbccababccbccbccaaaabccacb...

output:

33543853

result:

ok single line: '33543853'

Test #10:

score: 10
Accepted
time: 546ms
memory: 17280kb

input:

200000
abaabbabacabaabaccbbabcbabbaababbbccabbaacacbcbbcbcacccabbbaccaaccaaaccbabbaabbcbcbaaaaabbbcb...

output:

33539915

result:

ok single line: '33539915'

Extra Test:

score: 0
Extra Test Passed