Submission #963920

# Submission time Handle Problem Language Result Execution time Memory
963920 2024-04-16T03:17:37 Z zNatsumi Race (IOI11_race) C++14
100 / 100
766 ms 35228 KB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second

using namespace std;
const int N = 2e5 + 5, oo = 1e9;
int n, k;
vector<ii> g[N];

namespace sub12{
  int up[N][20], cnt[N], path[N], res = oo;
  int timer, in[N], out[N];

  void dfs(int u){
    in[u] = ++timer;
    for(int i = 1; i < 20; i++)
      up[u][i] = up[up[u][i-1]][i-1];
    for(auto x : g[u]){
      int v = x.fi, w = x.se;
      if(v == up[u][0]) continue;
      up[v][0] = u;
      cnt[v] = cnt[u] + 1;
      path[v] = path[u] + w;
      dfs(v);
    }
    out[u] = ++timer;
  }
  bool anc(int u, int v){
    return in[u] <= in[v] && out[v] <= out[u];
  }
  int lca(int u, int v){
    if(anc(u, v)) return u;
    if(anc(v, u)) return v;
    for(int i = 19; i >= 0; i--)
      if(!anc(up[u][i], v)) u = up[u][i];
    return up[u][0];
  }
  int dist(int u, int v){
    return path[u] + path[v] - 2*path[lca(u, v)];
  }
  int cal(int u, int v){
    return cnt[u] + cnt[v] - 2*cnt[lca(u, v)];
  }
  int solve(){
    dfs(0);
    int res = oo;
    for(int u = 0; u < n; u++)
      for(int v = u+1; v < n; v++)
        if(dist(u, v) == k){
          res = min(res, cal(u, v));
        }
    return (res == oo ? -1 : res);
  }
}

namespace sub34{
  const int V = 1e6 + 5;
  int child[N], res = oo;
  bool del[N];
  map<int, int> path;

  int cnt_child(int u, int p){
    child[u] = 1;
    for(auto x : g[u]){
      int v = x.fi;
      if(v == p || del[v]) continue;
      child[u] += cnt_child(v, u);
    }
    return child[u];
  }
  int centroid(int u, int p, int sz){
    for(auto x : g[u]){
      int v = x.fi;
      if(v == p || del[v]) continue;
      if(child[v] > sz/2) return centroid(v, u, sz);
    }
    return u;
  }
  void cal(int u, int p, int cnt, int len){
    if(len > k) return;
    if(path.find(k - len) != path.end()) res = min(res, cnt + path[k - len]);
    for(auto x : g[u]){
      int v = x.fi, w = x.se;
      if(v == p || del[v]) continue;
      cal(v, u, cnt + 1, len + w);
    }
    return;
  }
  void upd(int u, int p, int cnt, int len){
    if(len > k) return;
    if(path.find(len) == path.end()) path[len] = cnt;
    else path[len] = min(path[len], cnt);

    for(auto x : g[u]){
      int v = x.fi, w = x.se;
      if(v == p || del[v]) continue;
      upd(v, u, cnt + 1, len + w);
    }
  }
  void dfs(int u){
    int sz = cnt_child(u, u);
    u = centroid(u, u, sz);
    del[u] = true;

    path.clear();
    path[0] = 0;
    for(auto x : g[u]){
      int v = x.fi, w = x.se;
      if(del[v]) continue;
      cal(v, u, 1, w);
      upd(v, u, 1, w);
    }

    for(auto x : g[u]){
      int v = x.fi;
      if(del[v]) continue;
      dfs(v);
    }
    return;
  }
  int solve(){
    dfs(0);
    return (res == oo ? -1 : res);
  }
}

