/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.559 MiB
#2 Accepted 4ms 2.527 MiB
#3 Wrong Answer 4ms 2.562 MiB
#4 Wrong Answer 6ms 2.613 MiB
#5 Wrong Answer 37ms 4.035 MiB
#6 Wrong Answer 76ms 4.508 MiB
#7 Wrong Answer 62ms 4.223 MiB
#8 Wrong Answer 27ms 2.578 MiB
#9 Wrong Answer 76ms 2.793 MiB
#10 Wrong Answer 95ms 4.203 MiB
#11 Wrong Answer 78ms 4.34 MiB
#12 Wrong Answer 74ms 4.285 MiB
#13 Wrong Answer 42ms 4.305 MiB
#14 Wrong Answer 121ms 4.645 MiB
#15 Wrong Answer 42ms 2.887 MiB
#16 Wrong Answer 39ms 4.312 MiB
#17 Wrong Answer 30ms 4.184 MiB
#18 Wrong Answer 40ms 4.469 MiB
#19 Accepted 129ms 13.992 MiB
#20 Accepted 118ms 13.879 MiB
#21 Accepted 124ms 13.738 MiB
#22 Accepted 189ms 28.449 MiB
#23 Wrong Answer 303ms 13.367 MiB
#24 Wrong Answer 288ms 13.375 MiB
#25 Accepted 201ms 13.164 MiB
#26 Accepted 22ms 3.477 MiB

Code

#define _GLIBCXX_FILESYSTEM
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+2;
ll n,ans[N],root,mx;
bool a[N],flag[N];
vector<int> g[N];

void dfs(int u,int p = 0) {
    if(a[u] == 1) {
        flag[u] = 1;
    }
    bool f = 0;
    for(auto x: g[u]) {
        if(x == p) continue;
        dfs(x,u);
        if(flag[x]) f = 1;
        flag[u] |= flag[x];
    }
    // if(u == 3) cerr << f << ' ' << flag[u] << '\n';
    if(!f and flag[u] and !root) {
        root = u;
    }
}

void dfs1(int u,int d = 0,int p = 0) {
    if(a[u] == 1) {
        flag[u] = 1;
        mx = max(mx,(ll)d);
    }
    for(auto x: g[u]) {
        if(x == p) continue;
        dfs1(x,d+1,u);
        if(flag[x]) 
            ans[u] += ans[x]+2;
        flag[u] |= flag[x];
    }
}

void solve() {
    cin >> n;
    root = 0;
    for(int i = 0; i < n; i++) {
        cin >> a[i+1];
        flag[i+1] = 0;
        ans[i+1] = 0;
        g[i+1].clear();
    }
    for(int i = 1; i < n; i++) {
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1);
    // cerr << root << ' ';
    if(root) {
        mx = 0;
        for(int i = 0; i < n; i++) flag[i+1] = 0;
        dfs1(root);
        ans[root] -= mx; 
        cout << ans[root] << '\n';
    }
    else cout << 0 << '\n';
    return;
}

int32_t main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    cin >> tc;
    for(int kase = 1; kase <= tc; kase++) {
        //cout << "Case " << kase << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Contest
Bangladesh 2.0
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-16 16:56:08
Judged At
2024-10-03 13:24:51
Judged By
Score
40
Total Time
303ms
Peak Memory
28.449 MiB