답안 #763529

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
763529 2023-06-22T12:14:33 Z adrilen 경주 (Race) (IOI11_race) C++17
100 / 100
684 ms 63836 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
typedef array<int, 2> arr;
typedef array<arr, 2> arrr;


constexpr int maxn = 2e5 + 5, maxk = 1e6 + 5;
int n, k;

basic_string <arr> adj[maxn];

int siz[maxn] = { 0 };
bool removed[maxn] = { 0 };

int fnd_size(int p, int par)
{
    siz[p] = 1;
    for (arr &i : adj[p])
    {
        if (i[0] == par || removed[i[0]]) continue;
        siz[p] += fnd_size(i[0], p);
    }
    return siz[p];
}

int fnd_centroid(int p, int par, int s)
{
    for (arr &i : adj[p])
    {
        if (i[0] == par || removed[i[0]]) continue;
        if (siz[i[0]] * 2 >= s) return fnd_centroid(i[0], p, s);
    }
    return p;
}
arrr values[maxk] = { 0 };

void fnd_lengths(int p, int par, set<int>&found, int key, int l, int depth)
{
    if (l > k) return ;

    found.insert(l);


    if (values[l][0][0] > depth)
    {
        if (values[l][0][1] != key) values[l][1] = values[l][0];
        values[l][0] = arr{depth, key};
    } else if (values[l][0][1] != key && values[l][1][0] > depth)
    {
        values[l][1] = arr{depth, key};
    }

    for (arr i : adj[p])
    {
        if (removed[i[0]] || i[0] == par) continue;

        fnd_lengths(i[0], p, found, key, l + i[1], depth + 1);
    }
}   

int output = 1e9;

void fix(int p)
{
    values[p] = arrr{arr{(int)2e9, -1}, arr{(int)2e9, -1}};
}

void centroid_decomp(int p)
{   
    fnd_size(p, -1);
    p = fnd_centroid(p, -1, siz[p]);

    removed[p] = true;


    /*
    Calculate
    */
    set <int> found = { 0 };
    int key = 0;
    for (const arr &i : adj[p])
    {
        if (removed[i[0]]) continue;
        fnd_lengths(i[0], p, found, key++, i[1], 1);
    }

    values[0][0] = arr{0, -2};
    
    auto it = found.begin();
    auto rt = found.rbegin();

    while (it != found.end() && rt != found.rend() && *it <= *rt)
    {
        if (*it + *rt < k) {fix(*it); it++; continue;}
        if (*it + *rt > k) {fix(*rt); rt++; continue;}

        // cout << "\t" << *it << " " << values[*it][0][0] << " " << *rt << " " << values[*rt][0][0] << "\n";

        if (values[*it][0][1] != values[*rt][0][1]) output = min(values[*it][0][0] + values[*rt][0][0], output);
        else {
            output = min({output, values[*it][0][0] + values[*rt][1][0], values[*rt][0][0] + values[*it][1][0]});
        }
        fix(*it); fix(*rt);
        it++, rt++;

    }
    
    // cout << p << " " << output << " " << found.size() << "\n";
    // for (int i : found) cout << i <<  " ";
    // cout << "\n";

    for (const arr &i : adj[p])
    {
        if (removed[i[0]]) continue;
        centroid_decomp(i[0]);       
    }
}


