Submission #1072782

# Submission time Handle Problem Language Result Execution time Memory
1072782 2024-08-24T04:34:52 Z jer033 Petrol stations (CEOI24_stations) C++17
36 / 100
40 ms 14424 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pil = pair<int, ll>;

vector<ll> answers;

void root_and_fill(vector<vector<pil>> (&edges), int root, ll K)
{
    int N = edges.size();
    vector<bool> visited(N, 0);
    visited[root] = 1;
    vector<int> visitlist;
    visitlist.push_back(root);
    vector<int> parent(N, -1);
    int vsize = 1;
    int curri = 0;
    while (curri<vsize)
    {
        int curr_city = visitlist[curri];
        curri++;
        for (pil neigh: edges[curr_city])
        {
            int a = neigh.first;
            if (visited[a] == 0)
            {
                visited[a] = 1;
                visitlist.push_back(a);
                vsize++;
                parent[a] = curr_city;
            }
        }
    }
    vector<ll> degree(N, 1);
    for (int i=N-1; i>=1; i--)
    {
        int curr_city = visitlist[i];
        degree[parent[curr_city]] += degree[curr_city];
    }

    vector<ll> tanks(N, K);
    for (int i=1; i<N; i++)
    {
        int curr_city = visitlist[i];
        ll cost = 0;
        for (pil neigh: edges[curr_city])
        {
            if (neigh.first == parent[curr_city])
                cost = neigh.second;
        }
        tanks[curr_city] = tanks[parent[curr_city]];
        tanks[curr_city] -= cost;
        if (tanks[curr_city] < 0ll)
        {
            tanks[curr_city] = K-cost;
            answers[parent[curr_city]] += degree[curr_city];
        }
    }
}

void line(vector<vector<pil>> (&edges), int endpt, ll K)
{
    ll N = edges.size();
    vector<int> nodes;
    vector<ll> weights;//will have size N-1, access until N-2

    //collapsed at "while (good)"
    //creates the line
    nodes.push_back(endpt);
    int prev = -1;
    int curr = endpt;
    bool good = 1;
    while (good)
    {
        good = 0;
        int nex = -1;
        for (pil x: edges[curr])
        {
            int y = x.first;
            ll z = x.second;
            if (y!=prev)
            {
                nex = y;
                nodes.push_back(y);
                weights.push_back(z);
            }
        }
        if (nex!=-1)
        {
            good = 1;
            prev = curr;
            curr = nex;
        }
    }

    vector<ll> nex_refill(N, -1);
    ll i = 0;
    ll j = 0;
    ll currsum = 0;
    good = 1;//this is a bool value that I am reusing
    while (good)
    {
        if (currsum<=K)
        {
            if (j<=(N-2ll))
            {
                currsum+=weights[j];
                j++;
            }
            else
                good = 0;
        }
        else
        {
            currsum-=weights[i];
            nex_refill[i] = j-1;
            i++;
            //cout << "next refill of " << i << "  is  " << j-1 << '\n';
        }
    }

    vector<ll> refill_count(N, 0);
    for (ll r=0; r<N; r++)
    {
        if (nex_refill[r] != -1)
            refill_count[nex_refill[r]] += (refill_count[r]+1ll);
        ll add_times = refill_count[r] * (N-r-1ll);
        answers[nodes[r]] += add_times;
    }
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    answers = vector<ll> (N, 0);
    vector<vector<pil>> edges(N);
    for (ll i=0; i<(N-1); i++)
    {
        int u, v; ll L;
        cin >> u >> v >> L;
        edges[u].push_back({v, L});
        edges[v].push_back({u, L});
    }

    if ((N<=1000ll) and (K<=1000ll))
    {
        for (ll i = 0; i<N; i++)
            root_and_fill(edges, i, K);
        for (ll i = 0; i<N; i++)
            cout << answers[i] << '\n';
    }
    else
    {
        for (ll i = 0; i<N; i++)
            if (edges[i].size() == 1)
                line(edges, i, K);
        for (ll i = 0; i<N; i++)
            cout << answers[i] << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 12 ms 556 KB Output is correct
4 Correct 19 ms 348 KB Output is correct
5 Correct 17 ms 348 KB Output is correct
6 Correct 22 ms 348 KB Output is correct
7 Correct 21 ms 584 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 15 ms 576 KB Output is correct
10 Correct 15 ms 584 KB Output is correct
11 Correct 18 ms 348 KB Output is correct
12 Correct 14 ms 344 KB Output is correct
13 Correct 15 ms 348 KB Output is correct
14 Correct 10 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 32 ms 9548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 32 ms 9548 KB Output is correct
5 Correct 40 ms 10056 KB Output is correct
6 Correct 40 ms 9796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 27 ms 14424 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 27 ms 14424 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 12 ms 556 KB Output is correct
4 Correct 19 ms 348 KB Output is correct
5 Correct 17 ms 348 KB Output is correct
6 Correct 22 ms 348 KB Output is correct
7 Correct 21 ms 584 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 15 ms 576 KB Output is correct
10 Correct 15 ms 584 KB Output is correct
11 Correct 18 ms 348 KB Output is correct
12 Correct 14 ms 344 KB Output is correct
13 Correct 15 ms 348 KB Output is correct
14 Correct 10 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 32 ms 9548 KB Output is correct
17 Correct 40 ms 10056 KB Output is correct
18 Correct 40 ms 9796 KB Output is correct
19 Runtime error 27 ms 14424 KB Execution killed with signal 11
20 Halted 0 ms 0 KB -