답안 #829856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
829856 2023-08-18T15:24:09 Z Dec0Dedd 경주 (Race) (IOI11_race) C++14
100 / 100
1111 ms 54176 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

const int N = 2e5+10;
const int K = 1e6+100;
const int INF = 1e9+10;

int n, k, sub[N], mn[K+10];
set<pii> G[N];

void pre(int v, int p) {
    sub[v]=1;
    for (auto u : G[v]) {
        if (u.first == p) continue;
        pre(u.first, v);
        sub[v]+=sub[u.first];
    }
}

int cent(int v, int p, int sz) {
    int x=-1; bool ok=2*(sz-sub[v]) <= sz;
    for (auto u : G[v]) {
        if (u.first == p) continue;

        int r=cent(u.first, v, sz);
        if (sub[u.first]*2 > sz) ok=false;
        if (r > -1) x=r;
    }

    if (ok) x=v;
    return x;
}

void add(int v, int p, int dt, ll s) {
    for (auto u : G[v]) {
        if (u.first == p) continue;
        add(u.first, v, dt+1, s+u.second);
    }
    if (s <= k) mn[s]=min(mn[s], dt);
}

int calc(int v, int p, int dt, ll s) {
    int res=INF;
    if (s <= k) res=min(res, dt+mn[k-s]);
    for (auto u : G[v]) {
        if (u.first == p) continue;
        res=min(res, calc(u.first, v, dt+1, s+u.second));
    }
    return res;
}

void rem(int v, int p, ll s) {
    if (s <= k) mn[s]=INF;
    for (auto u : G[v]) {
        if (u.first == p) continue;
        rem(u.first, v, s+u.second);
    }
}

int solve(int v) {
    pre(v, v); int sz=sub[v];
    int r=cent(v, v, sz);
    assert(r > -1);

    int ans=INF;
    for (auto u : G[r]) {
        ans=min(ans, calc(u.first, r, 1, u.second));
        add(u.first, r, 1, u.second);
        ans=min(ans, mn[k]);
    }

    for (auto u : G[r]) rem(u.first, r, u.second);

    for (auto u : G[r]) {
        G[u.first].erase({r, u.second});
        ans=min(ans, solve(u.first));
    } G[r].clear();

    return ans;
}

int best_path(int sz, int l, int h[][2], int w[]) {
    n=sz, k=l;

    for (int i=0; i<=K; ++i) mn[i]=INF;
    for (int i=0; i<n-1; ++i) {
        int a=h[i][0]+1, b=h[i][1]+1;
        G[a].insert({b, w[i]});
        G[b].insert({a, w[i]});
    }

    int x=solve(1);
    if (x < INF) return x;
    return -1;
}

/*
#define MAX_N 500000

static int nv, kv;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;

inline 
void my_assert(int e) {if (!e) abort();}

void read_input()
{
  int i;
  my_assert(2==scanf("%d %d",&nv,&kv));
  for(i=0; i<nv-1; i++)
    my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
  //my_assert(1==scanf("%d",&solution));
}

int main()
{
  int ans;
  read_input();
  ans = best_path(nv,kv,H,L);
  printf("%d\n", ans);

  /*
  if(ans==solution)
    printf("Correct.\n")
  else
    printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);

  return 0;
} */

Compilation message