int best_path(int _n, int _k, int h[][2], int l[]){
  n = _n;
  k = _k;
  for(int i = 0; i < n-1; i++){
    int u = h[i][0], v = h[i][1], w = l[i];
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  return (n <= 1000 ? sub12::solve() : sub34::solve());
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5268 KB Output is correct
12 Correct 2 ms 5180 KB Output is correct
13 Correct 3 ms 4952 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4952 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5268 KB Output is correct
12 Correct 2 ms 5180 KB Output is correct
13 Correct 3 ms 4952 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4952 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 4 ms 4956 KB Output is correct
21 Correct 50 ms 5300 KB Output is correct
22 Correct 49 ms 5208 KB Output is correct
23 Correct 48 ms 5208 KB Output is correct
24 Correct 54 ms 5300 KB Output is correct
25 Correct 54 ms 5212 KB Output is correct
26 Correct 51 ms 5300 KB Output is correct
27 Correct 50 ms 5212 KB Output is correct
28 Correct 50 ms 5212 KB Output is correct
29 Correct 49 ms 5212 KB Output is correct
30 Correct 50 ms 5368 KB Output is correct
31 Correct 50 ms 5212 KB Output is correct
32 Correct 49 ms 5212 KB Output is correct
33 Correct 53 ms 5208 KB Output is correct
34 Correct 37 ms 5296 KB Output is correct
35 Correct 30 ms 5208 KB Output is correct
36 Correct 19 ms 5316 KB Output is correct
37 Correct 28 ms 5296 KB Output is correct
38 Correct 38 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5268 KB Output is correct
12 Correct 2 ms 5180 KB Output is correct
13 Correct 3 ms 4952 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4952 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
19 Correct 144 ms 10456 KB Output is correct
20 Correct 146 ms 10460 KB Output is correct
21 Correct 157 ms 10484 KB Output is correct
22 Correct 152 ms 10544 KB Output is correct
23 Correct 62 ms 10588 KB Output is correct
24 Correct 64 ms 10584 KB Output is correct
25 Correct 114 ms 13228 KB Output is correct
26 Correct 82 ms 15964 KB Output is correct
27 Correct 141 ms 15896 KB Output is correct
28 Correct 182 ms 26944 KB Output is correct
29 Correct 207 ms 26372 KB Output is correct
30 Correct 143 ms 15984 KB Output is correct
31 Correct 143 ms 15764 KB Output is correct
32 Correct 145 ms 15952 KB Output is correct
33 Correct 216 ms 14728 KB Output is correct
34 Correct 118 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5268 KB Output is correct
12 Correct 2 ms 5180 KB Output is correct
13 Correct 3 ms 4952 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4952 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 4 ms 4956 KB Output is correct
21 Correct 50 ms 5300 KB Output is correct
22 Correct 49 ms 5208 KB Output is correct
23 Correct 48 ms 5208 KB Output is correct
24 Correct 54 ms 5300 KB Output is correct
25 Correct 54 ms 5212 KB Output is correct
26 Correct 51 ms 5300 KB Output is correct
27 Correct 50 ms 5212 KB Output is correct
28 Correct 50 ms 5212 KB Output is correct
29 Correct 49 ms 5212 KB Output is correct
30 Correct 50 ms 5368 KB Output is correct
31 Correct 50 ms 5212 KB Output is correct
32 Correct 49 ms 5212 KB Output is correct
33 Correct 53 ms 5208 KB Output is correct
34 Correct 37 ms 5296 KB Output is correct
35 Correct 30 ms 5208 KB Output is correct
36 Correct 19 ms 5316 KB Output is correct
37 Correct 28 ms 5296 KB Output is correct
38 Correct 38 ms 5212 KB Output is correct
39 Correct 144 ms 10456 KB Output is correct
40 Correct 146 ms 10460 KB Output is correct
41 Correct 157 ms 10484 KB Output is correct
42 Correct 152 ms 10544 KB Output is correct
43 Correct 62 ms 10588 KB Output is correct
44 Correct 64 ms 10584 KB Output is correct
45 Correct 114 ms 13228 KB Output is correct
46 Correct 82 ms 15964 KB Output is correct
47 Correct 141 ms 15896 KB Output is correct
48 Correct 182 ms 26944 KB Output is correct
49 Correct 207 ms 26372 KB Output is correct
50 Correct 143 ms 15984 KB Output is correct
51 Correct 143 ms 15764 KB Output is correct
52 Correct 145 ms 15952 KB Output is correct
53 Correct 216 ms 14728 KB Output is correct
54 Correct 118 ms 14672 KB Output is correct
55 Correct 20 ms 5724 KB Output is correct
56 Correct 12 ms 5680 KB Output is correct
57 Correct 114 ms 10868 KB Output is correct
58 Correct 39 ms 10696 KB Output is correct
59 Correct 237 ms 19532 KB Output is correct
60 Correct 697 ms 35228 KB Output is correct
61 Correct 201 ms 16016 KB Output is correct
62 Correct 234 ms 16116 KB Output is correct
63 Correct 248 ms 15952 KB Output is correct
64 Correct 766 ms 20404 KB Output is correct
65 Correct 146 ms 15876 KB Output is correct
66 Correct 424 ms 24348 KB Output is correct
67 Correct 107 ms 16128 KB Output is correct
68 Correct 368 ms 19740 KB Output is correct
69 Correct 344 ms 19704 KB Output is correct
70 Correct 310 ms 19192 KB Output is correct