QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#396240#5418. Color the Tree2745518585WA 26ms19772kbC++202.7kb2024-04-22 15:46:402024-04-22 15:46:40

Judging History

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

  • [2024-04-22 15:46:40]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:19772kb
  • [2024-04-22 15:46:40]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1000001,M=31;
int n,b[N],*g[N];
ll *f[N];
vector<int> a[N];
namespace ST
{
    int a[N][M],lg[N];
    void init()
    {
        for(int i=1;i<=n;++i) a[i][0]=b[i];
        for(int i=1;i<=20;++i)
        {
            for(int j=(1<<i);j<=min((1<<(i+1))-1,n);++j) lg[j]=i;
        }
        for(int i=1;i<=20;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(j+(1<<i)-1<=n) a[j][i]=min(a[j][i-1],a[j+(1<<(i-1))][i-1]);
            }
        }
    }
    int sum(int x,int y)
    {
        return min(a[x][lg[y-x]],a[y-(1<<lg[y-x])+1][lg[y-x]]);
    }
}
struct tree
{
    int md,z;
}T[N];
void dfs0(int x,int fa)
{
    T[x].md=1;
    T[x].z=0;
    for(auto i:a[x])
    {
        if(i==fa) continue;
        dfs0(i,x);
        T[x].md=max(T[x].md,T[i].md+1);
        if(T[i].md>T[T[x].z].md) T[x].z=i;
    }
}
void dfs(int x,int fa)
{
    if(T[x].z)
    {
        f[T[x].z]=f[x]+1;
        g[T[x].z]=g[x]+1;
        dfs(T[x].z,x);
    }
    for(auto i:a[x])
    {
        if(i==fa||i==T[x].z) continue;
        f[i]=new ll[T[i].md+2];
        g[i]=new int[T[i].md+2];
        for(int j=0;j<=T[i].md+1;++j) f[i][j]=g[i][j]=0;
        dfs(i,x);
        for(int j=0;j<=T[i].md;++j)
        {
            if(g[x][j]<j) f[i][j]=min(f[i][j],(ll)ST::sum(g[i][j]+1,j)),g[i][j]=j;
        }
        for(int j=1;j<=T[i].md+1;++j)
        {
            if(g[x][j]<j) f[x][j]=min(f[x][j],(ll)ST::sum(g[x][j]+1,j)),g[x][j]=j;
            f[x][j]=min(f[x][j]+f[i][j-1],(ll)b[j]);
        }
    }
    f[x][0]=b[0];
    g[x][0]=0;
    // printf("%d:\n",x);
    // for(int i=0;i<=T[x].md;++i) printf("%lld ",f[x][i]);printf("\n");
    // for(int i=0;i<=T[x].md;++i) printf("%d ",g[x][i]);printf("\n");
}
int main()
{
    int TOT;
    scanf("%d",&TOT);
    while(TOT--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i) a[i].clear();
        for(int i=0;i<=n-1;++i)
        {
            scanf("%d",&b[i]);
        }
        for(int i=1;i<=n-1;++i)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x].push_back(y);
            a[y].push_back(x);
        }
        ST::init();
        dfs0(1,0);
        f[1]=new ll[T[1].md+2];
        g[1]=new int[T[1].md+2];
        for(int i=0;i<=T[1].md+1;++i) f[1][i]=g[1][i]=0;
        dfs(1,0);
        for(int i=1;i<=T[1].md+1;++i)
        {
            if(g[1][i]<i) f[1][i]=min(f[1][i],(ll)ST::sum(g[1][i]+1,i)),g[1][i]=i;
        }
        ll s=0;
        for(int i=0;i<=T[1].md;++i) s+=f[1][i];
        printf("%lld\n",s);
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 14212kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Wrong Answer
time: 26ms
memory: 19772kb

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:

180
174
222
230
156
240
225
126
100
81
155
73
154
127
149
146
228
230
132
187
153
170
78
282
195
286
191
211
119
197
211
233
88
252
239
233
175
180
195
138
109
148
180
175
226
210
182
109
199
59
56
31
115
220
203
172
149
208
53
158
189
170
173
137
233
94
163
273
80
350
156
133
146
159
252
269
137
22...

result:

wrong answer 2nd numbers differ - expected: '168', found: '174'