/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.57 MiB
#2 Wrong Answer 3ms 2.57 MiB
#3 Wrong Answer 3ms 2.57 MiB
#4 Wrong Answer 5ms 2.48 MiB
#5 Wrong Answer 40ms 4.34 MiB
#6 Wrong Answer 72ms 4.258 MiB
#7 Wrong Answer 45ms 4.238 MiB
#8 Wrong Answer 21ms 2.582 MiB
#9 Wrong Answer 55ms 2.812 MiB
#10 Wrong Answer 92ms 4.172 MiB
#11 Wrong Answer 63ms 4.34 MiB
#12 Wrong Answer 70ms 4.328 MiB
#13 Wrong Answer 34ms 4.219 MiB
#14 Wrong Answer 98ms 4.129 MiB
#15 Wrong Answer 36ms 2.902 MiB
#16 Wrong Answer 34ms 4.457 MiB
#17 Wrong Answer 28ms 4.094 MiB
#18 Wrong Answer 29ms 4.207 MiB
#19 Accepted 111ms 13.836 MiB
#20 Accepted 111ms 13.875 MiB
#21 Accepted 110ms 13.734 MiB
#22 Wrong Answer 227ms 49.84 MiB
#23 Accepted 268ms 13.371 MiB
#24 Accepted 257ms 13.371 MiB
#25 Wrong Answer 167ms 13.156 MiB
#26 Accepted 12ms 3.934 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];
bool a[N],flag[N];
vector<int> g[N];
ll f,s,tmp;

int dfs(int u,int d = 0,int p = 0) {
    int mx = 0;
    if(a[u] == 1) {
        flag[u] = 1;
        mx = d;
    }
    set<int> st;
    for(auto x: g[u]) {
        if(x == p) continue;
        int now = dfs(x,d+1,u);
        if(now) {
            if(st.size() < 2) st.insert(now);
            else if(now > *st.begin()){
                st.erase(st.begin());
                st.insert(now);
            }
        }
        mx = max(mx,now);
        if(flag[x]) 
            ans[u] += ans[x]+2;
        flag[u] |= flag[x];
    }
    if(st.size() > 1) {
        f = *st.begin();
        st.erase(st.begin());
        s = *st.begin();
    }
    if(st.size() == 1) {
        tmp = *st.begin();
    }
    return mx;
}

void solve() {
    cin >> n;
    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);
    }
    f = 0;s = 0;
    dfs(1);
    ll sum = ans[1];
    // cerr << sum << ' ';
    if(f and s) sum -= (f+s);
    else sum -= 2*tmp;
    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:35:15
Judged At
2024-10-03 13:26:38
Judged By
Score
37
Total Time
268ms
Peak Memory
49.84 MiB