Submission #185578

# Submission time Handle Problem Language Result Execution time Memory
185578 2020-01-12T01:24:13 Z kdh9949 Race (IOI11_race) C++17
100 / 100
492 ms 70796 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;

using pii = pair<int, int>;
#define x first
#define y second

const int N = 200005;

int n, k, res = N;
vector<pii> e[N];

struct SET{
  set<pii> s;
  pii ofs;
  SET(): ofs(0, 0) { s.emplace(0, 0); }
};

SET *f(int x, int y){
  if(x && e[x].size() == 1) return new SET();
  vector<SET*> v;
  SET* mx = NULL;
  for(pii i : e[x]){
    if(i.x == y) continue;
    v.push_back(f(i.x, x));
    v.back()->ofs.x += i.y;
    v.back()->ofs.y++;
    if(!mx || mx->s.size() < v.back()->s.size()) mx = v.back();
  }
  for(SET* i : v){
    if(i == mx) continue;
    for(pii j : i->s){
      auto it = mx->s.lower_bound(pii(k - (j.x + i->ofs.x) - mx->ofs.x, -N));
      if(it != mx->s.end() && it->x + mx->ofs.x + j.x + i->ofs.x == k)
        res = min(res, it->y + mx->ofs.y + j.y + i->ofs.y);
    }
    for(pii j : i->s){
      pii cur(j.x + i->ofs.x - mx->ofs.x, j.y + i->ofs.y - mx->ofs.y);
      auto it = mx->s.lower_bound(pii(cur.x, -N));
      if(it != mx->s.end() && it->x == cur.x){
        if(it->y <= cur.y) continue;
        if(it->y > cur.y) mx->s.erase(it);
      }
      mx->s.insert(cur);
    }
  }
  set<pii> t;
  while(!mx->s.empty() && mx->s.rbegin()->x + mx->ofs.x > k) mx->s.erase(*mx->s.rbegin());
  mx->s.emplace(-mx->ofs.x, -mx->ofs.y);

  auto it = mx->s.lower_bound(pii(k - mx->ofs.x, -N));
  if(it != mx->s.end() && it->x + mx->ofs.x == k) res = min(res, it->y + mx->ofs.y);

  return mx;
}

int best_path(int N, int K, int H[][2], int L[]) {
  n = N;
  k = K;
  for(int i = 0; i < n-1; i++){
    e[H[i][0]].emplace_back(H[i][1], L[i]);
    e[H[i][1]].emplace_back(H[i][0], L[i]);
  }

  f(0, -1);
  return (res == ::N ? -1 : res);
}

# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 12 ms 4984 KB Output is correct
4 Correct 7 ms 4988 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 5 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 6 ms 4984 KB Output is correct
17 Correct 7 ms 5112 KB Output is correct
18 Correct 21 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 12 ms 4984 KB Output is correct
4 Correct 7 ms 4988 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 5 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 6 ms 4984 KB Output is correct
17 Correct 7 ms 5112 KB Output is correct
18 Correct 21 ms 4984 KB Output is correct
19 Correct 7 ms 4984 KB Output is correct
20 Correct 5 ms 5084 KB Output is correct
21 Correct 16 ms 5240 KB Output is correct
22 Correct 7 ms 5116 KB Output is correct
23 Correct 0 ms 5112 KB Output is correct
24 Correct 7 ms 5112 KB Output is correct
25 Correct 9 ms 5296 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 7 ms 5112 KB Output is correct
28 Correct 12 ms 5240 KB Output is correct
29 Correct 8 ms 5240 KB Output is correct
30 Correct 9 ms 5240 KB Output is correct
31 Correct 9 ms 5240 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
33 Correct 9 ms 5240 KB Output is correct
34 Correct 7 ms 5112 KB Output is correct
35 Correct 9 ms 5212 KB Output is correct
36 Correct 9 ms 5112 KB Output is correct
37 Correct 9 ms 5116 KB Output is correct
38 Correct 10 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 12 ms 4984 KB Output is correct
4 Correct 7 ms 4988 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 5 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 6 ms 4984 KB Output is correct
17 Correct 7 ms 5112 KB Output is correct
18 Correct 21 ms 4984 KB Output is correct
19 Correct 154 ms 21920 KB Output is correct
20 Correct 141 ms 21984 KB Output is correct
21 Correct 136 ms 22240 KB Output is correct
22 Correct 139 ms 22508 KB Output is correct
23 Correct 119 ms 20344 KB Output is correct
24 Correct 137 ms 26636 KB Output is correct
25 Correct 132 ms 23124 KB Output is correct
26 Correct 129 ms 36728 KB Output is correct
27 Correct 209 ms 29176 KB Output is correct
28 Correct 230 ms 63612 KB Output is correct
29 Correct 226 ms 60208 KB Output is correct
30 Correct 281 ms 29224 KB Output is correct
31 Correct 266 ms 29120 KB Output is correct
32 Correct 258 ms 29320 KB Output is correct
33 Correct 210 ms 20216 KB Output is correct
34 Correct 190 ms 18196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 12 ms 4984 KB Output is correct
4 Correct 7 ms 4988 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 5 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 6 ms 4984 KB Output is correct
17 Correct 7 ms 5112 KB Output is correct
18 Correct 21 ms 4984 KB Output is correct
19 Correct 7 ms 4984 KB Output is correct
20 Correct 5 ms 5084 KB Output is correct
21 Correct 16 ms 5240 KB Output is correct
22 Correct 7 ms 5116 KB Output is correct
23 Correct 0 ms 5112 KB Output is correct
24 Correct 7 ms 5112 KB Output is correct
25 Correct 9 ms 5296 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 7 ms 5112 KB Output is correct
28 Correct 12 ms 5240 KB Output is correct
29 Correct 8 ms 5240 KB Output is correct
30 Correct 9 ms 5240 KB Output is correct
31 Correct 9 ms 5240 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
33 Correct 9 ms 5240 KB Output is correct
34 Correct 7 ms 5112 KB Output is correct
35 Correct 9 ms 5212 KB Output is correct
36 Correct 9 ms 5112 KB Output is correct
37 Correct 9 ms 5116 KB Output is correct
38 Correct 10 ms 5240 KB Output is correct
39 Correct 154 ms 21920 KB Output is correct
40 Correct 141 ms 21984 KB Output is correct
41 Correct 136 ms 22240 KB Output is correct
42 Correct 139 ms 22508 KB Output is correct
43 Correct 119 ms 20344 KB Output is correct
44 Correct 137 ms 26636 KB Output is correct
45 Correct 132 ms 23124 KB Output is correct
46 Correct 129 ms 36728 KB Output is correct
47 Correct 209 ms 29176 KB Output is correct
48 Correct 230 ms 63612 KB Output is correct
49 Correct 226 ms 60208 KB Output is correct
50 Correct 281 ms 29224 KB Output is correct
51 Correct 266 ms 29120 KB Output is correct
52 Correct 258 ms 29320 KB Output is correct
53 Correct 210 ms 20216 KB Output is correct
54 Correct 190 ms 18196 KB Output is correct
55 Correct 24 ms 7280 KB Output is correct
56 Correct 16 ms 6496 KB Output is correct
57 Correct 85 ms 21520 KB Output is correct
58 Correct 68 ms 22888 KB Output is correct
59 Correct 104 ms 38468 KB Output is correct
60 Correct 287 ms 69920 KB Output is correct
61 Correct 239 ms 30804 KB Output is correct
62 Correct 234 ms 33776 KB Output is correct
63 Correct 266 ms 33980 KB Output is correct
64 Correct 492 ms 70796 KB Output is correct
65 Correct 255 ms 29744 KB Output is correct
66 Correct 242 ms 52600 KB Output is correct
67 Correct 194 ms 41824 KB Output is correct
68 Correct 372 ms 45168 KB Output is correct
69 Correct 385 ms 46132 KB Output is correct
70 Correct 370 ms 44184 KB Output is correct