Submission #522096

# Submission time Handle Problem Language Result Execution time Memory
522096 2022-02-03T20:13:20 Z blue Road Closures (APIO21_roads) C++17
7 / 100
2000 ms 34772 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
#define sz(x) int(x.size())

const int mx = 100'000;
const ll INF = 1'000'000'000'000'000'000LL;


int N;
vi U, V, W;

vi edge[mx];
vi deg(mx, 0);
vi deg_occ[mx];

vi depth(mx);
vi parent(mx, -1);

ll wt_sum = 0;

vi par_wt(mx);



void dfs1(int u, int p)
{
    for(int e: edge[u])
    {
        int v = U[e] + V[e] - u;
        int w = W[e];
        if(v == p) continue;

        par_wt[v] = w;

        depth[v] = depth[u] + 1;
        parent[v] = u;
        dfs1(v, u);
    }
}





struct DS
{
    multiset<ll> values;

    ll minSum(int z)
    {
        if(z > sz(values)) return INF;

        if(z == 0) return 0;

        int ct = 0;
        ll res = 0;

        for(ll v : values)
        {
            res += v;
            ct++;
            if(ct == z) break;
        }

        return res;
    }

    void insert(ll v) 
    {
        values.insert(v);
    }

    int size()
    {
        return sz(values);
    }
};







vll minimum_closure_costs(int N_, vi U_, vi V_, vi W_)
{   
    N = N_;
    U = U_;
    V = V_;
    W = W_;

    for(int e = 0; e < N-1; e++)
    {
        edge[U[e]].push_back(e);
        edge[V[e]].push_back(e);

        deg[U[e]]++;
        deg[V[e]]++;

        wt_sum += W[e];
    }

    for(int i = 0; i < N; i++)
        deg_occ[deg[i]].push_back(i);

    for(int i = 0; i < N; i++)
    {
        sort(edge[i].begin(), edge[i].end(), [] (int e, int f)
        {
            return deg[U[e]] + deg[V[e]] > deg[U[f]] + deg[V[f]];
        });
    }


    dfs1(0, -1);

    set<pii> it_list;

    for(int i = 0; i < N; i++) it_list.insert({-depth[i], i});


    vll res(N, 0);

    res[0] = wt_sum;

                     // root deg <= K-1
    vll dp0(N), dp1(N);
    //root deg <= K

    DS extra_edges[N];

    vi intdeg = deg;
    for(int i = 1; i < N; i++)
        intdeg[i]--;

    for(int k = 1; k < N; k++)
    {
        cerr << "\n\n\n\n\n";
        cerr << "k = " << k << '\n';
        for(int u: deg_occ[k]) //deg_occ[0] is empty
        {
            // cerr << "eliminating " << u << '\n';
            it_list.erase({-depth[u], u});
            for(int e : edge[u])
            {
                int v = U[e] + V[e] - u;
                int w = W[e];
                if(parent[v] != u)
                    extra_edges[v].insert(w);
                // cerr << "extra edges " << v << " -> insert " << w << '\n';
            }
        }

        for(auto p: it_list)
        {
            // cerr << "\n\n";
            int u = p.second;
            // cerr << "u = " << u << '\n';

            // cerr << "ee = ";
            // for(ll y : extra_edges[u].values) cerr << y << ' ';
                // cerr << '\n';

            dp0[u] = dp1[u] = INF;

            ll basicCost = 0;

            vector<ll> upgrades;

            for(auto e: edge[u])
            {
                int v = U[e] + V[e] - u;
                ll w = W[e];

                if(v == parent[u]) continue;

                if(deg[v] <= k) break;

                basicCost += dp1[v];
                upgrades.push_back(dp0[v] + w - dp1[v]);
            }

            sort(upgrades.begin(), upgrades.end());

            ll upgrade_total = 0;

            dp0[u] = min(dp0[u], basicCost + extra_edges[u].minSum(intdeg[u] - k));
            dp1[u] = min(dp1[u], basicCost + extra_edges[u].minSum(intdeg[u] - (k-1)));

            // cerr << basicCost << '\n';

            // cerr << "ee size = " << sz(extra_edges[u]) << ", query val = " << intdeg[u] - k << '\n';




            for(int cct = 0; cct < sz(upgrades); cct++)
            {
                // cerr << "cct = " << cct << '\n';
                upgrade_total += upgrades[cct];
                dp0[u] = min(dp0[u], basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - k - (cct+1)));
                dp1[u] = min(dp1[u], basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - (k-1) - (cct+1)));
            }

            if(u == 0) 
            {
                    // cerr << "adding " << dp0[u] << " a to res " << k << '\n'; 
                res[k] += dp0[u];
            }
            else if(deg[parent[u]] <= k)
            {
                // cerr << "adding " << min(dp1[u], dp0[u] + par_wt[u]) << " b to res " << k << '\n'; 
                res[k] += min(dp1[u], dp0[u] + par_wt[u]);
            }

            cerr << u << " : " << parent[u] << ' ' << dp0[u] << ' ' << dp1[u] << '\n';
        }
    }


    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6860 KB Output is correct
