Submission #680371

# Submission time Handle Problem Language Result Execution time Memory
680371 2023-01-10T16:52:47 Z pls33 Race (IOI11_race) C++17
100 / 100
965 ms 49512 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifndef _AAAAAAAAA
#include "race.h"
#endif

using namespace std;
using namespace __gnu_pbds;

#pragma region dalykai
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;
#pragma endregion

using state_t = bitset<(int)2e5>;
using map_t = map<int, int>;
void merge_maps(map_t &path, map_t &new_path)
{
    if (path.size() < new_path.size())
    {
        new_path.swap(path);
    }

    for (auto &[sum, len] : new_path)
    {
        auto it = path.find(sum);

        if (it == path.end())
        {
            path.emplace(sum, len);
        }
        else
        {
            it->second = min(it->second, len);
        }
    }
}

void find_size(int v, int p,
               vi32 &size, vvp32 &adj, state_t &deleted)
{
    if (deleted[v])
    {
        size[v] = 0;
        return;
    }

    size[v] = 1;
    for (auto &[next, len] : adj[v])
    {
        if (next == p || deleted[v])
        {
            continue;
        }

        find_size(next, v, size, adj, deleted);
        size[v] += size[next];
    }
}

void find_centroid(int v, int p, int needed,
                   int &centroid,
                   vi32 &size, vvp32 &adj, state_t &deleted)
{
    if (deleted[v])
    {
        return;
    }

    bool ok = true;
    for (auto &[next, len] : adj[v])
    {
        if (size[next] <= needed || next == p || deleted[next])
        {
            continue;
        }

        find_centroid(next, v, needed, centroid, size, adj, deleted);
        ok = false;
        break;
    }

    if (centroid == -1 && ok)
    {
        centroid = v;
    }
}

int update_length(int sum, int length, map_t &map)
{
    auto it = map.find(sum);

    if (it == map.end())
    {
        map[sum] = length;
        return length;
    }

    it->second = min(it->second, length);
    return length;
}

void find_paths(int v, int p, int sum, int length, int K,
                int &shortest,
                map_t &path, map_t &new_path, vvp32 &adj, state_t &deleted)
{
    if (sum > K || deleted[v])
    {
        return;
    }

    if (sum == K)
    {
        shortest = min(shortest, length);
        return;
    }

    int cur_length = update_length(sum, length, new_path);
    auto it = path.find(K - sum);

    if (it != path.end())
    {
        shortest = min(shortest, it->second + cur_length);
    }

    for (auto &[next, len] : adj[v])
    {
        if (next == p || deleted[next])
        {
            continue;
        }

        find_paths(next, v, sum + len, length + 1, K,
                   shortest,
                   path, new_path, adj, deleted);
    }
}

void eval_centroid(int v, int K,
                   int &shortest,
                   vi32 &size, vvp32 &adj, state_t &deleted)
{
    find_size(v, -1, size, adj, deleted);
    if (size[v] < 2)
    {
        return;
    }

    int centroid = -1;
    find_centroid(v, -1, size[v] / 2, centroid, size, adj, deleted);

    deleted[centroid] = true;
    map_t path;
    for (auto &[next, len] : adj[centroid])
    {
        if (deleted[next])
        {
            continue;
        }

        map_t new_path;
        find_paths(next, -1, len, 1, K, shortest, path, new_path, adj, deleted);
        merge_maps(path, new_path);
    }

    for (auto &[next, len] : adj[centroid])
    {
        if (deleted[next])
        {
            continue;
        }
        eval_centroid(next, K, shortest, size, adj, deleted);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
    vvp32 adj(N);
    for (int i = 0; i < N - 1; i++)
    {
        adj[H[i][0]].emplace_back(H[i][1], L[i]);
        adj[H[i][1]].emplace_back(H[i][0], L[i]);
    }

    int shortest = N + 1;
    vi32 size(N);
    state_t deleted;
    eval_centroid(0, K, shortest, size, adj, deleted);

    return shortest == N + 1 ? -1 : shortest;
}

#ifdef _AAAAAAAAA
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];

void read_input()
{
    int i;
    scanf("%d %d", &N, &K);
    for (i = 0; i < N - 1; i++)
        scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
}

int main()
{
    freopen("race.in", "r", stdin);
    int ans;
    read_input();
    ans = best_path(N, K, H, L);
    printf("%d\n", ans);
    return 0;
}
#endif

Compilation message

race.cpp:12: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
   12 | #pragma region dalykai
      | 
race.cpp:35: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
   35 | #pragma endregion
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 184 ms 7940 KB Output is correct
20 Correct 174 ms 7944 KB Output is correct
21 Correct 165 ms 7844 KB Output is correct
22 Correct 166 ms 7912 KB Output is correct
23 Correct 82 ms 8160 KB Output is correct
24 Correct 72 ms 7952 KB Output is correct
25 Correct 132 ms 12264 KB Output is correct
26 Correct 94 ms 16756 KB Output is correct
27 Correct 195 ms 15712 KB Output is correct
28 Correct 275 ms 33120 KB Output is correct
29 Correct 291 ms 31720 KB Output is correct
30 Correct 208 ms 15804 KB Output is correct
31 Correct 201 ms 15708 KB Output is correct
32 Correct 216 ms 15712 KB Output is correct
33 Correct 293 ms 14672 KB Output is correct
34 Correct 172 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 456 KB Output is correct
39 Correct 184 ms 7940 KB Output is correct
40 Correct 174 ms 7944 KB Output is correct
41 Correct 165 ms 7844 KB Output is correct
42 Correct 166 ms 7912 KB Output is correct
43 Correct 82 ms 8160 KB Output is correct
44 Correct 72 ms 7952 KB Output is correct
45 Correct 132 ms 12264 KB Output is correct
46 Correct 94 ms 16756 KB Output is correct
47 Correct 195 ms 15712 KB Output is correct
48 Correct 275 ms 33120 KB Output is correct
49 Correct 291 ms 31720 KB Output is correct
50 Correct 208 ms 15804 KB Output is correct
51 Correct 201 ms 15708 KB Output is correct
52 Correct 216 ms 15712 KB Output is correct
53 Correct 293 ms 14672 KB Output is correct
54 Correct 172 ms 14540 KB Output is correct
55 Correct 19 ms 1472 KB Output is correct
56 Correct 11 ms 1108 KB Output is correct
57 Correct 119 ms 8168 KB Output is correct
58 Correct 40 ms 8060 KB Output is correct
59 Correct 309 ms 23784 KB Output is correct
60 Correct 965 ms 49512 KB Output is correct
61 Correct 266 ms 15880 KB Output is correct
62 Correct 278 ms 15788 KB Output is correct
63 Correct 338 ms 15932 KB Output is correct
64 Correct 948 ms 26592 KB Output is correct
65 Correct 204 ms 15648 KB Output is correct
66 Correct 623 ms 30016 KB Output is correct
67 Correct 123 ms 15952 KB Output is correct
68 Correct 418 ms 22656 KB Output is correct
69 Correct 405 ms 22868 KB Output is correct
70 Correct 382 ms 21908 KB Output is correct