/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 1ms 436.0 KiB
#3 Wrong Answer 1ms 536.0 KiB
#4 Wrong Answer 4ms 748.0 KiB
#5 Time Exceeded ≥1086ms ≥13.672 MiB
#6 Time Exceeded ≥1085ms ≥15.699 MiB
#7 Time Exceeded ≥1087ms ≥14.551 MiB
#8 Wrong Answer 24ms 788.0 KiB
#9 Wrong Answer 205ms 1.941 MiB
#10 Time Exceeded ≥1086ms ≥15.633 MiB
#11 Time Exceeded ≥1090ms ≥16.609 MiB
#12 Time Exceeded ≥1084ms ≥21.055 MiB
#13 Time Exceeded ≥1091ms ≥23.309 MiB
#14 Time Exceeded ≥1086ms ≥15.305 MiB
#15 Wrong Answer 202ms 3.742 MiB
#16 Time Exceeded ≥1091ms ≥19.918 MiB
#17 Time Exceeded ≥1092ms ≥28.574 MiB
#18 Time Exceeded ≥1090ms ≥19.93 MiB
#19 Time Exceeded ≥1101ms ≥16.617 MiB
#20 Time Exceeded ≥1002ms ≥17.297 MiB
#21 Time Exceeded ≥1101ms ≥17.336 MiB
#22 Memory Exceeded ≥1169ms ≥256.016 MiB
#23 Time Exceeded ≥1102ms ≥44.316 MiB
#24 Time Exceeded ≥1103ms ≥41.328 MiB
#25 Time Exceeded ≥1103ms ≥40.387 MiB
#26 Wrong Answer 221ms 192.805 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;

// void Height(int node, vector<int> &vis, vector<int> &height, vector<int> adj[]) {
//     vis[node] = 1;

//     for(auto it: adj[node]) {
//         if(!vis[it]) {
//             Height(it,vis,height,adj);

//             height[node] = max(height[node], height[it]+1);
//         }
//     }
// }


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

    int 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(){
    int n;
    cin >> n;

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

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

    // Height(0,vis,height,adj);
    int tmp = adj[0].size();
    int mx = 0;

    for(int i = 0; i < n; i++) {
        // cout << i+1 << " : " << height[i] << endl;
        vis[i] = 0;

        if(check[i]) {
            // if(height[i] <= tmp) root = i;
            if(adj[i].size() <= tmp) root = i;
        }
    }


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

    // cout << mx << endl;
    cout << ans-mx << endl;
    
    last_ver = 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 12:30:14
Judged At
2024-08-17 12:30:14
Judged By
Score
1
Total Time
≥1169ms
Peak Memory
≥256.016 MiB