QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446016#8518. Roars IIIgrass8cowTL 2971ms18632kbC++17829b2024-06-16 19:35:032024-06-16 19:35:04

Judging History

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

  • [2024-06-16 19:35:04]
  • 评测
  • 测评结果:TL
  • 用时:2971ms
  • 内存:18632kb
  • [2024-06-16 19:35:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n;
char c[201000];
vector<int>g[200100];
multiset<int>s[201000];
#define pb push_back
ll ans;
void dfs(int x,int f,int d){
    s[x].clear();
    int so=-1,mm=-1;
    for(int v:g[x])if(v!=f){
        dfs(v,x,d+1);
        if(mm<(int)s[v].size())mm=s[v].size(),so=v;
    }
    if(so!=-1)swap(s[x],s[so]);
    for(int v:g[x])if(v!=f&&v!=so){for(int o:s[v])s[x].insert(o);}
    if(c[x]=='1'){s[x].insert(d);return;}
    if(s[x].empty())return;
    auto it=--s[x].end();
    ans+=(*it)-d,s[x].erase(it),s[x].insert(d);
}
int main(){
    scanf("%d%s",&n,c+1);
    for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),g[u].pb(v),g[v].pb(u);
    for(int i=1;i<=n;i++){
        ans=0,dfs(i,0,0);
        printf("%lld ",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 2ms
memory: 17988kb

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

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

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

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

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: 6ms
memory: 18072kb

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

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: 2971ms
memory: 18632kb

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:


result: