QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#344547#8213. GraffitiSolitaryDream#WA 3ms12692kbC++173.7kb2024-03-04 19:08:412024-03-04 19:08:41

Judging History

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

  • [2024-03-04 19:08:41]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:12692kb
  • [2024-03-04 19:08:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long

const int N=3e5+1e3+7;

int n;

string s;

vector<int> g[N];

namespace TWO {
    int f[N][2];

    void dfs(int x,int F)
    {
        vector<int> val;
        int s=0;
        for(auto v:g[x])
        {
            if(v==F)
                continue;
            dfs(v,x);
            val.push_back(f[v][1]-f[v][0]);
            s+=f[v][0];
        }
        sort(val.begin(),val.end(),greater<int>());
        f[x][0]=s,f[x][1]=s+val.size();
        int k=0;
        for(auto w:val)
        {
            s+=w;
            k++;
            f[x][0]=max(f[x][0],s+k);
            f[x][1]=max(f[x][1],s+(int)val.size()-k);
        }
    }

    int solve()
    {
        dfs(1,0);
        return max(f[1][0],f[1][1]);
    }
}

namespace THREE {

    int f[N][2][2];

    int ty;

    //0: aba, 1: abb, 2: abc

    int calc(int base,int i,int j,int a,int b,int F)
    {
        if(ty==0)
        {
            if(i==0)
                return base;
            else
                return a*(a-1)+(j==0&&F)*a*2+base;
        }
        else if(ty==1)
        {
            if(i==0)
                return base;
            else
                return a*b+(j==1&&F)*a+(j==0&&F)*b+base;
        }
        else if(ty==2)
        {
            if(i==0)
                return base;
            else
            {
                int w=a+(j==0&&F);
                return w/2*(w-w/2)+base;
            }
        }
        else
            assert(0);
    }

    void dfs(int x,int F)
    {
        for(auto v:g[x])
        {
            if(v==F)
                continue;
            dfs(v,x);
        }
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
            {
                //f[x][i][j]
                vector<int> val;
                int s=0;
                for(auto v:g[x])
                {
                    if(v==F)
                        continue;
                    val.push_back(f[v][1][i]-f[v][0][i]);
                    s+=f[v][0][i];
                }
                f[x][i][j]=calc(s,i,j,val.size(),0,F);
                int k=0;
                for(auto w:val)
                {
                    s+=w;
                    k++;
                    f[x][i][j]=max(f[x][i][j],calc(s,i,j,val.size()-k,k,F));
                }
            }
    }

    int solve(int _ty)
    {
        ty=_ty;
        dfs(1,0);
        int ans=0;
        for(int p=0;p<2;p++)
            ans=max(ans,f[1][p][0]);
        return ans;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    cin>>s;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    if(s.size()==1)
    {
        cout<<n<<"\n";
        return 0;
    }
    if(s.size()==2)
    {
        if(s[0]==s[1])
            cout<<(n-1)*2<<"\n";
        else
            cout<<TWO::solve()<<"\n";
    }
    if(s.size()==3)
    {
        if(s[0]==s[1]&&s[1]==s[2])
        {
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(g[i].size())
                    ans+=1ll*g[i].size()*(g[i].size()-1);
            }
            cout<<ans<<"\n";
        }
        else
        {
            int ans;
            if(s[0]==s[2])
            {
                ans=THREE::solve(0);
            }
            else if(s[1]==s[2]||s[0]==s[1])
            {
                ans=THREE::solve(1);
            }
            else
            {
                ans=THREE::solve(2);
            }
            cout<<ans<<"\n";
        }
    }
}

详细

Test #1:

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

input:

1
a

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 3ms
memory: 10828kb

input:

3
orz
1 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 3ms
memory: 12692kb

input:

2
ab
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 3ms
memory: 12220kb

input:

5
bob
3 2
5 1
1 4
2 4

output:

4

result:

ok 1 number(s): "4"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 12332kb

input:

50
abc
23 14
24 25
1 3
47 46
2 26
22 41
34 19
7 14
50 24
29 38
17 25
4 26
35 37
21 14
11 4
13 27
8 25
5 10
20 27
44 27
15 39
19 9
30 12
38 27
39 27
41 40
14 48
32 7
16 37
3 13
42 5
48 27
49 25
6 5
26 9
31 17
36 7
43 29
9 5
45 9
18 9
40 42
27 5
25 42
46 10
37 42
12 48
28 26
33 5

output:

34

result:

wrong answer 1st numbers differ - expected: '37', found: '34'