Submission #360203

# Submission time Handle Problem Language Result Execution time Memory
360203 2021-01-27T18:14:07 Z ljuba Race (IOI11_race) C++17
100 / 100
2234 ms 119868 KB
#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<ll> vll;

typedef vector<vi> vvi;
typedef vector<vll> vvll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second

template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif

const char nl = '\n';

#include "race.h"

const int mxN = 2e5 + 12;

int N;
ll k;

//prvi oznacava sumu duzina, drugi drugo
unordered_map<int, pll> dist[mxN];
unordered_map<int, int> dist2[mxN];
set<pii> adj[mxN];

int sub[mxN];

int ans = 1e9;

void dfs(int s, int p = -1) {
    sub[s] = 1;
    for(auto e : adj[s]) {
        if(e.fi == p)
            continue;
        dfs(e.fi, s);
        sub[s] += sub[e.fi];
    }
}

int centroid(int s, int p, int n) {
    for(auto e : adj[s]) {
        if(e.fi == p)
            continue;
        if(sub[e.fi] > n/2)
            return centroid(e.fi, s, n);
    }
    return s;
}

vpii tmp;

void dfs2(int s, int c, int p) {
    dbg(s, c, p, dist[c][p]);
    tmp.pb(dist[c][s]);
    for(auto e : adj[s]) {
        if(e.fi == p)
            continue;
        //dbg(s);
        dist[c][e.fi] = {dist[c][s].fi + e.se, dist[c][s].se + 1};
        int treba = k - dist[c][e.fi].fi;
        if(dist2[c].count(treba)) {
            ckmin(ans, (int)dist[c][e.fi].se + dist2[c][treba]);
        }
        dfs2(e.fi, c, s);
    }
}

void build(int s) {
    dfs(s);
    int n = sub[s];
    int c = centroid(s, -1, n);
    dbg(c);

    /*
    dist[c].reserve(1024);
    dist[c].max_load_factor(0.25);

    dist2[c].reserve(1024);
    dist2[c].max_load_factor(0.25);
    */

    dist[c][c] = {0, 0};
    dist2[c][0] = 0;

    for(auto e : adj[c]) {
        tmp.clear();
        dist[c][e.fi] = {e.se, 1};
        //tmp.pb(dist[c][e.fi]);
        //ker.pb(e.fi);
        if(dist2[c].count(k-e.se)) {
            ckmin(ans, 1 + dist2[c][k-e.se]);
        }
        dfs2(e.fi, c, c);

        for(auto z : tmp) {
            if(!dist2[c].count(z.fi)) {
                dist2[c][z.fi] = z.se;
            }
            ckmin(dist2[c][z.fi], z.se);
        }
        dbg(tmp);
    }

    if(dist2[c].count(k)) {
        ckmin(ans, dist2[c][k]);
    }

    dbg(dist[c], dist2[c]);

    dist[c].clear();
    dist2[c].clear();

    vpii temp(all(adj[c]));
    for(auto z : temp) {
        int b = z.fi;
        int w = z.se;
        adj[c].erase({b, w});
        adj[b].erase({c, w});
        build(b);
    }
}

