/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 1ms 532.0 KiB
#3 Wrong Answer 1ms 344.0 KiB
#4 Wrong Answer 11ms 788.0 KiB
#5 Time Exceeded ≥1082ms ≥13.781 MiB
#6 Time Exceeded ≥1005ms ≥22.746 MiB
#7 Time Exceeded ≥1083ms ≥13.25 MiB
#8 Wrong Answer 24ms 788.0 KiB
#9 Wrong Answer 126ms 2.176 MiB
#10 Time Exceeded ≥1081ms ≥15.691 MiB
#11 Time Exceeded ≥1081ms ≥21.32 MiB
#12 Time Exceeded ≥1082ms ≥18.777 MiB
#13 Time Exceeded ≥1084ms ≥23.617 MiB
#14 Time Exceeded ≥1084ms ≥15.352 MiB
#15 Wrong Answer 121ms 3.316 MiB
#16 Wrong Answer 842ms 29.402 MiB
#17 Time Exceeded ≥1081ms ≥21.414 MiB
#18 Wrong Answer 882ms 19.742 MiB
#19 Time Exceeded ≥1099ms ≥16.734 MiB
#20 Time Exceeded ≥1100ms ≥17.496 MiB
#21 Time Exceeded ≥1093ms ≥17.633 MiB
#22 Memory Exceeded ≥1151ms ≥256.016 MiB
#23 Time Exceeded ≥1096ms ≥30.75 MiB
#24 Time Exceeded ≥1099ms ≥30.656 MiB
#25 Time Exceeded ≥1100ms ≥28.305 MiB
#26 Wrong Answer 188ms 193.023 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 cnt = 0;
vector<int> tmp;

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

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

            cost += dfs(it,2,lvl,vis,check,adj);
            // lvl[node] = max(lvl[node],lvl[it]+1);
        }
    } 

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

    return ans+cost;
}

void bfs2(int node, int n ,vector<int> &lvl,
    vector<int> adj[])
{  
    queue<int> q;
    q.push(node);
    int vis[n] = {0};

    while(!q.empty()) {
        int v = q.front();
        q.pop();

        for(auto it: adj[v]){
            if(!vis[it]) {
                vis[it] = 1;
                lvl[it] = lvl[v]+1;
                q.push(it);
            }
        }
    }
}


void Hrid_solve(){
    int n;
    cin >> n;
    vector<int> check(n), vis(n,0),lvl(n,0);
    for(auto &i: check) cin >> 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);
    }


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

    int mx = 0;
    for(int i = 0; i < n; i++) {
        // cout <<i+1 << " " << lvl[i] << endl;
        
        if(check[i]) mx = max(mx,lvl[i]);
    }

    // for(auto i: tmp) cout << i << " ";
    // cout << endl;
    // cout << tmp[0] << " " << lvl[tmp[0]] << endl;
    
    // cout << ans << endl;
    cout << ans-mx << endl;

    tmp.clear();
    
}


/* ************** 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 11:39:15
Judged At
2024-08-17 11:39:15
Judged By
Score
1
Total Time
≥1151ms
Peak Memory
≥256.016 MiB