/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 3ms 480.0 KiB
#5 Accepted 34ms 4.809 MiB
#6 Accepted 64ms 5.117 MiB
#7 Accepted 46ms 5.043 MiB
#8 Accepted 30ms 664.0 KiB
#9 Accepted 66ms 1004.0 KiB
#10 Accepted 87ms 5.078 MiB
#11 Accepted 59ms 5.312 MiB
#12 Accepted 68ms 5.484 MiB
#13 Accepted 29ms 5.52 MiB
#14 Accepted 93ms 5.156 MiB
#15 Accepted 41ms 1.27 MiB
#16 Accepted 23ms 5.023 MiB
#17 Accepted 22ms 5.109 MiB
#18 Accepted 21ms 5.062 MiB
#19 Accepted 168ms 31.887 MiB
#20 Accepted 151ms 31.887 MiB
#21 Accepted 128ms 31.082 MiB
#22 Accepted 100ms 39.77 MiB
#23 Accepted 192ms 31.02 MiB
#24 Accepted 198ms 31.391 MiB
#25 Accepted 105ms 31.391 MiB
#26 Accepted 7ms 2.109 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    ll n, cnt = 0;
    cin >> n;
    vector<ll> val(n + 1);
    for (int i = 1; i <= n; i++) {
        ll x; cin >> x;
        val[i] = x;
        if(x) ++cnt;
    }
    vector<set<ll>> g(n + 1);
    for(int i = 0; i + 1 < n; i++) {
        ll u, v; cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
    }
    if(cnt == 0) {
        cout << 0 << endl;
        return;
    }

    priority_queue<ll> pq;
    for(int i = 1; i <= n; i++) {
        if(g[i].size() == 1 && val[i] == 0) pq.push(i);
    }
    while(pq.size()) {
        ll u = pq.top();
        ll v = *g[u].begin();
        // cout << u << " <= ";
        pq.pop();
        if(val[v] == 0 && g[v].size() <= 2) {
            pq.push(v);
        }
        g[u].clear();
        g[v].erase(u);
    }

    vector<ll> depth(n + 1, 0);
    auto dfs2 = [&](int u, int par, int d, auto&& self) -> void {
        depth[u] = d;
        for(auto &i: g[u]) {
            if(i == par) continue;
            self(i, u, d + 1, self);
        }
    };
    ll root = 1;
    for(int i = 1; i <= n; i++) {
        if(g[i].size() == 1) {
            dfs2(i, -1, 0, dfs2);
            root = max_element(all(depth)) - depth.begin();
            break;
        }
    }
    
    ll ans = 0;
    auto dfs = [&](int u, int par, int d, auto&& self) -> ll {
        ll mxD = d;
        for(auto &v: g[u]) {
            if(v == par) continue;
            // cout << u << " " << v << endl;
            ++ans;
            mxD = max(mxD, self(v, u, d + 1, self));
            ++ans;
        }
        return mxD;
        // cout << u << " => " << c << endl;
    };
    ans = 0;
    ans -= dfs(root, -1, 0, dfs);
    cout << ans << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        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 17:38:11
Judged At
2024-08-16 17:38:11
Judged By
Score
100
Total Time
198ms
Peak Memory
39.77 MiB