/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 532.0 KiB
#2 Wrong Answer 4ms 584.0 KiB
#3 Wrong Answer 4ms 532.0 KiB

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-11-11 03:14:59
Judged By
Score
1
Total Time
4ms
Peak Memory
584.0 KiB