Submission #222693

# Submission time Handle Problem Language Result Execution time Memory
222693 2020-04-13T17:28:50 Z xiaowuc1 Race (IOI11_race) C++17
100 / 100
1447 ms 42872 KB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> pll;

int ret;
int n, k;
vector<pii> edges[200000];
bool centroidseen[200000];
int treesz[200000];

void dfsfromcentroid(int curr, int par, map<int, int>& dp, int dist, int roads) {
  if(dist > k) return;
  if(!dp.count(dist)) dp[dist] = roads;
  else dp[dist] = min(dp[dist], roads);
  for(pii out: edges[curr]) {
    if(centroidseen[out.first] || out.first == par) continue;
    dfsfromcentroid(out.first, curr, dp, dist + out.second, roads + 1);
  }
}
void dfsforsz(int curr, int par) {
  treesz[curr] = 1;
  for(pii out: edges[curr]) {
    int child = out.first;
    if(centroidseen[child] || child == par) continue;
    dfsforsz(child, curr);
    treesz[curr] += treesz[child];
  }
}
void centroiddecomp(int curr) {
  if(centroidseen[curr]) return;
  dfsforsz(curr, -1);
  int totalsz = treesz[curr];
  int centroidpar = -1;
  while(true) {
    int biggestchildsz = -1;
    int biggestchild = -1;
    for(pii out: edges[curr]) {
      int cand = out.first;
      if(centroidseen[cand] || cand == centroidpar) continue;
      if(treesz[cand] > biggestchildsz) {
        biggestchildsz = treesz[cand];
        biggestchild = cand;
      }
    }
    if(2 * biggestchildsz > totalsz) {
      centroidpar = curr;
      curr = biggestchild;
    }
    else {
      break;
    }
  }
  // curr is the centroid
  {
    map<int, int> dp;
    for(pii edge: edges[curr]) {
      if(centroidseen[edge.first]) continue;
      map<int, int> childdp;
      dfsfromcentroid(edge.first, curr, childdp, edge.second, 1);
      for(auto out: childdp) {
        if(out.first == k) ret = min(ret, out.second);
        if(dp.count(k - out.first)) ret = min(ret, out.second + dp[k - out.first]);
      }
      for(auto out: childdp) {
        if(!dp.count(out.first)) dp[out.first] = out.second;
        else dp[out.first] = min(dp[out.first], out.second);
      }
    }
  }
  centroidseen[curr] = true;
  for(pii out: edges[curr]) centroiddecomp(out.first);
}

int best_path(int N, int K, int H[][2], int L[]) {
  n = N;
  k = K;
  ret = n+1;
  for(int i = 0; i < n-1; i++) {
    edges[H[i][0]].emplace_back(H[i][1], L[i]);
    edges[H[i][1]].emplace_back(H[i][0], L[i]);
  }
  centroiddecomp(0);
  if(ret > n) ret = -1;
  return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4992 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 8 ms 4992 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 8 ms 4992 KB Output is correct
14 Correct 8 ms 4992 KB Output is correct
15 Correct 8 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4992 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 8 ms 4992 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 8 ms 4992 KB Output is correct
14 Correct 8 ms 4992 KB Output is correct
15 Correct 8 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 8 ms 4992 KB Output is correct
21 Correct 11 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 11 ms 5248 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 11 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 8 ms 5120 KB Output is correct
28 Correct 18 ms 5168 KB Output is correct
29 Correct 10 ms 5120 KB Output is correct
30 Correct 10 ms 5120 KB Output is correct
31 Correct 10 ms 5120 KB Output is correct
32 Correct 10 ms 5120 KB Output is correct
33 Correct 10 ms 5120 KB Output is correct
34 Correct 9 ms 5120 KB Output is correct
35 Correct 9 ms 5120 KB Output is correct
36 Correct 9 ms 5120 KB Output is correct
37 Correct 9 ms 5120 KB Output is correct
38 Correct 10 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4992 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 8 ms 4992 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 8 ms 4992 KB Output is correct
14 Correct 8 ms 4992 KB Output is correct
15 Correct 8 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 274 ms 10488 KB Output is correct
20 Correct 260 ms 10360 KB Output is correct
21 Correct 268 ms 10444 KB Output is correct
22 Correct 261 ms 10616 KB Output is correct
23 Correct 116 ms 10616 KB Output is correct
24 Correct 107 ms 10488 KB Output is correct
25 Correct 177 ms 13184 KB Output is correct
26 Correct 118 ms 17400 KB Output is correct
27 Correct 286 ms 18808 KB Output is correct
28 Correct 371 ms 30456 KB Output is correct
29 Correct 347 ms 29304 KB Output is correct
30 Correct 301 ms 18808 KB Output is correct
31 Correct 292 ms 18808 KB Output is correct
32 Correct 331 ms 19040 KB Output is correct
33 Correct 470 ms 17760 KB Output is correct
34 Correct 222 ms 18528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4992 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 8 ms 4992 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 8 ms 4992 KB Output is correct
14 Correct 8 ms 4992 KB Output is correct
15 Correct 8 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 8 ms 4992 KB Output is correct
21 Correct 11 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 11 ms 5248 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 11 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 8 ms 5120 KB Output is correct
28 Correct 18 ms 5168 KB Output is correct
29 Correct 10 ms 5120 KB Output is correct
30 Correct 10 ms 5120 KB Output is correct
31 Correct 10 ms 5120 KB Output is correct
32 Correct 10 ms 5120 KB Output is correct
33 Correct 10 ms 5120 KB Output is correct
34 Correct 9 ms 5120 KB Output is correct
35 Correct 9 ms 5120 KB Output is correct
36 Correct 9 ms 5120 KB Output is correct
37 Correct 9 ms 5120 KB Output is correct
38 Correct 10 ms 5120 KB Output is correct
39 Correct 274 ms 10488 KB Output is correct
40 Correct 260 ms 10360 KB Output is correct
41 Correct 268 ms 10444 KB Output is correct
42 Correct 261 ms 10616 KB Output is correct
43 Correct 116 ms 10616 KB Output is correct
44 Correct 107 ms 10488 KB Output is correct
45 Correct 177 ms 13184 KB Output is correct
46 Correct 118 ms 17400 KB Output is correct
47 Correct 286 ms 18808 KB Output is correct
48 Correct 371 ms 30456 KB Output is correct
49 Correct 347 ms 29304 KB Output is correct
50 Correct 301 ms 18808 KB Output is correct
51 Correct 292 ms 18808 KB Output is correct
52 Correct 331 ms 19040 KB Output is correct
53 Correct 470 ms 17760 KB Output is correct
54 Correct 222 ms 18528 KB Output is correct
55 Correct 34 ms 5888 KB Output is correct
56 Correct 25 ms 5632 KB Output is correct
57 Correct 181 ms 12024 KB Output is correct
58 Correct 64 ms 11628 KB Output is correct
59 Correct 509 ms 23520 KB Output is correct
60 Correct 1437 ms 42872 KB Output is correct
61 Correct 406 ms 19192 KB Output is correct
62 Correct 431 ms 19064 KB Output is correct
63 Correct 546 ms 19180 KB Output is correct
64 Correct 1447 ms 25932 KB Output is correct
65 Correct 282 ms 19704 KB Output is correct
66 Correct 912 ms 27772 KB Output is correct
67 Correct 170 ms 19556 KB Output is correct
68 Correct 596 ms 23288 KB Output is correct
69 Correct 596 ms 23528 KB Output is correct
70 Correct 561 ms 22680 KB Output is correct