2 Correct 60 ms 7364 KB Output is correct
3 Correct 84 ms 7368 KB Output is correct
4 Correct 55 ms 7356 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 6 ms 7016 KB Output is correct
7 Correct 7 ms 6988 KB Output is correct
8 Correct 46 ms 7316 KB Output is correct
9 Correct 59 ms 7384 KB Output is correct
10 Correct 6 ms 7000 KB Output is correct
11 Correct 7 ms 6988 KB Output is correct
12 Execution timed out 2066 ms 18652 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6860 KB Output is correct
2 Correct 1174 ms 30156 KB Output is correct
3 Correct 1314 ms 32860 KB Output is correct
4 Correct 1401 ms 34704 KB Output is correct
5 Correct 1409 ms 34772 KB Output is correct
6 Correct 28 ms 7372 KB Output is correct
7 Correct 31 ms 7480 KB Output is correct
8 Correct 29 ms 7400 KB Output is correct
9 Correct 6 ms 6988 KB Output is correct
10 Correct 6 ms 6996 KB Output is correct
11 Correct 7 ms 6980 KB Output is correct
12 Correct 839 ms 22544 KB Output is correct
13 Correct 1399 ms 33044 KB Output is correct
14 Correct 3 ms 6860 KB Output is correct
15 Correct 1265 ms 30320 KB Output is correct
16 Correct 1413 ms 32956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6860 KB Output is correct
2 Correct 3 ms 6860 KB Output is correct
3 Correct 4 ms 6860 KB Output is correct
4 Correct 5 ms 6988 KB Output is correct
5 Incorrect 6 ms 6988 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6860 KB Output is correct
2 Correct 3 ms 6860 KB Output is correct
3 Correct 4 ms 6860 KB Output is correct
4 Correct 5 ms 6988 KB Output is correct
5 Incorrect 6 ms 6988 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1501 ms 28316 KB Output is correct
2 Correct 1484 ms 27936 KB Output is correct
3 Execution timed out 2081 ms 29624 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1501 ms 28316 KB Output is correct
2 Correct 1484 ms 27936 KB Output is correct
3 Execution timed out 2081 ms 29624 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6860 KB Output is correct
2 Correct 60 ms 7364 KB Output is correct
3 Correct 84 ms 7368 KB Output is correct
4 Correct 55 ms 7356 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 6 ms 7016 KB Output is correct
7 Correct 7 ms 6988 KB Output is correct
8 Correct 46 ms 7316 KB Output is correct
9 Correct 59 ms 7384 KB Output is correct
10 Correct 6 ms 7000 KB Output is correct
11 Correct 7 ms 6988 KB Output is correct
12 Execution timed out 2066 ms 18652 KB Time limit exceeded
13 Halted 0 ms 0 KB -