Submission #413083

# Submission time Handle Problem Language Result Execution time Memory
413083 2021-05-28T08:18:14 Z ngpin04 Race (IOI11_race) C++14
100 / 100
499 ms 93624 KB
#include <bits/stdc++.h>
#include "race.h"
//#include "grader.cpp"
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int Nmax = 2e5 + 5;

vector <pair <int, int>> adj[Nmax];

map <long long, int> s[Nmax];

long long val[Nmax];

int cnt[Nmax];
int k, ans = 1e9;

void dfs(int u, int p) {
    int vmax = u;
    for (pair <int, int> e : adj[u]){
        int v = e.fi;
        int w = e.se;
        if (v == p)
            continue;
        dfs(v, u);
        if (s[v].size() > s[vmax].size()) {
            vmax = v;
            cnt[u] = cnt[vmax] + 1;
            val[u] = val[vmax] + w;
        }
    }

    swap(s[u], s[vmax]);
    if (!s[u].count(-val[u]))
        s[u][-val[u]] = -cnt[u];
    else 
        s[u][-val[u]] = min(s[u][-val[u]], -cnt[u]);

    if (s[u].count(k - val[u]))
        ans = min(ans, s[u][k - val[u]] + cnt[u]);

    if (vmax == u) 
        return;

    for (pair <int, int> e : adj[u]) {
        int v = e.fi;
        int w = e.se;   
        if (v == vmax || v == p) 
            continue;
        for (pair <long long, int> pir : s[v]) {
            long long res = k - (pir.fi + val[v] + w) - val[u];
            if (s[u].count(res))
                ans = min(ans, (s[u][res] + cnt[u]) + (pir.se + cnt[v]) + 1);
        }
        
        for (pair <long long, int> pir : s[v]) {
            long long res = (pir.fi + val[v] + w) - val[u];
            int rescnt = (pir.se + cnt[v] + 1) - cnt[u];
            if (!s[u].count(res))
                s[u][res] = rescnt;
            else 
                s[u][res] = min(s[u][res], rescnt);
        }   
    }

    // cout << "Path " << u << ": \n";
    // for (pair <long long, int> pir : s[u]) 
    //     cout << pir.fi + val[u] << " " << pir.se + cnt[u] << "\n";
}

int best_path(int N, int K, int H[][2], int L[])
{
    k = K;
    for (int i = 0; i < N - 1; i++) {   
        int u = H[i][0];
        int v = H[i][1];
        adj[u].push_back(mp(v, L[i]));
        adj[v].push_back(mp(u, L[i]));
    }

    dfs(0, -1);
    if (ans == 1e9)
        ans = -1;

    return ans;                                                                               
}

