Submission #99410

# Submission time Handle Problem Language Result Execution time Memory
99410 2019-03-03T16:34:46 Z naoai Race (IOI11_race) C++14
43 / 100
399 ms 71572 KB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;

#define fin cin
#define fout cout
//ifstream fin("x.in"); ofstream fout("x.out");

typedef long long i64;
const int nmax = 2e5;

static i64 K;
static int ans;
bool viz[nmax + 1];
int lant[nmax + 1], sz[nmax + 1];
i64 ct[nmax + 1];

vector<pair<int, int>> g[nmax + 1];
set<pair<i64, int>> s[nmax + 1];

void dfs (int nod, int h, int dad = -1) {
    viz[nod] = 1;
    sz[nod] = 1;

    int mxf = -1, mxg = -1, valm = 0;

    for (auto i : g[nod]) {
        if (viz[i.first] == 0) {
            dfs(i.first, h + 1, nod);

            sz[nod] += sz[i.first];
            if (sz[i.first] > mxg) {
                mxg = sz[i.first];
                mxf = i.first;
                valm = i.second;
            }
        }
    }

    if (mxf == -1) {
        lant[nod] = nod;
    } else {
        lant[nod] = lant[mxf];
    }

    ct[lant[nod]] += valm;

    set<pair<i64, int>>::iterator it = s[lant[nod]].lower_bound({K - ct[lant[nod]] - 0, 0});
    if (it != s[lant[nod]].end() && (it -> first) == K - ct[lant[nod]]) {
        ans = min(ans, (it -> second) - h);
    }
    s[lant[nod]].insert({-ct[lant[nod]], h});

    for (auto i : g[nod]) {
        if (i.first == mxf || i.first == dad)
            continue;

        int l = lant[i.first];
        ct[l] += i.second;
        while (!s[l].empty()) {
            pair<i64, int> x = *s[l].begin();
            s[l].erase(s[l].begin());

            x.first += ct[l];
            it = s[lant[nod]].lower_bound({K - ct[lant[nod]] - x.first, 0});
            if (it != s[lant[nod]].end() && (it -> first) == K - ct[lant[nod]] - x.first) {
                ans = min(ans, x.second + (it -> second) - 2 * h);
            }

            s[lant[nod]].insert({x.first - ct[lant[nod]], x.second});
        }
    }
}

int best_path (int n, int k, int h[][2], int l[]) {
    K = k;
    for (int i = 0; i < n - 1; ++ i) {
        g[h[i][0]].push_back({h[i][1], l[i]});
        g[h[i][1]].push_back({h[i][0], l[i]});
    }

    ans = n + 1;
    dfs(0, 0);

    if (ans == n + 1)
        ans = -1;
    return ans;
}

