답안 #1055399

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055399 2024-08-12T18:43:12 Z E_rK 경주 (Race) (IOI11_race) C++14
21 / 100
75 ms 25944 KB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;
#define MAXN 200005
#define pb push_back
#define fi first
#define se second
typedef long long int lo;

lo k;
vector<pair<lo,lo> > v[MAXN];
lo vis[MAXN];
lo sub[MAXN];
lo mpp[1000005];
lo ans;
void dfs(lo node, lo par)
{
    sub[node] = 1;
    for(auto e: v[node]){
        if(e.fi == par or vis[e.fi])
            continue;
        dfs(e.fi,node);
        sub[node] += sub[e.fi];
    }
}

lo centroid(lo node, lo par, lo val){
    for(auto e: v[node]){
        if(e.fi == par or vis[e.fi])
            continue;
        if(sub[e.fi] >= val/2)
            return centroid(e.fi,node,val);
    }
    return node;
}
void check(lo node, lo par, lo cost,lo num){
    if(cost > k)
        return;
    if(mpp[k-cost] != -1)
        ans = min(ans, mpp[k-cost] + num);
    for(auto e: v[node]){
        if(e.fi == par or vis[e.fi])
            continue;
        check(e.fi,node,cost+e.se,num+1);
    }
}
void build(lo node, lo par, lo cost,lo num){
    if(cost > k)
        return;
    if(mpp[cost] == -1)
        mpp[cost] = num;
    else
        mpp[cost] = min(num,mpp[cost]);
    for(auto e: v[node]){
        if(e.fi == par or vis[e.fi])
            continue;
        build(e.fi,node,cost+e.se,num+1);
    }
}

void sifirla(lo node, lo par, lo cost){
    if(cost > k)
        return;
    mpp[cost] = -1;
    for(auto e:v[node]){
        if(e.fi == par || vis[e.fi])continue;
        sifirla(e.fi,node,cost+e.se);
    }
}

