/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define F first 
#define S second

const ll M = 1e18 + 7;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;

int cnt = 0;

int dfs(int node, int ans, vector<int> &lvl,
    vector<int> &vis, vector<int> check, vector<int> adj[]) {
    
    vis[node] = 1;

    int cost = 0;
    for(auto it: adj[node]) {
        if(!vis[it]) {
            lvl[it] = lvl[node]+1;

            cost += dfs(it,2,lvl,vis,check,adj);
        }
    } 

    if(cost==0) {
        if(check[node] == 0) {
            return 0;
        }
    }

    return ans+cost;
}

void Hrid_solve(){
    int n;
    cin >> n;
    vector<int> check(n), vis(n,0),lvl(n+1,0);
    for(auto &i: check) cin >> i;
    int apple = count(all(check),1);

    vector<int> adj[n];
    for(int i = 0; i < n-1; i++) {
        int u,v;
        cin >> u >> v;
        u--, v--;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(check[i]) {
            ans = dfs(0,0,lvl,vis,check,adj);
            break;
        }
    }
    int mx = 0;
    for(int i = 0; i < n; i++) {
        if(check[i]) {
            mx = max(lvl[i],mx);
            // cout << lvl[i] << " ";
        }
    }

    // cout << endl;
    cout << ans-mx-1 << endl;

    cnt = 0;
    
}


/* ************** What defines us is how well we rise after we Fall !! ************** */

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
 
    int tc = 1;
    cin >> tc;
    for(int kase = 1; kase <= tc; kase++){
        // cout << "Case " << kase << ": ";
        Hrid_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:06:57
Judged At
2024-11-11 03:12:37
Judged By
Score
4
Total Time
4ms
Peak Memory
576.0 KiB