/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Wrong Answer 5ms 5.02 MiB
#3 Wrong Answer 5ms 5.02 MiB
#4 Wrong Answer 10ms 5.332 MiB
#5 Time Exceeded ≥1089ms ≥17.504 MiB
#6 Time Exceeded ≥1089ms ≥24.578 MiB
#7 Time Exceeded ≥1089ms ≥16.828 MiB
#8 Wrong Answer 26ms 5.27 MiB
#9 Wrong Answer 331ms 6.684 MiB
#10 Time Exceeded ≥1087ms ≥18.863 MiB
#11 Time Exceeded ≥1090ms ≥20.57 MiB
#12 Time Exceeded ≥1085ms ≥17.863 MiB
#13 Time Exceeded ≥1094ms ≥27.617 MiB
#14 Time Exceeded ≥1089ms ≥18.68 MiB
#15 Wrong Answer 348ms 8.402 MiB
#16 Time Exceeded ≥1093ms ≥32.809 MiB
#17 Time Exceeded ≥1092ms ≥24.773 MiB
#18 Time Exceeded ≥1092ms ≥22.516 MiB
#19 Time Exceeded ≥1002ms ≥14.137 MiB
#20 Time Exceeded ≥1002ms ≥14.887 MiB
#21 Time Exceeded ≥1101ms ≥14.938 MiB
#22 Memory Exceeded ≥1165ms ≥256.016 MiB
#23 Time Exceeded ≥1002ms ≥28.262 MiB
#24 Time Exceeded ≥1002ms ≥28.273 MiB
#25 Time Exceeded ≥1101ms ≥25.816 MiB
#26 Accepted 419ms 197.277 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 root = 0;
vector<int> adj[N];
int depth[N];

void root_find(int node,int par, vector<int> check, vector<int> adj[]){

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

int dfs(int node, int cost, int par, vector<int> check, vector<int> adj[]) {

    int tmp = 0;
    for(auto it: adj[node]) {
        if(it == par) continue;

        depth[it] = depth[node] + 1;
        tmp += dfs(it,2,node,check,adj);
    }

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

    return tmp + cost;
}

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

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

    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);
    }

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

    root_find(0,-1,check,adj);
    // cout << root << endl;
    int mx = 0;

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

        adj[i].clear();
        depth[i] = 0;
    }

    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);
 
    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
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-17 15:20:21
Judged At
2024-08-17 15:20:21
Judged By
Score
7
Total Time
≥1165ms
Peak Memory
≥256.016 MiB