/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.555 MiB
#2 Wrong Answer 3ms 2.492 MiB
#3 Wrong Answer 4ms 2.559 MiB
#4 Wrong Answer 6ms 2.617 MiB
#5 Wrong Answer 43ms 4.062 MiB
#6 Wrong Answer 68ms 4.234 MiB
#7 Wrong Answer 50ms 4.344 MiB
#8 Wrong Answer 28ms 2.574 MiB
#9 Wrong Answer 63ms 2.836 MiB
#10 Wrong Answer 79ms 2.27 MiB
#11 Wrong Answer 58ms 4.281 MiB
#12 Wrong Answer 68ms 4.059 MiB
#13 Wrong Answer 33ms 4.559 MiB
#14 Wrong Answer 100ms 4.277 MiB
#15 Wrong Answer 39ms 2.891 MiB
#16 Wrong Answer 27ms 4.121 MiB
#17 Wrong Answer 26ms 4.387 MiB
#18 Wrong Answer 28ms 4.176 MiB
#19 Accepted 114ms 13.754 MiB
#20 Accepted 115ms 13.797 MiB
#21 Accepted 112ms 13.832 MiB
#22 Wrong Answer 219ms 37.609 MiB
#23 Accepted 231ms 13.379 MiB
#24 Wrong Answer 209ms 13.371 MiB
#25 Wrong Answer 144ms 12.926 MiB
#26 Wrong Answer 11ms 3.434 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],cnt;
bool a[N],flag[N];
vector<int> g[N];
set<int,greater<int>> st;

void dfs(int u,int d = 0,int p = 0) {
    if(a[u] == 1) {
        st.insert(d);
        flag[u] = 1;
    }
    for(auto x: g[u]) {
        if(x == p) continue;
        dfs(x,d+1,u);
        if(flag[x]) 
            ans[u] += ans[x]+2;
        flag[u] |= flag[x];
    }
}

void solve() {
    cin >> n;
    st.clear();
    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);
    }
    cnt = 0;
    dfs(1);
    ll sum = ans[1];
    // cerr << sum << ' ';
    if(st.size() == 1) {
        sum -= 2*(*st.begin());
    }
    else if(st.size() > 1) {
        sum -= *st.begin();
        st.erase(st.begin());
        sum -= *st.begin();
    }
    cout << sum << '\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:22:33
Judged At
2024-10-03 13:27:15
Judged By
Score
25
Total Time
231ms
Peak Memory
37.609 MiB