QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#378018#8518. Roars IIIDays_of_Future_Past#WA 4ms20988kbC++171.8kb2024-04-05 22:35:342024-04-05 22:35:34

Judging History

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

  • [2024-04-05 22:35:34]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:20988kb
  • [2024-04-05 22:35:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
#define pb push_back
typedef long long LL;
#define fi first
#define se second
const int mod = 1e9 + 7;
const int N = 200022;
char st[N];
int siz[N];
int dp[N], sd[N], ans[N];
map<int, int> ss[N];
vector<int> e[N];
void f(int v) {
    
    if(st[v] == '0') {
        dp[v] = sd[v] + (ss[v].empty() ? 0 : (ss[v].rbegin())->fi);
    } else {
        dp[v] = sd[v];
    }
}
void dfs(int v, int fa = -1) {
    siz[v] = st[v] == '1';
    sd[v] = 0;
    for(int y : e[v]) {
        if(y == fa) {
            continue;
        }
        dfs(y, v);
        siz[v] += siz[y];
        ss[v][siz[y]]++;
        sd[v] += dp[y];
    }
    f(v);
}
void inc(int fa, int v, int delta) {
    int val = (ss[fa][siz[v]] += delta);
    if(val == 0) {
        ss[fa].erase(siz[v]);
    }
}
void sfd(int v, int fa = -1) {

    if(fa != -1) {
        sd[fa] -= dp[v];
        siz[fa] -= siz[v];
        inc(fa, v, -1);
        f(fa);
        sd[v] += dp[fa];
        siz[v] += siz[fa];
        inc(v, fa, 1);
        f(v);
    }
    ans[v] = dp[v];
    //printf("!%d\n", dp[v]);
    for(int y : e[v]) {
        if(y == fa) {
            continue;
        }
        sfd(y, v);
    }
    if(fa != -1) {
        sd[v] -= dp[fa];
        siz[v] -= siz[fa];
        inc(v, fa, -1);
        f(v);
        sd[fa] += dp[v];
        siz[fa] += siz[v];
        inc(fa, v, 1);
        f(fa);
    }
}
int main() {
    int n;
    scanf("%d", &n);
    scanf("%s", st);
    for(int i = 0; i < n - 1; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        x--;
        y--;
        e[x].pb(y);
        e[y].pb(x);
    }
    dfs(0);
    sfd(0);
    for(int i = 0; i < n; i++) {
        printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

1
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1

result:

ok 2 number(s): "0 1"

Test #5:

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

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

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

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

input:

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

output:

5 3 5 6 4 4 4 7

result:

wrong answer 4th numbers differ - expected: '5', found: '6'