/*int main() {
    int a[11][2]= {0, 1,
0, 2,
2, 3,
3, 4,
4, 5,
0, 6,
6, 7,
6, 8,
8, 9,
8, 10};
    int b[11] = {3,4,5,4,6,3,2,5,6,7};

    //int a[3][2]= {0, 1, 1, 2, 1, 3 };
    //int b[3] = {1, 2, 4};
    cout << best_path (11, 12, a, b) << "\n";
    return 0;
}*/
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14592 KB Output is correct
2 Correct 18 ms 14464 KB Output is correct
3 Correct 15 ms 14464 KB Output is correct
4 Correct 14 ms 14552 KB Output is correct
5 Correct 16 ms 14464 KB Output is correct
6 Correct 17 ms 14464 KB Output is correct
7 Correct 15 ms 14464 KB Output is correct
8 Correct 15 ms 14436 KB Output is correct
9 Correct 16 ms 14464 KB Output is correct
10 Correct 16 ms 14464 KB Output is correct
11 Correct 16 ms 14464 KB Output is correct
12 Correct 16 ms 14464 KB Output is correct
13 Correct 17 ms 14464 KB Output is correct
14 Correct 15 ms 14464 KB Output is correct
15 Correct 16 ms 14464 KB Output is correct
16 Correct 18 ms 14464 KB Output is correct
17 Correct 16 ms 14464 KB Output is correct
18 Correct 17 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14592 KB Output is correct
2 Correct 18 ms 14464 KB Output is correct
3 Correct 15 ms 14464 KB Output is correct
4 Correct 14 ms 14552 KB Output is correct
5 Correct 16 ms 14464 KB Output is correct
6 Correct 17 ms 14464 KB Output is correct
7 Correct 15 ms 14464 KB Output is correct
8 Correct 15 ms 14436 KB Output is correct
9 Correct 16 ms 14464 KB Output is correct
10 Correct 16 ms 14464 KB Output is correct
11 Correct 16 ms 14464 KB Output is correct
12 Correct 16 ms 14464 KB Output is correct
13 Correct 17 ms 14464 KB Output is correct
14 Correct 15 ms 14464 KB Output is correct
15 Correct 16 ms 14464 KB Output is correct
16 Correct 18 ms 14464 KB Output is correct
17 Correct 16 ms 14464 KB Output is correct
18 Correct 17 ms 14464 KB Output is correct
19 Correct 18 ms 14464 KB Output is correct
20 Correct 16 ms 14464 KB Output is correct
21 Correct 18 ms 14592 KB Output is correct
22 Correct 16 ms 14592 KB Output is correct
23 Correct 18 ms 14592 KB Output is correct
24 Correct 18 ms 14672 KB Output is correct
25 Correct 20 ms 14592 KB Output is correct
26 Correct 16 ms 14592 KB Output is correct
27 Correct 17 ms 14592 KB Output is correct
28 Correct 20 ms 14632 KB Output is correct
29 Correct 16 ms 14592 KB Output is correct
30 Correct 16 ms 14592 KB Output is correct
31 Correct 17 ms 14592 KB Output is correct
32 Correct 15 ms 14592 KB Output is correct
33 Correct 20 ms 14564 KB Output is correct
34 Correct 20 ms 14592 KB Output is correct
35 Correct 16 ms 14592 KB Output is correct
36 Correct 17 ms 14592 KB Output is correct
37 Correct 16 ms 14592 KB Output is correct
38 Correct 16 ms 14644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14592 KB Output is correct
2 Correct 18 ms 14464 KB Output is correct
3 Correct 15 ms 14464 KB Output is correct
4 Correct 14 ms 14552 KB Output is correct
5 Correct 16 ms 14464 KB Output is correct
6 Correct 17 ms 14464 KB Output is correct
7 Correct 15 ms 14464 KB Output is correct
8 Correct 15 ms 14436 KB Output is correct
9 Correct 16 ms 14464 KB Output is correct
10 Correct 16 ms 14464 KB Output is correct
11 Correct 16 ms 14464 KB Output is correct
12 Correct 16 ms 14464 KB Output is correct
13 Correct 17 ms 14464 KB Output is correct
14 Correct 15 ms 14464 KB Output is correct
15 Correct 16 ms 14464 KB Output is correct
16 Correct 18 ms 14464 KB Output is correct
17 Correct 16 ms 14464 KB Output is correct
18 Correct 17 ms 14464 KB Output is correct
19 Correct 154 ms 22828 KB Output is correct
20 Correct 189 ms 22972 KB Output is correct
21 Correct 150 ms 22700 KB Output is correct
22 Correct 164 ms 22812 KB Output is correct
23 Correct 242 ms 23416 KB Output is correct
24 Correct 158 ms 22948 KB Output is correct
25 Correct 177 ms 35576 KB Output is correct
26 Correct 120 ms 42916 KB Output is correct
27 Correct 306 ms 36984 KB Output is correct
28 Correct 372 ms 71572 KB Output is correct
29 Correct 322 ms 69340 KB Output is correct
30 Correct 299 ms 36984 KB Output is correct
31 Correct 295 ms 37112 KB Output is correct
32 Correct 325 ms 37164 KB Output is correct
33 Correct 274 ms 29900 KB Output is correct
34 Correct 399 ms 42872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14592 KB Output is correct
2 Correct 18 ms 14464 KB Output is correct
3 Correct 15 ms 14464 KB Output is correct
4 Correct 14 ms 14552 KB Output is correct
5 Correct 16 ms 14464 KB Output is correct
6 Correct 17 ms 14464 KB Output is correct
7 Correct 15 ms 14464 KB Output is correct
8 Correct 15 ms 14436 KB Output is correct
9 Correct 16 ms 14464 KB Output is correct
10 Correct 16 ms 14464 KB Output is correct
11 Correct 16 ms 14464 KB Output is correct
12 Correct 16 ms 14464 KB Output is correct
13 Correct 17 ms 14464 KB Output is correct
14 Correct 15 ms 14464 KB Output is correct
15 Correct 16 ms 14464 KB Output is correct
16 Correct 18 ms 14464 KB Output is correct
17 Correct 16 ms 14464 KB Output is correct
18 Correct 17 ms 14464 KB Output is correct
19 Correct 18 ms 14464 KB Output is correct
20 Correct 16 ms 14464 KB Output is correct
21 Correct 18 ms 14592 KB Output is correct
22 Correct 16 ms 14592 KB Output is correct
23 Correct 18 ms 14592 KB Output is correct
24 Correct 18 ms 14672 KB Output is correct
25 Correct 20 ms 14592 KB Output is correct
26 Correct 16 ms 14592 KB Output is correct
27 Correct 17 ms 14592 KB Output is correct
28 Correct 20 ms 14632 KB Output is correct
29 Correct 16 ms 14592 KB Output is correct
30 Correct 16 ms 14592 KB Output is correct
31 Correct 17 ms 14592 KB Output is correct
32 Correct 15 ms 14592 KB Output is correct
33 Correct 20 ms 14564 KB Output is correct
34 Correct 20 ms 14592 KB Output is correct
35 Correct 16 ms 14592 KB Output is correct
36 Correct 17 ms 14592 KB Output is correct
37 Correct 16 ms 14592 KB Output is correct
38 Correct 16 ms 14644 KB Output is correct
39 Correct 154 ms 22828 KB Output is correct
40 Correct 189 ms 22972 KB Output is correct
41 Correct 150 ms 22700 KB Output is correct
42 Correct 164 ms 22812 KB Output is correct
43 Correct 242 ms 23416 KB Output is correct
44 Correct 158 ms 22948 KB Output is correct
45 Correct 177 ms 35576 KB Output is correct
46 Correct 120 ms 42916 KB Output is correct
47 Correct 306 ms 36984 KB Output is correct
48 Correct 372 ms 71572 KB Output is correct
49 Correct 322 ms 69340 KB Output is correct
50 Correct 299 ms 36984 KB Output is correct
51 Correct 295 ms 37112 KB Output is correct
52 Correct 325 ms 37164 KB Output is correct
53 Correct 274 ms 29900 KB Output is correct
54 Correct 399 ms 42872 KB Output is correct
55 Correct 30 ms 15716 KB Output is correct
56 Correct 25 ms 15232 KB Output is correct
57 Incorrect 103 ms 22264 KB Output isn't correct
58 Halted 0 ms 0 KB -