Submission #958295

# Submission time Handle Problem Language Result Execution time Memory
958295 2024-04-05T10:26:13 Z hariaakas646 Race (IOI11_race) C++14
100 / 100
370 ms 85984 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;

vector<vii> graph;
int n, k;
vector<mpii> val;
int out = 1e9;

pii dfs(int x, int pa) {
  val[x][0] = 0;
  int lt = 0;
  int dt = 0;
  // printf("%d: \n", x);
  for(auto e : graph[x]) {
    if(e.f != pa) {
      pii p1 = dfs(e.f, x);
      int l = p1.f + e.s;
      int d = p1.s + 1;
      // printf("%d\n", e.f);
      if(val[e.f].size() > val[x].size()) {
        for(auto p : val[x]) {

          auto it = val[e.f].find(k-(p.f+lt)-l);
          // printf("%d %d %d\n", p.f, lt, l);
          if(it != val[e.f].end()) {
            out = min(out, (*it).s + d + p.s + dt);
          }
        }
        swap(val[e.f], val[x]);
        for(auto pt : val[e.f]) {
          pii p = pt;
          p.f += lt - l;
          p.s += dt - d;

          if(val[x].find(p.f) == val[x].end()) val[x][p.f] = p.s;
          else
          val[x][p.f] = min(val[x][p.f], p.s); 
        }
        lt = l;
        dt = d;
      }
      else {
        for(auto p : val[e.f]) {
          auto it = val[x].find(k-(p.f+l)-lt);
          if(it != val[x].end()) {
            out = min(out, (*it).s + dt + p.s + d);
          }
        }
        for(auto pt : val[e.f]) {
          pii p = pt;
          p.f += l - lt;
          p.s += d - dt;
          if(val[x].find(p.f) == val[x].end()) val[x][p.f] = p.s;
          else
          val[x][p.f] = min(val[x][p.f], p.s); 
        }
      }
    }
  }
  return mp(lt, dt);
}

int best_path(int N, int K, int H[][2], int L[])
{
  n = N;
  k = K;
  graph = vector<vii>(n);

  frange(i, n-1) {
    graph[H[i][0]].pb(mp(H[i][1], L[i]));
    graph[H[i][1]].pb(mp(H[i][0], L[i]));
  }
  
  val = vector<mpii>(n);
  dfs(0, -1);
  if(out == 1e9) return -1;
  return out;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2552 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2904 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 1 ms 2724 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2656 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 100 ms 27552 KB Output is correct
20 Correct 84 ms 27984 KB Output is correct
21 Correct 85 ms 27536 KB Output is correct
22 Correct 81 ms 27992 KB Output is correct
23 Correct 122 ms 37420 KB Output is correct
24 Correct 107 ms 30292 KB Output is correct
25 Correct 73 ms 31824 KB Output is correct
26 Correct 49 ms 40676 KB Output is correct
27 Correct 144 ms 42004 KB Output is correct
28 Correct 239 ms 85984 KB Output is correct
29 Correct 217 ms 84048 KB Output is correct
30 Correct 165 ms 42836 KB Output is correct
31 Correct 144 ms 42596 KB Output is correct
32 Correct 196 ms 42664 KB Output is correct
33 Correct 185 ms 46476 KB Output is correct
34 Correct 256 ms 70736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2552 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2904 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 1 ms 2724 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2656 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
39 Correct 100 ms 27552 KB Output is correct
40 Correct 84 ms 27984 KB Output is correct
41 Correct 85 ms 27536 KB Output is correct
42 Correct 81 ms 27992 KB Output is correct
43 Correct 122 ms 37420 KB Output is correct
44 Correct 107 ms 30292 KB Output is correct
45 Correct 73 ms 31824 KB Output is correct
46 Correct 49 ms 40676 KB Output is correct
47 Correct 144 ms 42004 KB Output is correct
48 Correct 239 ms 85984 KB Output is correct
49 Correct 217 ms 84048 KB Output is correct
50 Correct 165 ms 42836 KB Output is correct
51 Correct 144 ms 42596 KB Output is correct
52 Correct 196 ms 42664 KB Output is correct
53 Correct 185 ms 46476 KB Output is correct
54 Correct 256 ms 70736 KB Output is correct
55 Correct 11 ms 5468 KB Output is correct
56 Correct 6 ms 4444 KB Output is correct
57 Correct 53 ms 25172 KB Output is correct
58 Correct 43 ms 21388 KB Output is correct
59 Correct 71 ms 45284 KB Output is correct
60 Correct 211 ms 84800 KB Output is correct
61 Correct 164 ms 44624 KB Output is correct
62 Correct 141 ms 42580 KB Output is correct
63 Correct 184 ms 42508 KB Output is correct
64 Correct 370 ms 82644 KB Output is correct
65 Correct 351 ms 83028 KB Output is correct
66 Correct 229 ms 76832 KB Output is correct
67 Correct 126 ms 39592 KB Output is correct
68 Correct 274 ms 54864 KB Output is correct
69 Correct 307 ms 56856 KB Output is correct
70 Correct 252 ms 53328 KB Output is correct