QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#563427#8518. Roars IIIqwqwf#WA 4ms21172kbC++141.1kb2024-09-14 11:44:382024-09-14 11:44:38

Judging History

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

  • [2024-09-14 11:44:38]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:21172kb
  • [2024-09-14 11:44:38]
  • 提交

answer

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
//#pragma GCC optimize("Ofast","unroll-loops","inline")
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define pii pair<int,int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int N=5e5+10,M=1e6+10,mod=998244353;
const int Inf=1e9;
int n,a[N],f[N],g[N];
char s[N];
vector<int> e[N];
void dfs(int u,int F){
	f[u]=g[u]=0;
	for(int v:e[u]) if(v!=F) dfs(v,u);
	if(a[u]){
		for(int v:e[u]) if(v!=F) g[u]=max(g[u],g[v]),f[u]+=f[v];
		g[u]++;
	}
	else{
		pii x=MP(-Inf,-Inf);
		for(int v:e[u]) if(v!=F){
			if(g[v]>x.fi) x.se=x.fi,x.fi=g[v];
			else if(g[v]>x.se) x.se=g[v];
			f[u]+=f[v];
		}
		g[u]=max(x.fi,x.se+1);
		f[u]+=x.fi;
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n>>(s+1);
	for(int i=1;i<=n;i++) a[i]=s[i]-'0';
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		e[u].pb(v),e[v].pb(u);
	}
	for(int i=1;i<=n;i++){
		dfs(i,0);
		cout<<f[i]<<' ';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 21172kb

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: -100
Wrong Answer
time: 0ms
memory: 20168kb

input:

1
0

output:

-1000000000 

result:

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