Submission #486996

# Submission time Handle Problem Language Result Execution time Memory
486996 2021-11-13T17:47:37 Z noob_c0de Race (IOI11_race) C++17
100 / 100
560 ms 85392 KB
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define ar array
#define db double
#define sz(x) (int)x.size()
#define mat vector<vector<int> >
const db pi = acos(-1);
const int mxn = 2e5 + 7, INF = 1e6 + 7;;
int n, k, ans;
vector<ar<int, 2> > g[mxn];
int depth[mxn], len[mxn];
map<int, int> mp[mxn];
int r[mxn];
// int h[mxn][2];
// int l[mxn];
void dfs(int u, int p)
{
    for (auto [v, w] : g[u])
    {
        if (v == p) continue;
        depth[v] = depth[u] + 1;
        len[v] = len[u] + w;
        dfs(v, u);
    }
}
void gopset(int u, int v)
{
    if (sz(mp[r[u]]) < sz(mp[r[v]])) swap(r[u], r[v]);
    for (auto it = mp[r[v]].begin(); it != mp[r[v]].end(); it++)
    {
        int t = it->first, dep = it->second;
        if (mp[r[u]].count(k - t + 2 * len[u]))
        {
            ans = min(ans, mp[r[u]][k - t + 2 * len[u]] + dep - 2 * depth[u]);
        }
    }
    for (auto it = mp[r[v]].begin(); it != mp[r[v]].end(); it++)
    {
        int t = it->first, dep = it->second;
        if (mp[r[u]].count(t)) mp[r[u]][t] = min(mp[r[u]][t], dep);
        else mp[r[u]][t] = dep;   
    }
}
void dfs2(int u, int p)
{
    mp[u][len[u]] = depth[u];
    for (auto [v, w] : g[u])
    {
        if (v == p) continue;
        dfs2(v, u);
        gopset(u, v);
    }
}
int best_path(int N, int K, int h[][2], int l[])
{
    n = N; 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]});
    }
    for (int i = 0; i < n; i++) r[i] = i;
    dfs(0, 0);
    ans = INF; 
    dfs2(0, 0);
    if (ans == INF) return -1;
    return ans;
}
// signed main()
// {
//     ios::sync_with_stdio(0);
//     cin.tie(0); cout.tie(0);
//     // De nghi ko bat ultra instinct tay nhanh hon nao luc lam bai
//     cin >> n >> k;
//     for (int i = 0; i < n - 1; i++) cin >> h[i][0] >> h[i][1];
//     for (int i = 0; i < n - 1; i++) cin >> l[i];
//     int res = best_path(n, k, h, l);
//     if (res == INF) cout << -1;
//     else cout << res;
//     return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14420 KB Output is correct
2 Correct 11 ms 14400 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 11 ms 14400 KB Output is correct
7 Correct 8 ms 14424 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14396 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14412 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 14412 KB Output is correct
16 Correct 10 ms 14396 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14420 KB Output is correct
2 Correct 11 ms 14400 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 11 ms 14400 KB Output is correct
7 Correct 8 ms 14424 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14396 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14412 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 14412 KB Output is correct
16 Correct 10 ms 14396 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 10 ms 14368 KB Output is correct
21 Correct 9 ms 14540 KB Output is correct
22 Correct 11 ms 14580 KB Output is correct
23 Correct 9 ms 14640 KB Output is correct
24 Correct 9 ms 14660 KB Output is correct
25 Correct 8 ms 14668 KB Output is correct
26 Correct 9 ms 14664 KB Output is correct
27 Correct 8 ms 14540 KB Output is correct
28 Correct 8 ms 14640 KB Output is correct
29 Correct 11 ms 14528 KB Output is correct
30 Correct 13 ms 14668 KB Output is correct
31 Correct 13 ms 14620 KB Output is correct
32 Correct 8 ms 14540 KB Output is correct
33 Correct 10 ms 14664 KB Output is correct
34 Correct 12 ms 14512 KB Output is correct
35 Correct 11 ms 14540 KB Output is correct
36 Correct 9 ms 14500 KB Output is correct
37 Correct 8 ms 14540 KB Output is correct
38 Correct 9 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14420 KB Output is correct
2 Correct 11 ms 14400 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 11 ms 14400 KB Output is correct
7 Correct 8 ms 14424 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14396 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14412 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 14412 KB Output is correct
16 Correct 10 ms 14396 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 141 ms 33472 KB Output is correct
20 Correct 177 ms 33428 KB Output is correct
21 Correct 149 ms 33272 KB Output is correct
22 Correct 218 ms 33016 KB Output is correct
23 Correct 194 ms 42616 KB Output is correct
24 Correct 216 ms 35592 KB Output is correct
25 Correct 153 ms 30568 KB Output is correct
26 Correct 101 ms 33752 KB Output is correct
27 Correct 202 ms 43824 KB Output is correct
28 Correct 293 ms 62784 KB Output is correct
29 Correct 299 ms 62280 KB Output is correct
30 Correct 222 ms 43788 KB Output is correct
31 Correct 249 ms 43808 KB Output is correct
32 Correct 331 ms 43940 KB Output is correct
33 Correct 297 ms 47104 KB Output is correct
34 Correct 395 ms 71964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14420 KB Output is correct
2 Correct 11 ms 14400 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 11 ms 14400 KB Output is correct
7 Correct 8 ms 14424 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 8 ms 14396 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14412 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 14412 KB Output is correct
16 Correct 10 ms 14396 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 10 ms 14368 KB Output is correct
21 Correct 9 ms 14540 KB Output is correct
22 Correct 11 ms 14580 KB Output is correct
23 Correct 9 ms 14640 KB Output is correct
24 Correct 9 ms 14660 KB Output is correct
25 Correct 8 ms 14668 KB Output is correct
26 Correct 9 ms 14664 KB Output is correct
27 Correct 8 ms 14540 KB Output is correct
28 Correct 8 ms 14640 KB Output is correct
29 Correct 11 ms 14528 KB Output is correct
30 Correct 13 ms 14668 KB Output is correct
31 Correct 13 ms 14620 KB Output is correct
32 Correct 8 ms 14540 KB Output is correct
33 Correct 10 ms 14664 KB Output is correct
34 Correct 12 ms 14512 KB Output is correct
35 Correct 11 ms 14540 KB Output is correct
36 Correct 9 ms 14500 KB Output is correct
37 Correct 8 ms 14540 KB Output is correct
38 Correct 9 ms 14532 KB Output is correct
39 Correct 141 ms 33472 KB Output is correct
40 Correct 177 ms 33428 KB Output is correct
41 Correct 149 ms 33272 KB Output is correct
42 Correct 218 ms 33016 KB Output is correct
43 Correct 194 ms 42616 KB Output is correct
44 Correct 216 ms 35592 KB Output is correct
45 Correct 153 ms 30568 KB Output is correct
46 Correct 101 ms 33752 KB Output is correct
47 Correct 202 ms 43824 KB Output is correct
48 Correct 293 ms 62784 KB Output is correct
49 Correct 299 ms 62280 KB Output is correct
50 Correct 222 ms 43788 KB Output is correct
51 Correct 249 ms 43808 KB Output is correct
52 Correct 331 ms 43940 KB Output is correct
53 Correct 297 ms 47104 KB Output is correct
54 Correct 395 ms 71964 KB Output is correct
55 Correct 20 ms 17060 KB Output is correct
56 Correct 14 ms 16064 KB Output is correct
57 Correct 92 ms 31448 KB Output is correct
58 Correct 50 ms 26408 KB Output is correct
59 Correct 87 ms 38412 KB Output is correct
60 Correct 276 ms 62264 KB Output is correct
61 Correct 267 ms 46484 KB Output is correct
62 Correct 225 ms 43868 KB Output is correct
63 Correct 337 ms 43912 KB Output is correct
64 Correct 541 ms 85392 KB Output is correct
65 Correct 560 ms 84132 KB Output is correct
66 Correct 326 ms 61492 KB Output is correct
67 Correct 193 ms 39920 KB Output is correct
68 Correct 398 ms 56760 KB Output is correct
69 Correct 480 ms 60624 KB Output is correct
70 Correct 438 ms 55020 KB Output is correct