void solve(lo node)
{
    dfs(node,-1);
    lo c = centroid(node,-1,sub[node]);
    for(auto e: v[c]){
        if(vis[e.fi])
            continue;
        check(e.fi,c,e.se,1);
        build(e.fi,c,e.se,1);
    }
    for(auto e: v[c]){
        if(vis[e.fi])
            continue;
        sifirla(e.fi,c,e.se);
    }
    vis[c]=1;
    for(auto e: v[c]){
        if(vis[e.fi])
            continue;
        solve(e.fi);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
    k = K;
    memset(mpp,-1,sizeof(mpp));
    for (int i = 0; i < N-1; ++i)
    {
        v[H[i][0]].pb({H[i][1],L[i]});
        v[H[i][1]].pb({H[i][0],L[i]});
    }
    ans = N+5;
    solve(0);
    if(ans == N+5)
        ans = -1;
    return ans;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17500 KB Output is correct
2 Correct 2 ms 17500 KB Output is correct
3 Correct 2 ms 17500 KB Output is correct
4 Correct 2 ms 17500 KB Output is correct
5 Correct 2 ms 17500 KB Output is correct
6 Correct 2 ms 17500 KB Output is correct
7 Correct 2 ms 17500 KB Output is correct
8 Correct 2 ms 17500 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 2 ms 17500 KB Output is correct
11 Correct 2 ms 17500 KB Output is correct
12 Correct 2 ms 17500 KB Output is correct
13 Correct 2 ms 17500 KB Output is correct
14 Correct 2 ms 17596 KB Output is correct
15 Correct 2 ms 17500 KB Output is correct
16 Correct 2 ms 17496 KB Output is correct
17 Correct 2 ms 17500 KB Output is correct
18 Correct 2 ms 17500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17500 KB Output is correct
2 Correct 2 ms 17500 KB Output is correct
3 Correct 2 ms 17500 KB Output is correct
4 Correct 2 ms 17500 KB Output is correct
5 Correct 2 ms 17500 KB Output is correct
6 Correct 2 ms 17500 KB Output is correct
7 Correct 2 ms 17500 KB Output is correct
8 Correct 2 ms 17500 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 2 ms 17500 KB Output is correct
11 Correct 2 ms 17500 KB Output is correct
12 Correct 2 ms 17500 KB Output is correct
13 Correct 2 ms 17500 KB Output is correct
14 Correct 2 ms 17596 KB Output is correct
15 Correct 2 ms 17500 KB Output is correct
16 Correct 2 ms 17496 KB Output is correct
17 Correct 2 ms 17500 KB Output is correct
18 Correct 2 ms 17500 KB Output is correct
19 Correct 2 ms 17500 KB Output is correct
20 Correct 2 ms 17584 KB Output is correct
21 Correct 2 ms 17500 KB Output is correct
22 Correct 2 ms 17500 KB Output is correct
23 Correct 2 ms 17500 KB Output is correct
24 Correct 2 ms 17752 KB Output is correct
25 Correct 2 ms 17756 KB Output is correct
26 Correct 2 ms 17500 KB Output is correct
27 Correct 2 ms 17756 KB Output is correct
28 Correct 2 ms 17500 KB Output is correct
29 Correct 2 ms 17500 KB Output is correct
30 Correct 3 ms 17500 KB Output is correct
31 Correct 3 ms 17500 KB Output is correct
32 Correct 2 ms 17500 KB Output is correct
33 Correct 2 ms 17500 KB Output is correct
34 Correct 3 ms 17752 KB Output is correct
35 Correct 2 ms 17752 KB Output is correct
36 Correct 2 ms 17756 KB Output is correct
37 Correct 2 ms 17500 KB Output is correct
38 Correct 2 ms 17500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17500 KB Output is correct
2 Correct 2 ms 17500 KB Output is correct
3 Correct 2 ms 17500 KB Output is correct
4 Correct 2 ms 17500 KB Output is correct
5 Correct 2 ms 17500 KB Output is correct
6 Correct 2 ms 17500 KB Output is correct
7 Correct 2 ms 17500 KB Output is correct
8 Correct 2 ms 17500 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 2 ms 17500 KB Output is correct
11 Correct 2 ms 17500 KB Output is correct
12 Correct 2 ms 17500 KB Output is correct
13 Correct 2 ms 17500 KB Output is correct
14 Correct 2 ms 17596 KB Output is correct
15 Correct 2 ms 17500 KB Output is correct
16 Correct 2 ms 17496 KB Output is correct
17 Correct 2 ms 17500 KB Output is correct
18 Correct 2 ms 17500 KB Output is correct
19 Correct 74 ms 25532 KB Output is correct
20 Correct 70 ms 25768 KB Output is correct
21 Correct 75 ms 25688 KB Output is correct
22 Correct 70 ms 25680 KB Output is correct
23 Incorrect 34 ms 25944 KB Output isn't correct
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17500 KB Output is correct
2 Correct 2 ms 17500 KB Output is correct
3 Correct 2 ms 17500 KB Output is correct
4 Correct 2 ms 17500 KB Output is correct
5 Correct 2 ms 17500 KB Output is correct
6 Correct 2 ms 17500 KB Output is correct
7 Correct 2 ms 17500 KB Output is correct
8 Correct 2 ms 17500 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 2 ms 17500 KB Output is correct
11 Correct 2 ms 17500 KB Output is correct
12 Correct 2 ms 17500 KB Output is correct
13 Correct 2 ms 17500 KB Output is correct
14 Correct 2 ms 17596 KB Output is correct
15 Correct 2 ms 17500 KB Output is correct
16 Correct 2 ms 17496 KB Output is correct
17 Correct 2 ms 17500 KB Output is correct
18 Correct 2 ms 17500 KB Output is correct
19 Correct 2 ms 17500 KB Output is correct
20 Correct 2 ms 17584 KB Output is correct
21 Correct 2 ms 17500 KB Output is correct
22 Correct 2 ms 17500 KB Output is correct
23 Correct 2 ms 17500 KB Output is correct
24 Correct 2 ms 17752 KB Output is correct
25 Correct 2 ms 17756 KB Output is correct
26 Correct 2 ms 17500 KB Output is correct
27 Correct 2 ms 17756 KB Output is correct
28 Correct 2 ms 17500 KB Output is correct
29 Correct 2 ms 17500 KB Output is correct
30 Correct 3 ms 17500 KB Output is correct
31 Correct 3 ms 17500 KB Output is correct
32 Correct 2 ms 17500 KB Output is correct
33 Correct 2 ms 17500 KB Output is correct
34 Correct 3 ms 17752 KB Output is correct
35 Correct 2 ms 17752 KB Output is correct
36 Correct 2 ms 17756 KB Output is correct
37 Correct 2 ms 17500 KB Output is correct
38 Correct 2 ms 17500 KB Output is correct
39 Correct 74 ms 25532 KB Output is correct
40 Correct 70 ms 25768 KB Output is correct
41 Correct 75 ms 25688 KB Output is correct
42 Correct 70 ms 25680 KB Output is correct
43 Incorrect 34 ms 25944 KB Output isn't correct
44 Halted 0 ms 0 KB -