int best_path(int _N, int K, int H[][2], int L[]) {
    N = _N;
    k = K;
    for(int i = 0; i < N-1; ++i) {
        int a = H[i][0], b = H[i][1];
        int w = L[i];
        adj[a].insert({b, w});
        adj[b].insert({a, w});
    }

    build(0);
    dbg(dist[2][0]);

    return (ans == 1e9 ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31724 KB Output is correct
2 Correct 24 ms 31740 KB Output is correct
3 Correct 25 ms 31724 KB Output is correct
4 Correct 23 ms 31724 KB Output is correct
5 Correct 23 ms 31724 KB Output is correct
6 Correct 23 ms 31724 KB Output is correct
7 Correct 23 ms 31724 KB Output is correct
8 Correct 24 ms 31724 KB Output is correct
9 Correct 23 ms 31724 KB Output is correct
10 Correct 23 ms 31724 KB Output is correct
11 Correct 24 ms 31724 KB Output is correct
12 Correct 24 ms 31724 KB Output is correct
13 Correct 24 ms 31852 KB Output is correct
14 Correct 24 ms 31724 KB Output is correct
15 Correct 24 ms 31724 KB Output is correct
16 Correct 24 ms 31724 KB Output is correct
17 Correct 24 ms 31724 KB Output is correct
18 Correct 24 ms 31852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31724 KB Output is correct
2 Correct 24 ms 31740 KB Output is correct
3 Correct 25 ms 31724 KB Output is correct
4 Correct 23 ms 31724 KB Output is correct
5 Correct 23 ms 31724 KB Output is correct
6 Correct 23 ms 31724 KB Output is correct
7 Correct 23 ms 31724 KB Output is correct
8 Correct 24 ms 31724 KB Output is correct
9 Correct 23 ms 31724 KB Output is correct
10 Correct 23 ms 31724 KB Output is correct
11 Correct 24 ms 31724 KB Output is correct
12 Correct 24 ms 31724 KB Output is correct
13 Correct 24 ms 31852 KB Output is correct
14 Correct 24 ms 31724 KB Output is correct
15 Correct 24 ms 31724 KB Output is correct
16 Correct 24 ms 31724 KB Output is correct
17 Correct 24 ms 31724 KB Output is correct
18 Correct 24 ms 31852 KB Output is correct
19 Correct 24 ms 31596 KB Output is correct
20 Correct 24 ms 31724 KB Output is correct
21 Correct 27 ms 31832 KB Output is correct
22 Correct 27 ms 31852 KB Output is correct
23 Correct 28 ms 31852 KB Output is correct
24 Correct 26 ms 31852 KB Output is correct
25 Correct 26 ms 31852 KB Output is correct
26 Correct 26 ms 31852 KB Output is correct
27 Correct 26 ms 31852 KB Output is correct
28 Correct 26 ms 31852 KB Output is correct
29 Correct 26 ms 31872 KB Output is correct
30 Correct 30 ms 31852 KB Output is correct
31 Correct 26 ms 31852 KB Output is correct
32 Correct 26 ms 31852 KB Output is correct
33 Correct 26 ms 31980 KB Output is correct
34 Correct 26 ms 31980 KB Output is correct
35 Correct 27 ms 31980 KB Output is correct
36 Correct 26 ms 31852 KB Output is correct
37 Correct 26 ms 31852 KB Output is correct
38 Correct 26 ms 31872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31724 KB Output is correct
2 Correct 24 ms 31740 KB Output is correct
3 Correct 25 ms 31724 KB Output is correct
4 Correct 23 ms 31724 KB Output is correct
5 Correct 23 ms 31724 KB Output is correct
6 Correct 23 ms 31724 KB Output is correct
7 Correct 23 ms 31724 KB Output is correct
8 Correct 24 ms 31724 KB Output is correct
9 Correct 23 ms 31724 KB Output is correct
10 Correct 23 ms 31724 KB Output is correct
11 Correct 24 ms 31724 KB Output is correct
12 Correct 24 ms 31724 KB Output is correct
13 Correct 24 ms 31852 KB Output is correct
14 Correct 24 ms 31724 KB Output is correct
15 Correct 24 ms 31724 KB Output is correct
16 Correct 24 ms 31724 KB Output is correct
17 Correct 24 ms 31724 KB Output is correct
18 Correct 24 ms 31852 KB Output is correct
19 Correct 721 ms 54740 KB Output is correct
20 Correct 705 ms 54796 KB Output is correct
21 Correct 682 ms 54796 KB Output is correct
22 Correct 650 ms 52912 KB Output is correct
23 Correct 795 ms 65036 KB Output is correct
24 Correct 403 ms 48652 KB Output is correct
25 Correct 710 ms 60508 KB Output is correct
26 Correct 400 ms 61196 KB Output is correct
27 Correct 800 ms 70388 KB Output is correct
28 Correct 2234 ms 119760 KB Output is correct
29 Correct 2144 ms 119396 KB Output is correct
30 Correct 810 ms 71988 KB Output is correct
31 Correct 802 ms 71860 KB Output is correct
32 Correct 1083 ms 70300 KB Output is correct
33 Correct 1359 ms 85848 KB Output is correct
34 Correct 1837 ms 106728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31724 KB Output is correct
2 Correct 24 ms 31740 KB Output is correct
3 Correct 25 ms 31724 KB Output is correct
4 Correct 23 ms 31724 KB Output is correct
5 Correct 23 ms 31724 KB Output is correct
6 Correct 23 ms 31724 KB Output is correct
7 Correct 23 ms 31724 KB Output is correct
8 Correct 24 ms 31724 KB Output is correct
9 Correct 23 ms 31724 KB Output is correct
10 Correct 23 ms 31724 KB Output is correct
11 Correct 24 ms 31724 KB Output is correct
12 Correct 24 ms 31724 KB Output is correct
13 Correct 24 ms 31852 KB Output is correct
14 Correct 24 ms 31724 KB Output is correct
15 Correct 24 ms 31724 KB Output is correct
16 Correct 24 ms 31724 KB Output is correct
17 Correct 24 ms 31724 KB Output is correct
18 Correct 24 ms 31852 KB Output is correct
19 Correct 24 ms 31596 KB Output is correct
20 Correct 24 ms 31724 KB Output is correct
21 Correct 27 ms 31832 KB Output is correct
22 Correct 27 ms 31852 KB Output is correct
23 Correct 28 ms 31852 KB Output is correct
24 Correct 26 ms 31852 KB Output is correct
25 Correct 26 ms 31852 KB Output is correct
26 Correct 26 ms 31852 KB Output is correct
27 Correct 26 ms 31852 KB Output is correct
28 Correct 26 ms 31852 KB Output is correct
29 Correct 26 ms 31872 KB Output is correct
30 Correct 30 ms 31852 KB Output is correct
31 Correct 26 ms 31852 KB Output is correct
32 Correct 26 ms 31852 KB Output is correct
33 Correct 26 ms 31980 KB Output is correct
34 Correct 26 ms 31980 KB Output is correct
35 Correct 27 ms 31980 KB Output is correct
36 Correct 26 ms 31852 KB Output is correct
37 Correct 26 ms 31852 KB Output is correct
38 Correct 26 ms 31872 KB Output is correct
39 Correct 721 ms 54740 KB Output is correct
40 Correct 705 ms 54796 KB Output is correct
41 Correct 682 ms 54796 KB Output is correct
42 Correct 650 ms 52912 KB Output is correct
43 Correct 795 ms 65036 KB Output is correct
44 Correct 403 ms 48652 KB Output is correct
45 Correct 710 ms 60508 KB Output is correct
46 Correct 400 ms 61196 KB Output is correct
47 Correct 800 ms 70388 KB Output is correct
48 Correct 2234 ms 119760 KB Output is correct
49 Correct 2144 ms 119396 KB Output is correct
50 Correct 810 ms 71988 KB Output is correct
51 Correct 802 ms 71860 KB Output is correct
52 Correct 1083 ms 70300 KB Output is correct
53 Correct 1359 ms 85848 KB Output is correct
54 Correct 1837 ms 106728 KB Output is correct
55 Correct 60 ms 34156 KB Output is correct
56 Correct 59 ms 33772 KB Output is correct
57 Correct 469 ms 60556 KB Output is correct
58 Correct 166 ms 49424 KB Output is correct
59 Correct 548 ms 74416 KB Output is correct
60 Correct 2070 ms 119868 KB Output is correct
61 Correct 850 ms 72208 KB Output is correct
62 Correct 780 ms 71860 KB Output is correct
63 Correct 1069 ms 70132 KB Output is correct
64 Correct 2106 ms 96276 KB Output is correct
65 Correct 1907 ms 89784 KB Output is correct
66 Correct 2213 ms 118816 KB Output is correct
67 Correct 606 ms 66628 KB Output is correct
68 Correct 1227 ms 75000 KB Output is correct
69 Correct 1228 ms 75080 KB Output is correct
70 Correct 1116 ms 73264 KB Output is correct