Submission #961057

# Submission time Handle Problem Language Result Execution time Memory
961057 2024-04-11T13:01:14 Z hariaakas646 Race (IOI11_race) C++14
100 / 100
1014 ms 58568 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;
int out = 1e9;
vi sze;
vb vis;

int get_size(int x, int p=-1) {
  sze[x] = 1;
  for(auto e : graph[x]) {
    if(e.f != p && !vis[e.f]) {
      sze[x] += get_size(e.f, x);
    }
  }
  return sze[x];
}

int get_cent(int x, int subsz, int p=-1) {
  for(auto e : graph[x]) {
    if(e.f != p && !vis[e.f] && 2*sze[e.f] > subsz) {
      return get_cent(e.f, subsz, x);
    }
  }
  return x;
}

map<lli, int> dist;

void dist_dfs(int x, int p, lli d, int d1) {
  if(dist.find(d) == dist.end()) dist[d] = d1;
  else dist[d] = min(dist[d], d1);
  for(auto e : graph[x]) {
    if(e.f != p && !vis[e.f]) {
      dist_dfs(e.f, x, d+e.s, d1+1);
    }
  }
}

void decomp(int x) {
  int sz = get_size(x);
  x = get_cent(x, sz);
  vis[x] = true;
  map<lli, int> curr;
  curr[0] = 0;

  for(auto e : graph[x]) {
    if(!vis[e.f]) {
      dist = {};
      dist_dfs(e.f, x, e.s, 1);
      for(auto p : dist) {
        auto it = curr.find(k-p.f);
        if(it != curr.end()) {
          out = min(out, p.s + (*it).s);
        }
      }
      for(auto p : dist) {
        if(curr.find(p.f) == curr.end()) curr[p.f] = p.s;
        else curr[p.f] = min(curr[p.f], p.s);
      }
    }
  }

  for(auto e : graph[x]) {
    if(!vis[e.f]) decomp(e.f);
  }
}

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]));
  }

  vis = vb(n+1);
  sze = vi(n+1);
  
  decomp(0);
  if(out == 1e9) return -1;
  return out;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2404 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 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 1 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 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 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 2404 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 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 1 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 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 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 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2648 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 2 ms 2648 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2404 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 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 1 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 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 202 ms 11048 KB Output is correct
20 Correct 188 ms 11084 KB Output is correct
21 Correct 175 ms 11160 KB Output is correct
22 Correct 154 ms 11268 KB Output is correct
23 Correct 306 ms 11904 KB Output is correct
24 Correct 125 ms 11352 KB Output is correct
25 Correct 150 ms 15188 KB Output is correct
26 Correct 87 ms 18432 KB Output is correct
27 Correct 281 ms 18000 KB Output is correct
28 Correct 996 ms 58568 KB Output is correct
29 Correct 1004 ms 57812 KB Output is correct
30 Correct 291 ms 20212 KB Output is correct
31 Correct 274 ms 20256 KB Output is correct
32 Correct 333 ms 20136 KB Output is correct
33 Correct 477 ms 19440 KB Output is correct
34 Correct 886 ms 41132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2404 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 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 1 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 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 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 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2648 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 2 ms 2648 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2648 KB Output is correct
39 Correct 202 ms 11048 KB Output is correct
40 Correct 188 ms 11084 KB Output is correct
41 Correct 175 ms 11160 KB Output is correct
42 Correct 154 ms 11268 KB Output is correct
43 Correct 306 ms 11904 KB Output is correct
44 Correct 125 ms 11352 KB Output is correct
45 Correct 150 ms 15188 KB Output is correct
46 Correct 87 ms 18432 KB Output is correct
47 Correct 281 ms 18000 KB Output is correct
48 Correct 996 ms 58568 KB Output is correct
49 Correct 1004 ms 57812 KB Output is correct
50 Correct 291 ms 20212 KB Output is correct
51 Correct 274 ms 20256 KB Output is correct
52 Correct 333 ms 20136 KB Output is correct
53 Correct 477 ms 19440 KB Output is correct
54 Correct 886 ms 41132 KB Output is correct
55 Correct 22 ms 3928 KB Output is correct
56 Correct 12 ms 3164 KB Output is correct
57 Correct 124 ms 11596 KB Output is correct
58 Correct 38 ms 12188 KB Output is correct
59 Correct 369 ms 28752 KB Output is correct
60 Correct 1008 ms 56192 KB Output is correct
61 Correct 306 ms 21072 KB Output is correct
62 Correct 292 ms 20160 KB Output is correct
63 Correct 338 ms 19880 KB Output is correct
64 Correct 1014 ms 33340 KB Output is correct
65 Correct 823 ms 42592 KB Output is correct
66 Correct 968 ms 53220 KB Output is correct
67 Correct 116 ms 21440 KB Output is correct
68 Correct 455 ms 35692 KB Output is correct
69 Correct 466 ms 34420 KB Output is correct
70 Correct 413 ms 34996 KB Output is correct