/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 5.938 MiB
#2 Accepted 6ms 5.77 MiB
#3 Accepted 6ms 5.797 MiB
#4 Accepted 7ms 5.77 MiB
#5 Accepted 29ms 6.77 MiB
#6 Accepted 68ms 7.004 MiB
#7 Accepted 33ms 6.812 MiB
#8 Accepted 21ms 5.922 MiB
#9 Accepted 40ms 6.082 MiB
#10 Accepted 52ms 6.77 MiB
#11 Accepted 61ms 7.02 MiB
#12 Accepted 43ms 6.902 MiB
#13 Accepted 20ms 7.02 MiB
#14 Accepted 52ms 7.02 MiB
#15 Accepted 27ms 6.02 MiB
#16 Accepted 18ms 6.77 MiB
#17 Accepted 18ms 6.816 MiB
#18 Accepted 19ms 6.812 MiB
#19 Accepted 61ms 13.371 MiB
#20 Accepted 88ms 13.543 MiB
#21 Accepted 61ms 13.422 MiB
#22 Accepted 106ms 28.02 MiB
#23 Accepted 109ms 12.816 MiB
#24 Accepted 113ms 12.82 MiB
#25 Accepted 96ms 12.77 MiB
#26 Accepted 13ms 6.52 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;
vector<int> adj[N];
int depth[N];
vector<int> check(N);

// int dfs(int node, int cost, int par) {

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

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

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

//     return tmp + cost;
// }

void dfs2(int node, int par) {

    for(auto it: adj[node]) {
        if(it == par) continue;
        
        depth[it] = depth[node]+1;
        dfs2(it,node);
        check[node] = max(check[node],check[it]);
    }

}

void dfs3(int node, int par) {

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

        depth[it] = depth[node]+1;
        dfs3(it,node);
    }

}


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

    bool f = true;
    for(int i = 1; i <= n; i++) {
        cin >> check[i];
        if(check[i]) f = false;
    } 

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

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

    if(f) {
        cout << 0 << endl;
        return;
    }

    int total = 0;
    int root = 0;
    int mx = -1;

    for(int i = 1; i <= n; i++) {
        if(check[i]) {
            // total = dfs(i,0,-1);
            dfs2(i,-1);
            root = i;
            break;
        }
    }
    for(int i = 1; i <= n; i++) {
        // cout << i <<  " " << check[i] << endl;

        if(check[i]) {
            if(depth[i] > mx) {
                mx = depth[i];
                root = i;
            }

            total++;
        }
        depth[i] = 0;
    }

    total = (total - 1) * 2;
    dfs3(root,-1);
    mx = -1;

    for(int i = 1; i <= n; i++) {
        if(check[i]) {
            if(depth[i] > mx) {
                mx = depth[i];
            }
        }
        depth[i] = 0;
        adj[i].clear();
        check[i] = 0;
    }
    // cout << mx << endl;    

    cout << total-mx << endl;
}


/* ************** 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 18:38:05
Judged At
2024-08-17 18:38:05
Judged By
Score
100
Total Time
113ms
Peak Memory
28.02 MiB