/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 2ms 540.0 KiB
#3 Wrong Answer 2ms 540.0 KiB
#4 Wrong Answer 10ms 1.031 MiB
#5 Time Exceeded ≥1093ms ≥25.547 MiB
#6 Time Exceeded ≥1096ms ≥39.785 MiB
#7 Time Exceeded ≥1093ms ≥24.445 MiB
#8 Wrong Answer 33ms 868.0 KiB
#9 Wrong Answer 881ms 3.602 MiB
#10 Time Exceeded ≥1092ms ≥18.945 MiB
#11 Time Exceeded ≥1094ms ≥32.199 MiB
#12 Time Exceeded ≥1088ms ≥18.648 MiB
#13 Time Exceeded ≥1099ms ≥38.391 MiB
#14 Time Exceeded ≥1096ms ≥28.133 MiB
#15 Wrong Answer 866ms 7.508 MiB
#16 Time Exceeded ≥1099ms ≥47.594 MiB
#17 Time Exceeded ≥1057ms ≥40.238 MiB
#18 Time Exceeded ≥1095ms ≥35.844 MiB
#19 Time Exceeded ≥1100ms ≥20.227 MiB
#20 Time Exceeded ≥1100ms ≥21.996 MiB
#21 Time Exceeded ≥1097ms ≥21.879 MiB
#22 Memory Exceeded ≥236ms ≥256.016 MiB
#23 Time Exceeded ≥1100ms ≥49.215 MiB
#24 Time Exceeded ≥1102ms ≥49.117 MiB
#25 Time Exceeded ≥1101ms ≥45.238 MiB
#26 Memory Exceeded ≥215ms ≥256.016 MiB

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 last_ver = 0;
ll root = 0;

void root_find(ll node,vector<ll> &vis, vector<ll> check, vector<ll> adj[]){
    vis[node] = 1;

    for (auto it : adj[node]){
        if(!vis[it]) {
            root_find(it, vis, check, adj);
            if (!root && check[it]) root = it;
        }
    }
}

ll dfs(ll node, ll cost, vector<ll> &vis, vector<ll> check, vector<ll> &depth, vector<ll> adj[]) {
    vis[node] = 1;
    if(check[node]) last_ver = node;

    ll tmp = 0;
    for(auto it: adj[node]) {
        if(!vis[it]) {
            depth[it] = depth[node] + 1;
            tmp += dfs(it,2,vis,check,depth,adj);
        }
    }

    if(tmp == 0 && !check[node]) return 0;

    return tmp + cost;
}

void Hrid_solve(){
    ll n;
    cin >> n;

    vector<ll> check(n), vis(n,0), depth(n,0);
    for(ll i = 0; i < n; i++) {
        cin >> check[i];
        if(check[i]) root = i;
    } 

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

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

    if(count(all(check),1)==0) {
        cout << 0 << endl;
        return;
    }

    ll tmp = adj[0].size();
    ll mx = 0;

    root_find(0,vis,check,adj);
    for(ll i = 0; i < n; i++) {
        // if(check[i]) {
        //     if(adj[i].size() <= tmp) root = i;
        // }

        vis[i] = 0;
    }



    ll ans = dfs(root,0,vis,check,depth,adj);
    for(ll i = 0; i < n; i++) {
        if(check[i]) {
            mx = max(mx, depth[i]);
        }
    }

    // cout << last_ver << endl;
    // cout << ans << " ";
    cout << ans-mx << endl;
    root = 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);
 
    ll tc = 1;
    cin >> tc;
    for(ll kase = 1; kase <= tc; kase++){
        // cout << "Case " << kase << ": ";
        Hrid_solve();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-17 15:09:31
Judged At
2024-08-17 15:09:31
Judged By
Score
1
Total Time
≥1102ms
Peak Memory
≥256.016 MiB