Submission #345112

# Submission time Handle Problem Language Result Execution time Memory
345112 2021-01-07T03:17:50 Z casperwang Race (IOI11_race) C++14
100 / 100
1108 ms 40716 KB
#include <bits/stdc++.h>
#include "race.h"
#define pb emplace_back
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }

const int MAXN = 200000;
vector <vector<pii>> path;
vector <bool> vis;
vector <int> sze;
vector <pii> d;
int n;
int ans, k;

void init(int N) {
  path.clear();
  path.resize(N);
  vis.clear();
  vis.resize(N);
  sze.clear();
  sze.resize(N);
  d.clear();
  ans = MAXN;
}

int dfs_sze(int now, int par) {
  sze[now] = 1;
  for (pii e : path[now]) {
    if (vis[e.ff] || e.ff == par) continue;
    sze[now] += dfs_sze(e.ff, now);
  }
  return sze[now];
}

int fnd_cen(int now, int par) {
  for (pii e : path[now]) {
    if (vis[e.ff] || e.ff == par) continue;
    if (sze[e.ff] * 2 > n)
      return fnd_cen(e.ff, now);
  }
  return now;
}

void dfs_dis(int now, int par, int sum, int dth) {
  if (sum > k) return;
  d.pb(sum, dth);
  for (pii e : path[now]) {
    if (vis[e.ff] || e.ff == par) continue;
    dfs_dis(e.ff, now, sum+e.ss, dth+1);
  }
}

void solve(int now, int par) {
  n = dfs_sze(now, -1);
  int r = fnd_cen(now, -1);
  vis[r] = 1;
  unordered_map <int,int> dis;
  for (pii e : path[r]) {
    if (vis[e.ff] || e.ff == par) continue;
    dfs_dis(e.ff, -1, e.ss, 1);
    for (pii it : d) {
      if (dis.count(k-it.ff)) {
        ans = min(ans, dis[k-it.ff] + it.ss);
      } else if (it.ff == k) {
        ans = min(ans, it.ss);
      }
    }
    for (pii it : d) {
      if (!dis.count(it.ff)) {
        dis[it.ff] = it.ss;
       } else {
        dis[it.ff] = min(dis[it.ff], it.ss);
       }
    }
    d.clear();
  }
  dis.clear();
  for (pii e : path[r]) {
    if (vis[e.ff] || e.ff == par) continue;
    solve(e.ff, now);
  }
  return;
}

int best_path(int N, int K, int H[][2], int L[]) {
  init(N);
  k = K;
  for (int i = 0; i < N-1; i++) {
    path[H[i][0]].pb(H[i][1], L[i]);
    path[H[i][1]].pb(H[i][0], L[i]);
  }
  solve(0, -1);
  return ans == MAXN ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 360 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 3 ms 492 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
29 Correct 3 ms 492 KB Output is correct
30 Correct 3 ms 492 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 2 ms 492 KB Output is correct
33 Correct 2 ms 492 KB Output is correct
34 Correct 2 ms 492 KB Output is correct
35 Correct 2 ms 492 KB Output is correct
36 Correct 3 ms 492 KB Output is correct
37 Correct 2 ms 492 KB Output is correct
38 Correct 3 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 219 ms 8460 KB Output is correct
20 Correct 250 ms 9532 KB Output is correct
21 Correct 246 ms 9784 KB Output is correct
22 Correct 227 ms 9708 KB Output is correct
23 Correct 138 ms 9668 KB Output is correct
24 Correct 114 ms 9456 KB Output is correct
25 Correct 247 ms 13036 KB Output is correct
26 Correct 172 ms 17264 KB Output is correct
27 Correct 295 ms 18668 KB Output is correct
28 Correct 434 ms 33080 KB Output is correct
29 Correct 494 ms 32044 KB Output is correct
30 Correct 313 ms 18656 KB Output is correct
31 Correct 286 ms 18796 KB Output is correct
32 Correct 355 ms 18784 KB Output is correct
33 Correct 423 ms 17516 KB Output is correct
34 Correct 328 ms 18412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 360 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 3 ms 492 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
29 Correct 3 ms 492 KB Output is correct
30 Correct 3 ms 492 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 2 ms 492 KB Output is correct
33 Correct 2 ms 492 KB Output is correct
34 Correct 2 ms 492 KB Output is correct
35 Correct 2 ms 492 KB Output is correct
36 Correct 3 ms 492 KB Output is correct
37 Correct 2 ms 492 KB Output is correct
38 Correct 3 ms 492 KB Output is correct
39 Correct 219 ms 8460 KB Output is correct
40 Correct 250 ms 9532 KB Output is correct
41 Correct 246 ms 9784 KB Output is correct
42 Correct 227 ms 9708 KB Output is correct
43 Correct 138 ms 9668 KB Output is correct
44 Correct 114 ms 9456 KB Output is correct
45 Correct 247 ms 13036 KB Output is correct
46 Correct 172 ms 17264 KB Output is correct
47 Correct 295 ms 18668 KB Output is correct
48 Correct 434 ms 33080 KB Output is correct
49 Correct 494 ms 32044 KB Output is correct
50 Correct 313 ms 18656 KB Output is correct
51 Correct 286 ms 18796 KB Output is correct
52 Correct 355 ms 18784 KB Output is correct
53 Correct 423 ms 17516 KB Output is correct
54 Correct 328 ms 18412 KB Output is correct
55 Correct 22 ms 1388 KB Output is correct
56 Correct 14 ms 1260 KB Output is correct
57 Correct 159 ms 10092 KB Output is correct
58 Correct 51 ms 9184 KB Output is correct
59 Correct 269 ms 20352 KB Output is correct
60 Correct 1108 ms 40716 KB Output is correct
61 Correct 351 ms 18924 KB Output is correct
62 Correct 368 ms 18796 KB Output is correct
63 Correct 484 ms 18924 KB Output is correct
64 Correct 972 ms 24760 KB Output is correct
65 Correct 325 ms 19600 KB Output is correct
66 Correct 795 ms 30188 KB Output is correct
67 Correct 232 ms 19420 KB Output is correct
68 Correct 548 ms 23824 KB Output is correct
69 Correct 487 ms 24004 KB Output is correct
70 Correct 462 ms 23096 KB Output is correct