Submission #559634

# Submission time Handle Problem Language Result Execution time Memory
559634 2022-05-10T10:43:00 Z fcmalkcin Road Closures (APIO21_roads) C++17
0 / 100
2000 ms 93476 KB
#include <bits/stdc++.h>
using namespace std;

#define ll  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn=5e5+50;
const ll mod=1e9+7  ;
const ll base=1e18;


/// i believe myself
/// goal 0/7

set<pll> adj[maxn];
pll anc[maxn];
struct tk
{
    vector<pll> st;
    vector<ll> vt;
    ll n;
    void setup()
    {
        n=vt.size();
        st=vector<pll> (4*n+5, {0,0});
    }
    pll mer(pll a,pll b)
    {
        return make_pair(a.ff+b.ff,a.ss+b.ss);
    }
    void update(ll id,ll left,ll right,ll x)
    {
        if (x>right||x<left)
            return ;
        if (left==right)
        {
            st[id].ff+=1;
            st[id].ss+=vt[left-1];
            return ;
        }
        ll mid=(left+right)/2;
        update(id*2,left,mid,x);
        update(id*2+1,mid+1,right,x);
        st[id]=mer(st[id*2],st[id*2+1]);
    }
    void update1(ll pos)
    {
        pos=lower_bound(vt.begin(),vt.end(),pos)-vt.begin()+1;
        update(1,1,n,pos);
    }
    pll get(ll id,ll left,ll right,ll x,ll y)
    {
        if (x>right||y<left)
            return make_pair(0,0);
        if (x<=left&&y>=right)
            return st[id];
        ll mid=(left+right)/2;
        return mer(get(id*2,left,mid,x,y),get(id*2+1,mid+1,right,x,y));
    }
    ll get1(ll id,ll left,ll right,ll p)
    {
        if (left==right)
            return left;
        ll mid=(left+right)/2;
        if (st[id*2].ff>=p)
            return get1(id*2,left,mid,p);
        return get1(id*2+1,mid+1,right,p-st[id*2].ff);
    }
    ll get2(ll p)
    {
        if (p==0)
            return 0;
        ll pos=get1(1,1,n,p);
        auto h=get(1,1,n,1,pos);
        return h.ss-(h.ff-p)*vt[pos-1];
    }
};
tk gr[maxn];
ll cnt=0;
ll f[maxn];
bool dd[maxn];
ll sl[maxn];

void dfs(ll u,ll par)
{
    cnt++;
    f[u]=cnt;
    for (auto p:adj[u])
    {
        ll to=p.ff;
        if (to==par)
            continue;
        anc[to]=make_pair(u,p.ss);
        dfs(to,u);
        gr[u].vt.pb(p.ss);
    }
    sort(gr[u].vt.begin(),gr[u].vt.end());
    gr[u].vt.resize(unique(gr[u].vt.begin(),gr[u].vt.end())-gr[u].vt.begin());
    gr[u].setup();
}
set<pll> st;
void ers(ll u)
{
    st.erase(make_pair(f[u],u));
    for (auto to:adj[u])
    {
        ll h=to.ff;
        ll w=to.ss;
        adj[h].erase(make_pair(u,w));
    }
    gr[anc[u].ff].update1(anc[u].ss);
    sl[anc[u].ff]++;
}
ll dp[maxn][2];
ll k;
void dfs1(ll u,ll par,ll w)
{
    dp[u][0]=base;
    dp[u][1]=base;
    vector<ll> vt;
    ll cntnw=0;
    for (auto p:adj[u])
    {
        ll to=p.ff;
        ll w=p.ss;
        if (to==par)
            continue;
        dfs1(to,u,w);
        cntnw+=dp[to][0];
        if (dp[to][0]>dp[to][1])
        {
            vt.pb(dp[to][1]-dp[to][0]);
        }
    }
    sort(vt.begin(),vt.end());
    ll pre=cntnw;
    for (int i=-1; i<min(k,(ll)vt.size()); i++)
    {
        if (i!=-1)
        {
            cntnw+=vt[i];
        }
        ll slnw=i+1+sl[u];
        ll h=max(0ll,slnw-k);
        dp[u][0]=min(dp[u][0],cntnw+gr[u].get2(h)+w);
    }
    cntnw=pre;
    for (int i=-1; i<min(k,(ll)vt.size()); i++)
    {
        if (i!=-1)
        {
            cntnw+=vt[i];
        }
        ll slnw=i+2+sl[u];
        ll h=max(0ll,slnw-k);
        if (h<=sl[u])
            dp[u][1]=min(dp[u][1],cntnw+gr[u].get2(h)+w);
    }
}
vector<ll> minimum_closure_costs(int N, vector<int> U,vector<int> V,vector<int> W)
{
    ll n=N;
    vector<pll> vt;
    for (int i=0; i<U.size(); i++)
    {
        adj[U[i]+1].insert(make_pair(V[i]+1,W[i]));
        adj[V[i]+1].insert(make_pair(U[i]+1,W[i]));
    }
    for (int i=1; i<=n; i++)
    {
        vt.pb(make_pair(adj[i].size(),i));
    }
    sort(vt.begin(),vt.end());
    dfs(1,0);
    for (int i=1; i<=n; i++)
        st.insert(make_pair(f[i],i));
    ll id=0;
    vector<ll> res;
    for (int t=0; t<=n-1; t++)
    {
        k=t;
        while (id<vt.size()&&vt[id].ff<=t)
        {
            ll pos=vt[id].ss;
            ers(pos);
            id++;
        }
        ll ans=0;
        for (auto to:st)
        {
            if (dd[to.ss])
                continue;
            dfs1(to.ff,0,0);
            ans+=dp[to.ff][0];
        }
        res.pb(ans);
        for (auto to:st)
        {
            dd[to.ss]=0;
        }
    }
    return res;
}

/*int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp", "r"))
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll n;
    vector<ll> p,p1,p2;
    cin>> n;
    for (int i=1;i<=n-1;i++)
    {
        ll x, y, w;
        cin>> x>> y>> w;
        p.pb(x);
        p1.pb(y);
        p2.pb(w);
    }
    vector<ll> res=minimum_closure_costs(n, p,p1,p2);
    for (auto to:res) cout <<to<<" ";
}*/
/*
5
0 1 1
0 2 4
0 3 3
2 4 2
*/

Compilation message

roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:169:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |     for (int i=0; i<U.size(); i++)
      |                   ~^~~~~~~~~
roads.cpp:187:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |         while (id<vt.size()&&vt[id].ff<=t)
      |                ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 51204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 51156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 51156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 51156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2087 ms 93476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2087 ms 93476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 51204 KB Output isn't correct
2 Halted 0 ms 0 KB -