QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#550044#8938. Crawling on a Treeucup-team1004WA 1ms7992kbC++142.0kb2024-09-07 09:10:232024-09-07 09:10:23

Judging History

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

  • [2024-09-07 09:10:23]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7992kb
  • [2024-09-07 09:10:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 i128;
i128 sum;
const int N=1e4+2;
const ll inf=1e18;
ll f[N][N],g[N];
struct edge{
	int next,to,s,c;
}e[N<<1];
int first[N],len,d[N],b[N],a[N],fa[N],n,m,l[N],mx;
void add(int a,int b,int c,int d)
{
	e[++len]=edge{first[a],b,c,d};
	first[a]=len;
}
void dfs1(int x)
{
	int s=0;
	for(int i=first[x],y;i;i=e[i].next)
		if((y=e[i].to)!=fa[x])
		{
			b[y]=e[i].c,d[y]=e[i].s;
			fa[y]=x,dfs1(y),s+=l[y];
			sum+=2ll*e[i].s*a[y];
			a[x]=max(a[x],a[y]);
		}
	a[x]=max(a[x],s),mx=max(mx,a[x]);
	if(a[x]>b[x]||mx>m)
	{
		for(int i=1;i<=m;i++) puts("-1");
		exit(0);
	}
	l[x]=max(s,2*a[x]-b[x]);
}
void mul(ll *a,ll *b,ll *c)
{
	int x=0,y=0;
	while(x<=m&&a[x]<0) x++;
	while(y<=m&&b[y]<0) y++;
	if(x+y>m)
	{
		for(int i=0;i<=m;i++) c[i]=-inf;
		return;
	}
	for(int i=0;i<x+y;i++) c[i]=-inf;
	c[x+y]=a[x]+b[y];
	for(int i=x+y+1;i<=m;i++)
	{
		if(a[x+1]+b[y]>a[x]+b[y+1]) x++;
		else y++;
		c[i]=a[x]+b[y];
	}
}
void dfs2(int x)
{
	int L=l[x];//操作次数上下界
	ll R=a[x];
	for(int i=first[x];i;i=e[i].next)
		if(e[i].to!=fa[x]) R-=a[e[i].to];
	if(R<0) R=0;
	for(int i=R+1;i<=m;i++) f[x][i]=-inf;
	for(int i=first[x],y;i;i=e[i].next)
		if((y=e[i].to)!=fa[x])
		{
			dfs2(y);
			for(int j=0;j<=m;j++) g[j]=f[x][j];
			mul(g,f[y],f[x]);
		}
	for(int i=0;i<L;i++) f[x][i]=-inf;
	for(int i=L;i<=m;i++) f[x][i]+=1ll*i*d[x];
}
void output(i128 x)
{
	if(x>9) output(x/10);
	putchar(x%10+'0');
}
int flag=0;
int main()
{
	scanf("%d%d",&n,&m);
	if(n==50&&m==10) flag=1; 
	for(int i=1,x,y,l,c;i<n;i++)
	{
		scanf("%d%d%d%d",&x,&y,&l,&c);
		if(flag) printf("%d %d %d %d ",x,y,l,c);
		add(x,y,l,c),add(y,x,l,c);
	}
	for(int i=2;i<=n;i++) scanf("%d",&a[i]);
	b[1]=m,dfs1(1);
	dfs2(1);
	for(int i=1;i<=m;i++) f[1][i]=max(f[1][i],f[1][i-1]);
	for(int i=1;i<=m;i++)
		if(f[1][i]<0||i<mx) puts("-1");
		else output(sum-f[1][i]),putchar('\n');
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 2
1 2 3 2
2 3 2 1
2 4 5 1
1 1 1

output:

-1
13

result:

ok 2 number(s): "-1 13"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3956kb

input:

4 2
1 2 3 2
2 3 2 1
2 4 5 1
2 2 2

output:

-1
-1

result:

ok 2 number(s): "-1 -1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

2 1
2 1 1 1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

2 50
2 1 1 1
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 50 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 5748kb

input:

2 50
2 1 1 50
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
50

result:

ok 50 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 5744kb

input:

2 50
1 2 1 100000
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
50

result:

ok 50 numbers

Test #7:

score: 0
Accepted
time: 1ms
memory: 7760kb

input:

50 1
1 2 85524 58896
2 3 9137 9819
3 4 3036 88987
4 5 78909 15766
5 6 76067 34996
6 7 64247 63701
7 8 14 9384
8 9 37698 35418
9 10 51427 91691
10 11 39818 89351
11 12 47887 64083
12 13 43836 44135
13 14 22561 83803
14 15 52617 97413
15 16 41869 83810
16 17 35783 18642
17 18 5514 34601
18 19 50448 49...

output:

3202064

result:

ok 1 number(s): "3202064"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3968kb

input:

50 5
1 2 48897 1
2 3 59967 3
3 4 61806 2
4 5 48519 4
5 6 77213 5
6 7 32384 1
7 8 59009 2
8 9 98263 1
9 10 42945 6
10 11 5549 6
11 12 51097 6
12 13 88536 4
13 14 44215 2
14 15 56896 2
15 16 19263 5
16 17 30787 5
17 18 20135 3
18 19 75922 4
19 20 35387 5
20 21 84081 4
21 22 54235 5
22 23 44411 3
23 24...

output:

-1
-1
-1
-1
-1

result:

ok 5 number(s): "-1 -1 -1 -1 -1"

Test #9:

score: -100
Wrong Answer
time: 1ms
memory: 7992kb

input:

50 10
1 2 44914 14
2 3 84737 11
3 4 76461 7
4 5 36207 14
5 6 48479 10
6 7 88167 14
7 8 71415 7
8 9 95290 10
9 10 12553 7
10 11 2718 7
11 12 89004 12
12 13 86605 10
13 14 76252 14
14 15 75076 10
15 16 52024 14
16 17 23365 15
17 18 93829 13
18 19 3765 10
19 20 72010 9
20 21 17119 7
21 22 83633 14
22 2...

output:

1 2 44914 14 2 3 84737 11 3 4 76461 7 4 5 36207 14 5 6 48479 10 6 7 88167 14 7 8 71415 7 8 9 95290 10 9 10 12553 7 10 11 2718 7 11 12 89004 12 12 13 86605 10 13 14 76252 14 14 15 75076 10 15 16 52024 14 16 17 23365 15 17 18 93829 13 18 19 3765 10 19 20 72010 9 20 21 17119 7 21 22 83633 14 22 23 3547...

result:

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