/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Time Exceeded ≥1019ms ≥5.5 MiB
#2 Time Exceeded ≥1097ms ≥5.277 MiB
#3 Time Exceeded ≥1095ms ≥6.027 MiB
#4 Time Exceeded ≥1099ms ≥6.527 MiB
#5 Time Exceeded ≥1000ms ≥6.777 MiB
#6 Time Exceeded ≥1099ms ≥6.777 MiB
#7 Time Exceeded ≥1099ms ≥5.277 MiB
#8 Time Exceeded ≥1099ms ≥5.277 MiB
#9 Time Exceeded ≥1099ms ≥6.004 MiB
#10 Time Exceeded ≥1099ms ≥6.027 MiB
#11 Time Exceeded ≥1099ms ≥5.527 MiB
#12 Time Exceeded ≥1098ms ≥6.527 MiB
#13 Time Exceeded ≥1099ms ≥5.527 MiB
#14 Time Exceeded ≥1099ms ≥6.027 MiB
#15 Time Exceeded ≥1098ms ≥6.277 MiB
#16 Time Exceeded ≥1095ms ≥6.527 MiB
#17 Time Exceeded ≥1097ms ≥6.812 MiB
#18 Time Exceeded ≥1099ms ≥5.527 MiB
#19 Time Exceeded ≥1099ms ≥6.828 MiB
#20 Time Exceeded ≥1097ms ≥5.027 MiB
#21 Time Exceeded ≥1099ms ≥5.754 MiB
#22 Time Exceeded ≥1099ms ≥5.277 MiB
#23 Time Exceeded ≥1099ms ≥6.027 MiB
#24 Time Exceeded ≥1101ms ≥6.277 MiB
#25 Time Exceeded ≥1097ms ≥5.027 MiB
#26 Time Exceeded ≥1096ms ≥7.047 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);

 #define fr freopen("07input.txt","r",stdin);
 ofstream file("07output.txt");

pair<ll,ll> dpup[200005],dpdown[200005];
ll apple[200005];
vector<ll> tr[200005];

void dpontree(ll node,ll starting,ll par)
{
    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);
    }

    ll mxdif = 0 , sum = 0;
    for(auto e:tr[node])
    {
        if(e == par) continue;
        sum += dpdown[e].first;
        mxdif = max(mxdif , 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(ll node,ll par)
{
    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);
    }
}


void rerooting2(ll node,ll par)
{
    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 , dpdown[ee].first - dpdown[ee].second);
        }
        mxdif = max(mxdif , 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);
    }
}

void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    ll n; cin >> n;
    for(int i=1;i<=n;i++) cin>>apple[i];
    ll u,v;
    for(int i=0;i<n-1;i++)
    {
        cin >> u >> v;
        tr[u].push_back(v);
        tr[v].push_back(u);
    }
    
    ll starting = 0;
    for(int i=1;i<=n;i++)
    {
        if(apple[i])
        {
            starting = i;
            break;
        }
    }
    //if(!starting) {file<<0<<endl; return;}
    

    dpontree(starting,starting,0ll);
    //cout<<"hi";
    //file<<"hi";
    dpup[starting] = {0,0};
    //rerooting(starting,starting,tr,apple,dpup,dpdown);
    rerooting2(starting,starting);
    //for(int i=1;i<=n;i++) cout<<i<<" "<<dpup[i].first<<" "<<dpup[i].second<<"     "<<dpdown[i].first<<" "<<dpdown[i].second<<endl;

    ll 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;
    file<<ans<<endl;

    for(int i=0;i<=n;i++)
    {
        dpup[i] = {0,0};
        dpdown[i] = {0,0};
        tr[i].clear();
    }
}

int main()
{
    life_is_a_race
    fr
    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:57:42
Judged At
2024-08-02 19:57:42
Judged By
Score
0
Total Time
≥1101ms
Peak Memory
≥7.047 MiB