# Verdict Execution time Memory Grader output
1 Correct 9 ms 14540 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14372 KB Output is correct
7 Correct 8 ms 14348 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 10 ms 14360 KB Output is correct
13 Correct 8 ms 14412 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14392 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 8 ms 14380 KB Output is correct
18 Correct 8 ms 14368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14540 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14372 KB Output is correct
7 Correct 8 ms 14348 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 10 ms 14360 KB Output is correct
13 Correct 8 ms 14412 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14392 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 8 ms 14380 KB Output is correct
18 Correct 8 ms 14368 KB Output is correct
19 Correct 8 ms 14372 KB Output is correct
20 Correct 10 ms 14380 KB Output is correct
21 Correct 10 ms 14508 KB Output is correct
22 Correct 10 ms 14656 KB Output is correct
23 Correct 11 ms 14668 KB Output is correct
24 Correct 10 ms 14608 KB Output is correct
25 Correct 10 ms 14668 KB Output is correct
26 Correct 10 ms 14668 KB Output is correct
27 Correct 11 ms 14540 KB Output is correct
28 Correct 10 ms 14656 KB Output is correct
29 Correct 10 ms 14664 KB Output is correct
30 Correct 10 ms 14668 KB Output is correct
31 Correct 10 ms 14664 KB Output is correct
32 Correct 10 ms 14664 KB Output is correct
33 Correct 10 ms 14692 KB Output is correct
34 Correct 9 ms 14540 KB Output is correct
35 Correct 9 ms 14596 KB Output is correct
36 Correct 10 ms 14532 KB Output is correct
37 Correct 9 ms 14540 KB Output is correct
38 Correct 9 ms 14528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14540 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14372 KB Output is correct
7 Correct 8 ms 14348 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 10 ms 14360 KB Output is correct
13 Correct 8 ms 14412 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14392 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 8 ms 14380 KB Output is correct
18 Correct 8 ms 14368 KB Output is correct
19 Correct 124 ms 32704 KB Output is correct
20 Correct 139 ms 32676 KB Output is correct
21 Correct 123 ms 32664 KB Output is correct
22 Correct 135 ms 32892 KB Output is correct
23 Correct 221 ms 46132 KB Output is correct
24 Correct 163 ms 39144 KB Output is correct
25 Correct 100 ms 30904 KB Output is correct
26 Correct 64 ms 38336 KB Output is correct
27 Correct 248 ms 42400 KB Output is correct
28 Correct 269 ms 75280 KB Output is correct
29 Correct 290 ms 73612 KB Output is correct
30 Correct 225 ms 42296 KB Output is correct
31 Correct 261 ms 42376 KB Output is correct
32 Correct 279 ms 42564 KB Output is correct
33 Correct 252 ms 41132 KB Output is correct
34 Correct 379 ms 74024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14540 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14372 KB Output is correct
7 Correct 8 ms 14348 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 10 ms 14360 KB Output is correct
13 Correct 8 ms 14412 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14392 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 8 ms 14380 KB Output is correct
18 Correct 8 ms 14368 KB Output is correct
19 Correct 8 ms 14372 KB Output is correct
20 Correct 10 ms 14380 KB Output is correct
21 Correct 10 ms 14508 KB Output is correct
22 Correct 10 ms 14656 KB Output is correct
23 Correct 11 ms 14668 KB Output is correct
24 Correct 10 ms 14608 KB Output is correct
25 Correct 10 ms 14668 KB Output is correct
26 Correct 10 ms 14668 KB Output is correct
27 Correct 11 ms 14540 KB Output is correct
28 Correct 10 ms 14656 KB Output is correct
29 Correct 10 ms 14664 KB Output is correct
30 Correct 10 ms 14668 KB Output is correct
31 Correct 10 ms 14664 KB Output is correct
32 Correct 10 ms 14664 KB Output is correct
33 Correct 10 ms 14692 KB Output is correct
34 Correct 9 ms 14540 KB Output is correct
35 Correct 9 ms 14596 KB Output is correct
36 Correct 10 ms 14532 KB Output is correct
37 Correct 9 ms 14540 KB Output is correct
38 Correct 9 ms 14528 KB Output is correct
39 Correct 124 ms 32704 KB Output is correct
40 Correct 139 ms 32676 KB Output is correct
41 Correct 123 ms 32664 KB Output is correct
42 Correct 135 ms 32892 KB Output is correct
43 Correct 221 ms 46132 KB Output is correct
44 Correct 163 ms 39144 KB Output is correct
45 Correct 100 ms 30904 KB Output is correct
46 Correct 64 ms 38336 KB Output is correct
47 Correct 248 ms 42400 KB Output is correct
48 Correct 269 ms 75280 KB Output is correct
49 Correct 290 ms 73612 KB Output is correct
50 Correct 225 ms 42296 KB Output is correct
51 Correct 261 ms 42376 KB Output is correct
52 Correct 279 ms 42564 KB Output is correct
53 Correct 252 ms 41132 KB Output is correct
54 Correct 379 ms 74024 KB Output is correct
55 Correct 25 ms 17220 KB Output is correct
56 Correct 17 ms 15848 KB Output is correct
57 Correct 83 ms 30508 KB Output is correct
58 Correct 64 ms 26816 KB Output is correct
59 Correct 104 ms 44612 KB Output is correct
60 Correct 266 ms 73936 KB Output is correct
61 Correct 288 ms 45892 KB Output is correct
62 Correct 211 ms 42400 KB Output is correct
63 Correct 273 ms 42472 KB Output is correct
64 Correct 499 ms 92956 KB Output is correct
65 Correct 473 ms 93624 KB Output is correct
66 Correct 290 ms 70260 KB Output is correct
67 Correct 228 ms 42836 KB Output is correct
68 Correct 372 ms 61356 KB Output is correct
69 Correct 425 ms 61804 KB Output is correct
70 Correct 376 ms 59192 KB Output is correct