QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#445684#8518. Roars IIIucup-team1231#WA 2ms9956kbC++201.6kb2024-06-16 07:55:132024-06-16 07:55:13

Judging History

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

  • [2024-06-16 07:55:13]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9956kb
  • [2024-06-16 07:55:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;

char c[200000];
vi adjList[200000];
int sub[200000];
pii m1[200000],m2[200000];
LLI ans[200000];
int doDFS(int u,int p) {
    m1[u] = m2[u] = mp(-1,-1);
    sub[u] = (c[u] == '1');
    for (int v: adjList[u]) {
        if (v != p) {
            doDFS(v,u);
            sub[u] += sub[v],ans[u] += ans[v];
            pii q = mp(sub[v],v);
            if (q >= m1[u]) m2[u] = m1[u],m1[u] = q;
            else if (q >= m2[u]) m2[u] = q;
        }
    }
    if (c[u] == '0') ans[u] += m1[u].first;
    return 0;
}
LLI ans2[200000];
int doDFS2(int u,int p) {
    for (int v: adjList[u]) {
        if (v != p) {
            ans2[v] = ans2[u];
            if (c[u] == '0') {
                ans2[v] -= max(m1[u].first,sub[0]-sub[u]);
                ans2[v] += max((v == m1[u].second) ? m2[u].first:m1[u].first,sub[0]-sub[u]);
            }
            if (c[v] == '0') {
                ans2[v] -= m1[v].first;
                ans2[v] += max(m1[v].first,sub[0]-sub[v]);
            }
            doDFS2(v,u);
        }
    }
    return 0;
}
int main() {
    int i;
    int n,a,b;
    scanf("%d",&n);
    for (i = 0; i < n; i++) scanf(" %c",&c[i]);
    for (i = 0; i < n-1; i++) {
        scanf("%d %d",&a,&b);
        a--,b--;
        adjList[a].pb(b);
        adjList[b].pb(a);
    }

    doDFS(0,-1);
    ans2[0] = ans[0];
    doDFS2(0,-1);
    for (i = 0; i < n; i++) printf("%lld ",ans2[i]);
    printf("\n");

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 7992kb

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

input:

1
0

output:

-1 

result:

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