답안 #1088967

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1088967 2024-09-15T16:31:06 Z Tymond 경주 (Race) (IOI11_race) C++17
21 / 100
3000 ms 53640 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

const int MAXN = 2e5 + 7;
vector<pii> g[MAXN];
bool blocked[MAXN];
int sz[MAXN];
int parent[MAXN];
int n, k;
ll ans;
ll m[1000010];
vector<pll> akt;

int dfs(int v, int p){
    sz[v] = 1;
    parent[v] = p;
    for(auto ele : g[v]){
        if(ele.fi == p || blocked[ele.fi]){
            continue;
        }
        sz[v] += dfs(ele.fi, v);
    }
    return sz[v];
}

void dfs2(int v, int p, pll s){
    if(s.fi > k){
        return;
    }
    akt.pb(s);
    for(auto ele : g[v]){
        if(blocked[ele.fi] || p == ele.fi || s.fi + ele.se > k){
            continue;
        }
        dfs2(ele.fi, v, {s.fi + ele.se, s.se + 1});
    }
}

void centroid_decomposition(int centroid){
    int roz = dfs(centroid, centroid);

    while(true){
        bool f = 0;
        for(auto ele : g[centroid]){
            if(ele.fi == parent[centroid] || blocked[ele.fi]){
                continue;
            }
            if(sz[ele.fi] * 2 >= roz){
                centroid = ele.fi;
                break;
            }
        }
        if(!f){
            break;
        }
    }

    blocked[centroid] = 1;
    vector<pll> every;
    for(auto ele : g[centroid]){
        if(blocked[ele.fi]){
            continue;
        }
        akt.clear();
        dfs2(ele.fi, ele.fi, {ele.se, 1});
        for(auto ele2 : akt){
            every.pb(ele2);
            if(ele2.fi == k){
                ans = min(ans, ele2.se);
            }else if(ele2.fi < k){
                if(m[k - ele2.fi] != 0){
                    ans = min(ans, ele2.se + m[k - ele2.fi]);
                }
            }
        }

        for(auto ele2 : akt){
            if(m[ele2.fi] == 0){
                m[ele2.fi] = ele2.se;
            }else{
                m[ele2.fi] = min(m[ele2.fi], ele2.se);
            }
        }
    }
    for(auto ele : every){
        m[ele.fi] = 0;
    }

    for(auto ele : g[centroid]){
        if(blocked[ele.fi]){
            continue;
        }
        centroid_decomposition(ele.fi);
    }
}

int best_path(int N, int K, int H[][2], int L[]){
    ans = 1e9;
    n = N;
    k = K;
    //cout << n << ' ' << k << '\n';
    for(int i = 0; i < n - 1; i++){
        //cout << H[i][0] << ' ' << H[i][1] << ' ' << L[i] << '\n';
        g[H[i][0]].pb({H[i][1], L[i]});
        g[H[i][1]].pb({H[i][0], L[i]});
    }

    centroid_decomposition(0);
    if(ans == (ll)1e9){
        return -1;
    }
    return (int)ans;
}

/*int main(){
    int tab[3][2] = {{0, 1}, {1, 2}, {1, 3}};
    int l[3] = {1, 2, 4};
    cout << best_path(4, 3, tab, l) << '\n';
    return 0;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5120 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5120 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 3 ms 8028 KB Output is correct
24 Correct 4 ms 9308 KB Output is correct
25 Correct 4 ms 8796 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 6748 KB Output is correct
29 Correct 4 ms 7772 KB Output is correct
30 Correct 4 ms 8028 KB Output is correct
31 Correct 4 ms 9564 KB Output is correct
32 Correct 4 ms 9308 KB Output is correct
33 Correct 5 ms 10844 KB Output is correct
34 Correct 5 ms 9820 KB Output is correct
35 Correct 6 ms 11612 KB Output is correct
36 Correct 6 ms 12636 KB Output is correct
37 Correct 7 ms 10100 KB Output is correct
38 Correct 5 ms 8024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5120 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 98 ms 11992 KB Output is correct
20 Correct 105 ms 11876 KB Output is correct
21 Correct 91 ms 14072 KB Output is correct
22 Correct 82 ms 17024 KB Output is correct
23 Correct 55 ms 11132 KB Output is correct
24 Correct 37 ms 10840 KB Output is correct
25 Execution timed out 3057 ms 53640 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5120 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 3 ms 8028 KB Output is correct
24 Correct 4 ms 9308 KB Output is correct
25 Correct 4 ms 8796 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 6748 KB Output is correct
29 Correct 4 ms 7772 KB Output is correct
30 Correct 4 ms 8028 KB Output is correct
31 Correct 4 ms 9564 KB Output is correct
32 Correct 4 ms 9308 KB Output is correct
33 Correct 5 ms 10844 KB Output is correct
34 Correct 5 ms 9820 KB Output is correct
35 Correct 6 ms 11612 KB Output is correct
36 Correct 6 ms 12636 KB Output is correct
37 Correct 7 ms 10100 KB Output is correct
38 Correct 5 ms 8024 KB Output is correct
39 Correct 98 ms 11992 KB Output is correct
40 Correct 105 ms 11876 KB Output is correct
41 Correct 91 ms 14072 KB Output is correct
42 Correct 82 ms 17024 KB Output is correct
43 Correct 55 ms 11132 KB Output is correct
44 Correct 37 ms 10840 KB Output is correct
45 Execution timed out 3057 ms 53640 KB Time limit exceeded
46 Halted 0 ms 0 KB -