QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#444158#8518. Roars IIIucup-team1004#WA 1ms8064kbC++141.2kb2024-06-15 17:34:092024-06-15 17:34:11

Judging History

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

  • [2024-06-15 17:34:11]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8064kb
  • [2024-06-15 17:34:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
struct edge{
	int next,to;
}e[N<<1];
int first[N],len,siz[N],fa[N];
pair<int,int> mx[N];
long long f[N],ans[N];
char str[N];
void dfs1(int x)
{
	siz[x]=(str[x]=='1');
	for(int i=first[x],y;i;i=e[i].next)
		if((y=e[i].to)!=fa[x])
		{
			fa[y]=x,dfs1(y);
			siz[x]+=siz[y],f[x]+=f[y];
			mx[x]=max(mx[x],{siz[y],y});
		}
	if(str[x]=='0') f[x]+=mx[x].first;
}
int n;
void add(int a,int b)
{
	e[++len]=edge{first[a],b};
	first[a]=len;
}
int gmax(int x,int ban)
{
	int s=0;
	for(int i=first[x],y;i;i=e[i].next)
		if((y=e[i].to)!=fa[x]&&y!=ban) s=max(s,siz[y]);
	return s;
}
void dfs2(int x,ll s)
{
	for(int i=first[x];i;i=e[i].next)
		if(e[i].to!=fa[x]) s+=f[e[i].to];
	int res=siz[1]-siz[x],p=(str[x]=='0');
	ans[x]=s+p*max(mx[x].first,res);
	for(int i=first[x],y;i;i=e[i].next)
		if((y=e[i].to)!=fa[x])
		{
			if(y!=mx[x].second) dfs2(y,s-f[y]+p*max(mx[x].first,res));
			else dfs2(y,s-f[y]+p*max(gmax(x,y),res));
		}
}
int main()
{
	scanf("%d%s",&n,str+1);
	for(int i=1,x,y;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dfs1(1);
	dfs2(1,0);
	for(int i=1;i<=n;i++) printf("%lld%c",ans[i]," \n"[i==n]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5840kb

input:

5
10101
1 2
2 3
2 4
4 5

output:

2 2 2 3 3

result:

ok 5 number(s): "2 2 2 3 3"

Test #2:

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

input:

1
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1

result:

ok 2 number(s): "0 1"

Test #5:

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

input:

3
100
2 3
2 1

output:

0 1 2

result:

ok 3 number(s): "0 1 2"

Test #6:

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

input:

4
1100
4 1
4 3
4 2

output:

1 1 3 1

result:

ok 4 number(s): "1 1 3 1"

Test #7:

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

input:

5
10100
4 5
1 3
2 1
3 5

output:

0 2 0 4 2

result:

ok 5 number(s): "0 2 0 4 2"

Test #8:

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

input:

8
11001000
4 2
7 2
5 7
6 2
3 1
6 3
7 8

output:

5 3 5 6 4 4 4 7

result:

wrong answer 4th numbers differ - expected: '5', found: '6'