QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#442556#8518. Roars IIIucup-team2279#WA 4ms18220kbC++201.5kb2024-06-15 12:43:242024-06-15 12:43:25

Judging History

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

  • [2024-06-15 12:43:25]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:18220kb
  • [2024-06-15 12:43:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mk make_pair
#define lowbit(x) (x&(-x))
#define pb emplace_back
#define pr pair<int,int>
#define let const auto
#define all(A) A.begin(),A.end()
void chkmin(int &x,int y){x=min(x,y);}
void chkmax(int &x,int y){x=max(x,y);}
const int N=2e5+5;
int read(){
	int x=0,f=1; char c=getchar();
	while(('0'>c||c>'9')&&c!='-') c=getchar();
	if(c=='-') f=0,c=getchar();
	while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return f?x:-x;
}
int n,siz[N],a[N],dep[N];
multiset <int> s[N];
vector <int> adj[N];
ll ans[N],f[N];
void merge(multiset <int> &s,multiset <int> &t){
	if(s.size()<t.size()) swap(s,t);
	for(auto v:t) s.insert(v);
}
int ban[N],rt,sum,Max[N];
void calcsiz(int u,int fa){
	siz[u]=1;
	for(int v:adj[u]) if(v!=fa&&!ban[v]){
		calcsiz(v,u);
		siz[u]+=siz[v];
		chkmax(Max[u],siz[v]);
	}
	Max[u]=max(Max[u],sum-siz[u]);
	if(Max[u]<Max[rt]) rt=u;
}
void dp(int u,int fa){
	f[u]=0;
	for(int v:adj[u]) if(v!=fa){
		dep[v]=dep[u]+1;
		dp(v,u);
		merge(s[u],s[v]);
		s[v].clear();
		f[u]+=f[v];
	}
	if(a[u]) s[u].insert(dep[u]);
	else if(!s[u].empty()){
		int x=*--s[u].end();
		s[u].erase(--s[u].end());
		s[u].insert(dep[u]);
		f[u]+=x-dep[u];
	}
}
int main(){
	n=read();
	for(int i=1; i<=n; i++) scanf("%1d",&a[i]);
	for(int i=1; i<n; i++){
		int u=read(),v=read();
		adj[u].pb(v),adj[v].pb(u);
	}
	sum=n;
	for(int i=1; i<=n; i++) dp(i,0),printf("%lld ",f[i]);
	return 0;
}

详细

Test #1:

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

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: 0ms
memory: 16112kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

input:

3
100
2 3
2 1

output:

0 1 2 

result:

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

Test #6:

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

input:

4
1100
4 1
4 3
4 2

output:

1 1 3 0 

result:

wrong answer 4th numbers differ - expected: '1', found: '0'