QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442557#8518. Roars IIIucup-team2279#TL 2898ms18600kbC++201.5kb2024-06-15 12:44:252024-06-15 12:44:25

Judging History

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

  • [2024-06-15 12:44:25]
  • 评测
  • 测评结果:TL
  • 用时:2898ms
  • 内存:18600kb
  • [2024-06-15 12:44:25]
  • 提交

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++) dep[i]=0,dp(i,0),s[i].clear(),printf("%lld ",f[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 16204kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

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

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

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

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

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 29 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:

ok 64 numbers

Test #10:

score: 0
Accepted
time: 20ms
memory: 16308kb

input:

512
11100000100000011111111011010110001001101100011110001001111001011011000001010000100011011101001001101100010000011100000110101010100001001110000011010000111001000110010011000100000010010000100100000000100001001011100010100100101101010000111100110010001010010011101010001000111001001010111111111101...

output:

229 209 213 228 257 224 211 243 208 238 231 211 246 231 235 206 211 208 220 225 226 206 220 251 215 220 238 234 230 208 215 235 230 220 220 235 233 217 243 250 206 220 221 253 230 206 240 209 208 220 215 251 228 221 228 219 208 208 219 256 221 206 222 208 228 230 208 230 235 241 228 223 231 221 229 ...

result:

ok 512 numbers

Test #11:

score: 0
Accepted
time: 2898ms
memory: 18600kb

input:

4096
0011111001111111001000100000001010100111001000010100110000110111011111111001001101101010011010001010111100111110001111000000000111001110100110100011011101110011101111001010011011110101111110000101111111100101101001001000000110100111100010110110011001111111011111100111001101111110110111000010101...

output:

1579 1639 1528 1542 1551 1523 1522 1553 1553 1532 1533 1551 1562 1534 1564 1568 1583 1559 1553 1537 1533 1568 1541 1566 1563 1557 1546 1537 1551 1586 1570 1548 1576 1587 1523 1541 1539 1522 1538 1565 1554 1589 1548 1614 1570 1570 1549 1527 1578 1520 1569 1581 1540 1540 1551 1579 1594 1550 1551 1550 ...

result:

ok 4096 numbers

Test #12:

score: -100
Time Limit Exceeded

input:

32768
010000100111010011101111111011001000100100101001100000110010100111111101110011000110001011001001100110111010010111000001110101000011101010010101111101001100100011101101010000001011001010100011011011001001111101011000001101101110101010001001011011101001010101010000011111100011110100011000001110...

output:

12964 12950 12999 13070 12980 12974 12927 12949 12952 12953 12999 12962 12972 12972 12951 12989 12948 12971 12930 12954 13012 12972 12946 12985 12946 12926 12996 13037 12915 12915 13011 13017 12939 13055 13044 12986 12986 12984 12968 12958 12960 12973 12981 12976 12992 13011 12991 12930 13001 13018 ...

result: