QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442782#8518. Roars IIIucup-team2279#WA 3ms20492kbC++202.9kb2024-06-15 13:29:472024-06-15 13:29:48

Judging History

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

  • [2024-06-15 13:29:48]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:20492kb
  • [2024-06-15 13:29:47]
  • 提交

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; Max[u]=0;
	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){
		if(ban[v]){
			auto it=s[v].end();
			int len=s[v].size();
			for(int j=0; j<min(len,sum); j++)
				--it,s[u].insert(*it+dep[u]+1);
			f[u]+=f[v];
		}
		else{
			dep[v]=dep[u]+1;
			dp(v,u);
			merge(s[u],s[v]);
			s[v].clear();
			f[u]+=f[v];
		}
		while((int)s[u].size()>sum) s[u].erase(s[u].begin());
	}
	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];
	}
}

void solve(int u){
	ban[u]=1; sum=siz[u];
	ll ret=0;
	for(auto v:adj[u]){
		if(!ban[v]){
			dep[v]=1;
			dp(v,u);
			for(auto val:s[v]) s[u].insert(val);
			ret+=f[v];
		}
		else{
			auto it=s[v].end();
			int len=s[v].size();
			for(int j=0; j<min(len,sum); j++)
				--it,s[u].insert(*it+1);
			ret+=f[v];
		}
		while((int)s[u].size()>sum) s[u].erase(s[u].begin());
	}
	if(a[u]) ans[u]=ret;
	else ans[u]=ret+(s[u].empty()?0:*--s[u].end());
	
	for(auto v:adj[u]){
		if(!ban[v]){
			for(auto val:s[v]) if(s[u].find(val)!=s[u].end()) s[u].erase(s[u].find(val));
			s[u].insert(0);
			int x=-1; ll y=f[v];
			if(!a[u]){
				x=*--s[u].end();
				ret+=x;
				s[u].erase(--s[u].end());
			}
			ret-=y;
			f[u]=ret;
			
			multiset <int> P;
			swap(P,s[v]);
			
			sum=siz[v];
			rt=0;
			calcsiz(v,u);
//			cerr<<v<<' '<<rt<<' '<<siz[v]<<'\n';
			calcsiz(rt,0);
			
			solve(rt);
			
			ret+=y;
			if(!a[u])
				ret-=x,s[u].insert(x);
			s[u].erase(s[u].find(0));
			for(auto val:P) s[u].insert(val);
		}
	}
}
int main(){
	Max[0]=0x3f3f3f3f;
	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;
	calcsiz(1,0),calcsiz(rt,0);
	solve(rt);
	for(int i=1; i<=n; i++) printf("%lld ",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 20192kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 3ms
memory: 20492kb

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

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: 3ms
memory: 18460kb

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: 20256kb

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: 0
Accepted
time: 3ms
memory: 18216kb

input:

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

output:

5 3 5 5 4 4 4 6 

result:

ok 8 numbers

Test #9:

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

input:

64
1110101001010110011100110000010100011011010001000111010110100101
57 60
58 63
7 43
64 9
19 8
62 17
4 40
41 18
34 56
46 14
41 36
57 26
46 58
16 32
59 1
8 17
17 13
34 29
55 10
43 5
34 8
28 36
6 10
37 21
11 48
2 8
56 55
3 8
56 61
53 52
49 51
20 30
52 39
57 22
9 49
18 16
9 27
50 52
38 40
59 43
2 18
31...

output:

34 29 29 33 34 37 34 29 31 30 49 31 41 33 27 36 34 29 29 27 30 36 30 27 34 36 38 46 27 27 38 36 50 27 33 36 30 33 30 27 36 40 34 34 31 33 38 40 36 30 36 30 37 36 30 27 29 33 34 36 32 34 40 27 

result:

wrong answer 29th numbers differ - expected: '29', found: '27'