int best_path(int N, int K, int H[][2], int L[])
{
    n = N, k = K;

    for (int i = 0; i < n - 1; i++)
    {
        adj[H[i][0]].push_back(arr{H[i][1], L[i]});
        adj[H[i][1]].push_back(arr{H[i][0], L[i]});
    }

    for (int i = 0; i <= k; i++) fix(i);

    int root = 0;
    centroid_decomp(root);


    if (output == 1e9) return -1;

    return output;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6568 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 4 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6568 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6572 KB Output is correct
14 Correct 3 ms 6612 KB Output is correct
15 Correct 3 ms 6568 KB Output is correct
16 Correct 5 ms 6612 KB Output is correct
17 Correct 3 ms 6572 KB Output is correct
18 Correct 4 ms 6612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6568 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 4 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6568 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6572 KB Output is correct
14 Correct 3 ms 6612 KB Output is correct
15 Correct 3 ms 6568 KB Output is correct
16 Correct 5 ms 6612 KB Output is correct
17 Correct 3 ms 6572 KB Output is correct
18 Correct 4 ms 6612 KB Output is correct
19 Correct 3 ms 6484 KB Output is correct
20 Correct 4 ms 6484 KB Output is correct
21 Correct 5 ms 6576 KB Output is correct
22 Correct 11 ms 20948 KB Output is correct
23 Correct 9 ms 18400 KB Output is correct
24 Correct 9 ms 20180 KB Output is correct
25 Correct 11 ms 19796 KB Output is correct
26 Correct 6 ms 11832 KB Output is correct
27 Correct 9 ms 19156 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 7 ms 11732 KB Output is correct
30 Correct 8 ms 12476 KB Output is correct
31 Correct 10 ms 16852 KB Output is correct
32 Correct 10 ms 17828 KB Output is correct
33 Correct 11 ms 18872 KB Output is correct
34 Correct 9 ms 15796 KB Output is correct
35 Correct 11 ms 19344 KB Output is correct
36 Correct 9 ms 21204 KB Output is correct
37 Correct 11 ms 19124 KB Output is correct
38 Correct 9 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6568 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 4 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6568 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6572 KB Output is correct
14 Correct 3 ms 6612 KB Output is correct
15 Correct 3 ms 6568 KB Output is correct
16 Correct 5 ms 6612 KB Output is correct
17 Correct 3 ms 6572 KB Output is correct
18 Correct 4 ms 6612 KB Output is correct
19 Correct 104 ms 11976 KB Output is correct
20 Correct 117 ms 11980 KB Output is correct
21 Correct 110 ms 11936 KB Output is correct
22 Correct 98 ms 11836 KB Output is correct
23 Correct 61 ms 11952 KB Output is correct
24 Correct 51 ms 10796 KB Output is correct
25 Correct 102 ms 16168 KB Output is correct
26 Correct 67 ms 18880 KB Output is correct
27 Correct 138 ms 17148 KB Output is correct
28 Correct 188 ms 31708 KB Output is correct
29 Correct 243 ms 30804 KB Output is correct
30 Correct 115 ms 17236 KB Output is correct
31 Correct 129 ms 17240 KB Output is correct
32 Correct 143 ms 17320 KB Output is correct
33 Correct 190 ms 19120 KB Output is correct
34 Correct 123 ms 20072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6568 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 4 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6568 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6572 KB Output is correct
14 Correct 3 ms 6612 KB Output is correct
15 Correct 3 ms 6568 KB Output is correct
16 Correct 5 ms 6612 KB Output is correct
17 Correct 3 ms 6572 KB Output is correct
18 Correct 4 ms 6612 KB Output is correct
19 Correct 3 ms 6484 KB Output is correct
20 Correct 4 ms 6484 KB Output is correct
21 Correct 5 ms 6576 KB Output is correct
22 Correct 11 ms 20948 KB Output is correct
23 Correct 9 ms 18400 KB Output is correct
24 Correct 9 ms 20180 KB Output is correct
25 Correct 11 ms 19796 KB Output is correct
26 Correct 6 ms 11832 KB Output is correct
27 Correct 9 ms 19156 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 7 ms 11732 KB Output is correct
30 Correct 8 ms 12476 KB Output is correct
31 Correct 10 ms 16852 KB Output is correct
32 Correct 10 ms 17828 KB Output is correct
33 Correct 11 ms 18872 KB Output is correct
34 Correct 9 ms 15796 KB Output is correct
35 Correct 11 ms 19344 KB Output is correct
36 Correct 9 ms 21204 KB Output is correct
37 Correct 11 ms 19124 KB Output is correct
38 Correct 9 ms 14676 KB Output is correct
39 Correct 104 ms 11976 KB Output is correct
40 Correct 117 ms 11980 KB Output is correct
41 Correct 110 ms 11936 KB Output is correct
42 Correct 98 ms 11836 KB Output is correct
43 Correct 61 ms 11952 KB Output is correct
44 Correct 51 ms 10796 KB Output is correct
45 Correct 102 ms 16168 KB Output is correct
46 Correct 67 ms 18880 KB Output is correct
47 Correct 138 ms 17148 KB Output is correct
48 Correct 188 ms 31708 KB Output is correct
49 Correct 243 ms 30804 KB Output is correct
50 Correct 115 ms 17236 KB Output is correct
51 Correct 129 ms 17240 KB Output is correct
52 Correct 143 ms 17320 KB Output is correct
53 Correct 190 ms 19120 KB Output is correct
54 Correct 123 ms 20072 KB Output is correct
55 Correct 17 ms 7540 KB Output is correct
56 Correct 15 ms 7124 KB Output is correct
57 Correct 90 ms 11912 KB Output is correct
58 Correct 32 ms 10176 KB Output is correct
59 Correct 188 ms 28888 KB Output is correct
60 Correct 587 ms 63836 KB Output is correct
61 Correct 197 ms 17728 KB Output is correct
62 Correct 187 ms 33204 KB Output is correct
63 Correct 215 ms 33212 KB Output is correct
64 Correct 684 ms 37104 KB Output is correct
65 Correct 148 ms 18112 KB Output is correct
66 Correct 517 ms 45956 KB Output is correct
67 Correct 148 ms 30552 KB Output is correct
68 Correct 275 ms 39424 KB Output is correct
69 Correct 271 ms 39608 KB Output is correct
70 Correct 243 ms 38812 KB Output is correct