QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#396236 | #5418. Color the Tree | 2745518585 | WA | 36ms | 19816kb | C++20 | 2.7kb | 2024-04-22 15:43:31 | 2024-04-22 15:43:32 |
Judging History
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<=n;++i) s+=f[1][i];
printf("%lld\n",s);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14144kb
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: 36ms
memory: 19816kb
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:
81604379575 128849019932 146028889365 73014444585 146028888968 180388627936 73014444690 68719477023 38654705819 73014444260 111669150255 38654705877 111669150531 137438954189 107374183045 193273529355 120259085011 73014444580 146028889106 188978562082 68719477050 77309412110 73014444397 128849020445...
result:
wrong answer 1st numbers differ - expected: '180', found: '81604379575'