Submission #682574

# Submission time Handle Problem Language Result Execution time Memory
682574 2023-01-16T14:35:42 Z MattTheNub Race (IOI11_race) C++17
100 / 100
439 ms 105744 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

template <class T> using v = vector<T>;
using int2 = pair<int, int>;
using ll = long long;
using ll2 = pair<ll, ll>;

#define f first
#define s second
#define all(x) begin(x), end(x)

struct Paths {
  ll2 d = {0, 0};
  unordered_map<ll, ll> m;
};
#define dbg(x) cerr << "[" << __LINE__ << "] " << #x << " = " << (x) << '\n';

void dfs(v<v<int2>> &adj, v<Paths> &paths, int &ans, int k, int node,
         int prev) {
  Paths cur;

  for (auto next : adj[node]) {
    if (next.f != prev) {
      dfs(adj, paths, ans, k, next.f, node);
    }
  }

  // for (auto i : adj[node]) {
  //   // dbg(node);
  //   if (i.f != prev) {
  //     for (auto j : adj[node]) {
  //       if (i.f > j.f && j.f != prev) {
  //         int2 i2 = i;
  //         int2 j2 = j;
  //         if (paths[i.f].m.size() > paths[j.f].m.size()) {
  //           swap(i2, j2);
  //         }

  //         for (auto x : paths[i2.f].m) {
  //           ll cost = x.f + paths[i2.f].d.f + i2.s + j2.s;
  //           if (paths[j2.f].m.count(k - cost - paths[j2.f].d.f)) {
  //             ans = min(ans, (int)(x.s + paths[i2.f].d.s +
  //                                  paths[j2.f].m[k - cost - paths[j2.f].d.f]
  //                                  + paths[j2.f].d.s + 2));
  //           } else if (cost == k) {
  //             ans = min(ans, (int)(x.s + paths[i2.f].d.s + 2));
  //           }
  //         }
  //         if (paths[j2.f].m.count(k - i2.s - j2.s - paths[j2.f].d.f)) {
  //           ans =
  //               min(ans,
  //                   (int)(2 + paths[j2.f].m[k - i2.s - j2.s -
  //                   paths[j2.f].d.f] +
  //                         paths[j2.f].d.s));
  //         }

  //         if (i2.s + j2.s == k) {
  //           ans = min(ans, 2);
  //         }
  //       }
  //     }
  //   }
  // }

  for (auto next : adj[node]) {
    if (next.f != prev) {
#define p paths[next.f]
      if (p.m.size() > cur.m.size()) {
        p.m.swap(cur.m);
        swap(p.d, cur.d);
        cur.d.f += next.s;
        cur.d.s += 1;
        cur.m[next.s - cur.d.f] = 1 - cur.d.s;
      } else {
        p.d.f += next.s;
        p.d.s += 1;
        p.m[next.s - p.d.f] = 1 - p.d.s;
      }

      // dbg(node);
      // dbg(p.m.size());

      for (auto x : p.m) {
        ll cost = x.f + p.d.f;
        if (cur.m.count(k - cost - cur.d.f)) {
          ans = min(ans,
                    (int)(x.s + p.d.s + cur.m[k - cost - cur.d.f] + cur.d.s));
        } else if (cost == k) {
          ans = min(ans, (int)(x.s + p.d.s));
        }
      }
      for (auto x : p.m) {
        ll cost = x.f + p.d.f;
        // dbg(cost);
        // dbg(cur.d.s);
        ll vx = x.s + p.d.s;
        if (cost < k && vx < ans) {
          if (cur.m.count(cost - cur.d.f)) {
            cur.m[cost - cur.d.f] = min(cur.m[cost - cur.d.f], vx - cur.d.s);
          } else {
            cur.m[cost - cur.d.f] = vx - cur.d.s;
          }
        }
      }
      // dbg(cur.m[next.s - cur.d.f]);
    }
  }
  // dbg(cur.m.size());

  if (cur.m.count(k - cur.d.f)) {
    ans = min(ans, (int)(cur.m[k - cur.d.f] + cur.d.s));
    if ((cur.m[k - cur.d.f] + cur.d.s) == 5) {
      dbg(node);
    }
  }

  paths[node] = {cur.d, move(cur.m)};
}

int best_path(int N, int K, int H[][2], int L[]) {
  int ans = INT_MAX;
  v<v<int2>> adj(N);
  for (int i = 0; i < N - 1; i++) {
    adj[H[i][0]].push_back({H[i][1], L[i]});
    adj[H[i][1]].push_back({H[i][0], L[i]});
  }
  v<Paths> paths(N);
  dfs(adj, paths, ans, K, 0, -1);

  if (ans == INT_MAX)
    ans = -1;

  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 0 ms 308 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 568 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 580 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 2 ms 576 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 140 ms 30356 KB Output is correct
20 Correct 137 ms 30348 KB Output is correct
21 Correct 141 ms 30448 KB Output is correct
22 Correct 160 ms 31272 KB Output is correct
23 Correct 117 ms 30284 KB Output is correct
24 Correct 129 ms 33128 KB Output is correct
25 Correct 86 ms 33144 KB Output is correct
26 Correct 69 ms 48140 KB Output is correct
27 Correct 236 ms 50300 KB Output is correct
28 Correct 268 ms 105744 KB Output is correct
29 Correct 284 ms 101604 KB Output is correct
30 Correct 193 ms 50368 KB Output is correct
31 Correct 195 ms 50324 KB Output is correct
32 Correct 257 ms 50504 KB Output is correct
33 Correct 179 ms 40132 KB Output is correct
34 Correct 249 ms 60060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 0 ms 308 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 568 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 580 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 2 ms 576 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 140 ms 30356 KB Output is correct
40 Correct 137 ms 30348 KB Output is correct
41 Correct 141 ms 30448 KB Output is correct
42 Correct 160 ms 31272 KB Output is correct
43 Correct 117 ms 30284 KB Output is correct
44 Correct 129 ms 33128 KB Output is correct
45 Correct 86 ms 33144 KB Output is correct
46 Correct 69 ms 48140 KB Output is correct
47 Correct 236 ms 50300 KB Output is correct
48 Correct 268 ms 105744 KB Output is correct
49 Correct 284 ms 101604 KB Output is correct
50 Correct 193 ms 50368 KB Output is correct
51 Correct 195 ms 50324 KB Output is correct
52 Correct 257 ms 50504 KB Output is correct
53 Correct 179 ms 40132 KB Output is correct
54 Correct 249 ms 60060 KB Output is correct
55 Correct 13 ms 3924 KB Output is correct
56 Correct 11 ms 3028 KB Output is correct
57 Correct 125 ms 29956 KB Output is correct
58 Correct 68 ms 29832 KB Output is correct
59 Correct 78 ms 52828 KB Output is correct
60 Correct 251 ms 102936 KB Output is correct
61 Correct 234 ms 51812 KB Output is correct
62 Correct 192 ms 50380 KB Output is correct
63 Correct 242 ms 50576 KB Output is correct
64 Correct 439 ms 90408 KB Output is correct
65 Correct 325 ms 60844 KB Output is correct
66 Correct 250 ms 88912 KB Output is correct
67 Correct 270 ms 60864 KB Output is correct
68 Correct 320 ms 65092 KB Output is correct
69 Correct 386 ms 68828 KB Output is correct
70 Correct 307 ms 62740 KB Output is correct