/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 2ms 336.0 KiB
#3 Accepted 2ms 332.0 KiB
#4 Accepted 5ms 632.0 KiB
#5 Accepted 44ms 2.531 MiB
#6 Accepted 82ms 2.738 MiB
#7 Accepted 60ms 2.77 MiB
#8 Accepted 41ms 640.0 KiB
#9 Accepted 91ms 772.0 KiB
#10 Accepted 103ms 2.707 MiB
#11 Accepted 73ms 2.746 MiB
#12 Accepted 83ms 2.699 MiB
#13 Accepted 34ms 2.945 MiB
#14 Accepted 120ms 2.828 MiB
#15 Accepted 54ms 924.0 KiB
#16 Accepted 28ms 2.516 MiB
#17 Accepted 27ms 2.695 MiB
#18 Accepted 27ms 2.504 MiB
#19 Time Exceeded ≥1029ms ≥15.754 MiB
#20 Time Exceeded ≥1043ms ≥15.758 MiB
#21 Time Exceeded ≥1046ms ≥15.777 MiB
#22 Accepted 159ms 36.266 MiB
#23 Accepted 220ms 15.285 MiB
#24 Accepted 225ms 15.297 MiB
#25 Accepted 146ms 15.027 MiB
#26 Accepted 9ms 1.562 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);


void dpontree(int node,int starting,int par,vector<vector<int>>&tr, vector<int>&apple, vector<pair<int,int>>&dpdown)
{
    if(apple[node]) dpdown[node] = {0,0};
    else dpdown[node] = {-2,0};
    for(auto e:tr[node])
    {
        if(e == par) continue;
        dpontree(e,starting,node,tr,apple,dpdown);
    }

    ll mxdif = 0 , sum = 0;
    for(auto e:tr[node])
    {
        if(e == par) continue;
        sum += dpdown[e].first;
        mxdif = max(mxdif , (ll) dpdown[e].first - dpdown[e].second);
    }
    sum += (tr[node].size()-1)*2;
    if(node == starting) sum += 2;
    if(sum > 0) dpdown[node] = {sum , sum - mxdif -1};
}

void rerooting(int node,int par,vector<vector<int>>&tr, vector<int>&apple, vector<pair<int,int>>&dpup, vector<pair<int,int>>&dpdown)
{
    ll mxdif = -1, mxdif2 = -1, mxe = -1, mxe2 = -1,sum = 0;
    for(auto e:tr[node])
    {
        if(e == par) continue;
        sum += dpdown[e].first + 2;
        if(dpdown[e].first-dpdown[e].second >= mxdif)
        {
            mxdif2 = mxdif;
            mxe2 = mxe;
            mxdif = dpdown[e].first - dpdown[e].second;
            mxe = e;
        }
        else if(dpdown[e].first - dpdown[e].second >= mxdif2)
        {
            mxdif2 = dpdown[e].first - dpdown[e].second;
            mxe2 = e;
        }
    }
    
    sum += dpup[node].first;
    if(dpup[node].first - dpup[node].second - 1  > mxdif)
    {
        mxdif2 = mxdif;
        mxe2 = mxe;
        mxdif = dpup[node].first - dpup[node].second - 1;
        mxe = node;
    }
    else if(dpup[node].first - dpup[node].second - 1 > mxdif2)
    {
        mxdif2 = dpup[node].first - dpup[node].second - 1;
        mxe2 = node;
    }

    for(auto e:tr[node])
    {
        if(e == par) continue;
    
        if(e == mxe)
        {
            dpup[e].first = sum - dpdown[e].first;
            dpup[e].second = dpup[e].first - mxdif2 - 2;
        }
        else
        {
            dpup[e].first = sum - dpdown[e].first ;
            dpup[e].second = dpup[e].first - mxdif - 2;
        }
        
        rerooting(e,node,tr,apple,dpup,dpdown);
    }
}


void rerooting2(int node,int par,vector<vector<int>>&tr, vector<int>&apple, vector<pair<int,int>>&dpup, vector<pair<int,int>>&dpdown)
{
    for(auto e:tr[node])
    {
        if(e == par) continue;

        ll sum = 0, mxdif = -1;
        for(auto ee:tr[node])
        {
            if(ee == e || ee == par) continue;
            sum += dpdown[ee].first + 2;
            mxdif = max(mxdif ,(ll) dpdown[ee].first - dpdown[ee].second);
        }
        mxdif = max(mxdif ,(ll) dpup[node].first - dpup[node].second - 1);
        sum += dpup[node].first;
        dpup[e].first = sum+2;
        dpup[e].second = dpup[e].first - mxdif - 2;
        rerooting2(e,node,tr,apple,dpup,dpdown);
    }
}

void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    int n; cin >> n;
    vector<int> apple(n+1);
    for(int i=1;i<=n;i++) cin>>apple[i];
    vector<vector<int>> tr(n+1);
    for(int i=0;i<n-1;i++)
    {
        int u,v; cin >> u >> v;
        tr[u].push_back(v);
        tr[v].push_back(u);
    }

    vector<pair<int,int>> dpdown(n+1),dpup(n+1);
    int starting = 0;
    for(int i=1;i<=n;i++)
    {
        if(apple[i])
        {
            starting = i;
            break;
        }
    }
    if(!starting) {cout<<0<<endl; return;}
 
    dpontree(starting,starting,0,tr,apple,dpdown);
    dpup[starting] = {0,0};
    //rerooting(starting,starting,tr,apple,dpup,dpdown);
    rerooting2(starting,starting,tr,apple,dpup,dpdown);
    //(int i=1;i<=n;i++) cout<<i<<" "<<dpup[i].first<<" "<<dpup[i].second<<"     "<<dpdown[i].first<<" "<<dpdown[i].second<<endl;

    int ans = 1e9;
    for(int i=1;i<=n;i++)
    {
        if(apple[i])
        {
            ans = min ({ans ,dpup[i].first +  dpdown[i].second, dpup[i].second + dpdown[i].first });
        }
    }
    cout<<ans<<endl;
}

int main()
{
    life_is_a_race
    int t=1; 
    cin>>t;
    for(int i=1;i<=t;i++) solve(i);
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-02 19:49:41
Judged At
2024-10-03 13:33:39
Judged By
Score
82
Total Time
≥1046ms
Peak Memory
≥36.266 MiB