race.cpp:129:3: warning: "/*" within comment [-Wcomment]
  129 |   /*
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13524 KB Output is correct
2 Correct 6 ms 13540 KB Output is correct
3 Correct 7 ms 13524 KB Output is correct
4 Correct 6 ms 13524 KB Output is correct
5 Correct 7 ms 13640 KB Output is correct
6 Correct 6 ms 13524 KB Output is correct
7 Correct 7 ms 13524 KB Output is correct
8 Correct 7 ms 13524 KB Output is correct
9 Correct 6 ms 13560 KB Output is correct
10 Correct 6 ms 13552 KB Output is correct
11 Correct 7 ms 13552 KB Output is correct
12 Correct 6 ms 13524 KB Output is correct
13 Correct 6 ms 13524 KB Output is correct
14 Correct 6 ms 13524 KB Output is correct
15 Correct 7 ms 13524 KB Output is correct
16 Correct 7 ms 13524 KB Output is correct
17 Correct 8 ms 13548 KB Output is correct
18 Correct 7 ms 13652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13524 KB Output is correct
2 Correct 6 ms 13540 KB Output is correct
3 Correct 7 ms 13524 KB Output is correct
4 Correct 6 ms 13524 KB Output is correct
5 Correct 7 ms 13640 KB Output is correct
6 Correct 6 ms 13524 KB Output is correct
7 Correct 7 ms 13524 KB Output is correct
8 Correct 7 ms 13524 KB Output is correct
9 Correct 6 ms 13560 KB Output is correct
10 Correct 6 ms 13552 KB Output is correct
11 Correct 7 ms 13552 KB Output is correct
12 Correct 6 ms 13524 KB Output is correct
13 Correct 6 ms 13524 KB Output is correct
14 Correct 6 ms 13524 KB Output is correct
15 Correct 7 ms 13524 KB Output is correct
16 Correct 7 ms 13524 KB Output is correct
17 Correct 8 ms 13548 KB Output is correct
18 Correct 7 ms 13652 KB Output is correct
19 Correct 6 ms 13524 KB Output is correct
20 Correct 7 ms 13548 KB Output is correct
21 Correct 8 ms 13652 KB Output is correct
22 Correct 8 ms 13652 KB Output is correct
23 Correct 9 ms 13692 KB Output is correct
24 Correct 8 ms 13652 KB Output is correct
25 Correct 7 ms 13652 KB Output is correct
26 Correct 7 ms 13688 KB Output is correct
27 Correct 7 ms 13692 KB Output is correct
28 Correct 7 ms 13652 KB Output is correct
29 Correct 7 ms 13692 KB Output is correct
30 Correct 7 ms 13652 KB Output is correct
31 Correct 7 ms 13652 KB Output is correct
32 Correct 8 ms 13748 KB Output is correct
33 Correct 7 ms 13692 KB Output is correct
34 Correct 7 ms 13652 KB Output is correct
35 Correct 7 ms 13652 KB Output is correct
36 Correct 8 ms 13688 KB Output is correct
37 Correct 8 ms 13684 KB Output is correct
38 Correct 9 ms 13692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13524 KB Output is correct
2 Correct 6 ms 13540 KB Output is correct
3 Correct 7 ms 13524 KB Output is correct
4 Correct 6 ms 13524 KB Output is correct
5 Correct 7 ms 13640 KB Output is correct
6 Correct 6 ms 13524 KB Output is correct
7 Correct 7 ms 13524 KB Output is correct
8 Correct 7 ms 13524 KB Output is correct
9 Correct 6 ms 13560 KB Output is correct
10 Correct 6 ms 13552 KB Output is correct
11 Correct 7 ms 13552 KB Output is correct
12 Correct 6 ms 13524 KB Output is correct
13 Correct 6 ms 13524 KB Output is correct
14 Correct 6 ms 13524 KB Output is correct
15 Correct 7 ms 13524 KB Output is correct
16 Correct 7 ms 13524 KB Output is correct
17 Correct 8 ms 13548 KB Output is correct
18 Correct 7 ms 13652 KB Output is correct
19 Correct 344 ms 25852 KB Output is correct
20 Correct 314 ms 26020 KB Output is correct
21 Correct 269 ms 25924 KB Output is correct
22 Correct 264 ms 25952 KB Output is correct
23 Correct 188 ms 25908 KB Output is correct
24 Correct 120 ms 26000 KB Output is correct
25 Correct 297 ms 30040 KB Output is correct
26 Correct 151 ms 33736 KB Output is correct
27 Correct 380 ms 38220 KB Output is correct
28 Correct 934 ms 54176 KB Output is correct
29 Correct 866 ms 53064 KB Output is correct
30 Correct 422 ms 38336 KB Output is correct
31 Correct 392 ms 38332 KB Output is correct
32 Correct 566 ms 38432 KB Output is correct
33 Correct 620 ms 38532 KB Output is correct
34 Correct 757 ms 39388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13524 KB Output is correct
2 Correct 6 ms 13540 KB Output is correct
3 Correct 7 ms 13524 KB Output is correct
4 Correct 6 ms 13524 KB Output is correct
5 Correct 7 ms 13640 KB Output is correct
6 Correct 6 ms 13524 KB Output is correct
7 Correct 7 ms 13524 KB Output is correct
8 Correct 7 ms 13524 KB Output is correct
9 Correct 6 ms 13560 KB Output is correct
10 Correct 6 ms 13552 KB Output is correct
11 Correct 7 ms 13552 KB Output is correct
12 Correct 6 ms 13524 KB Output is correct
13 Correct 6 ms 13524 KB Output is correct
14 Correct 6 ms 13524 KB Output is correct
15 Correct 7 ms 13524 KB Output is correct
16 Correct 7 ms 13524 KB Output is correct
17 Correct 8 ms 13548 KB Output is correct
18 Correct 7 ms 13652 KB Output is correct
19 Correct 6 ms 13524 KB Output is correct
20 Correct 7 ms 13548 KB Output is correct
21 Correct 8 ms 13652 KB Output is correct
22 Correct 8 ms 13652 KB Output is correct
23 Correct 9 ms 13692 KB Output is correct
24 Correct 8 ms 13652 KB Output is correct
25 Correct 7 ms 13652 KB Output is correct
26 Correct 7 ms 13688 KB Output is correct
27 Correct 7 ms 13692 KB Output is correct
28 Correct 7 ms 13652 KB Output is correct
29 Correct 7 ms 13692 KB Output is correct
30 Correct 7 ms 13652 KB Output is correct
31 Correct 7 ms 13652 KB Output is correct
32 Correct 8 ms 13748 KB Output is correct
33 Correct 7 ms 13692 KB Output is correct
34 Correct 7 ms 13652 KB Output is correct
35 Correct 7 ms 13652 KB Output is correct
36 Correct 8 ms 13688 KB Output is correct
37 Correct 8 ms 13684 KB Output is correct
38 Correct 9 ms 13692 KB Output is correct
39 Correct 344 ms 25852 KB Output is correct
40 Correct 314 ms 26020 KB Output is correct
41 Correct 269 ms 25924 KB Output is correct
42 Correct 264 ms 25952 KB Output is correct
43 Correct 188 ms 25908 KB Output is correct
44 Correct 120 ms 26000 KB Output is correct
45 Correct 297 ms 30040 KB Output is correct
46 Correct 151 ms 33736 KB Output is correct
47 Correct 380 ms 38220 KB Output is correct
48 Correct 934 ms 54176 KB Output is correct
49 Correct 866 ms 53064 KB Output is correct
50 Correct 422 ms 38336 KB Output is correct
51 Correct 392 ms 38332 KB Output is correct
52 Correct 566 ms 38432 KB Output is correct
53 Correct 620 ms 38532 KB Output is correct
54 Correct 757 ms 39388 KB Output is correct
55 Correct 23 ms 14836 KB Output is correct
56 Correct 18 ms 14840 KB Output is correct
57 Correct 111 ms 25848 KB Output is correct
58 Correct 63 ms 25548 KB Output is correct
59 Correct 158 ms 33736 KB Output is correct
60 Correct 1061 ms 53312 KB Output is correct
61 Correct 434 ms 38492 KB Output is correct
62 Correct 421 ms 38372 KB Output is correct
63 Correct 582 ms 38428 KB Output is correct
64 Correct 1091 ms 38988 KB Output is correct
65 Correct 824 ms 39380 KB Output is correct
66 Correct 1111 ms 50616 KB Output is correct
67 Correct 405 ms 38792 KB Output is correct
68 Correct 608 ms 38720 KB Output is correct
69 Correct 626 ms 39008 KB Output is correct
70 Correct 556 ms 37588 KB Output is correct