답안 #36933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
36933 2017-12-19T08:31:38 Z szawinis 경주 (Race) (IOI11_race) C++14
100 / 100
2177 ms 47808 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1, K = 1e6 + 1;

int n, k, sz[N];
vector<pair<int, int>> g[N];
bool block[N];

void init(int u, int par) {
  sz[u] = 1;
  for(auto p: g[u]) if(p.first != par && !block[p.first]) {
     init(p.first, u);
     sz[u] += sz[p.first];
  }
}

void dfs(int u, int par, int sum, int depth, map<int, int>& store) {
  // cout << u << ' ' << par << endl;
  for(auto p: g[u]) if(p.first != par && !block[p.first]) {
    // cout << u << ' ' << p.first << ' ' << sum + p.second << ' ' << depth + 1 << endl;
     if(!store.count(sum + p.second)) store[sum + p.second] = depth + 1;
     else store[sum + p.second] = min(depth + 1, store[sum + p.second]);
     dfs(p.first, u, sum + p.second, depth + 1, store);
  }
}

int solve(int src) {
  init(src, -1);
  int cen = src;
  while(true) {
    bool found = false;
    for(auto p: g[cen]) {
      if(sz[p.first] < sz[cen] && sz[p.first] << 1 >= sz[src]) { // second condition guarantees no block
        cen = p.first;
        found = true;
        break;
      }
    }
    if(!found) break;
  }
  block[cen] = true;

  int ans = INT_MAX;
  map<int, int> f;
  f[0] = 0;
  for(auto p: g[cen]) if(!block[p.first]) {
    map<int, int> tmp;
    tmp[p.second] = 1;
    dfs(p.first, -1, p.second, 1, tmp); // depth starts out at 1

    for(auto mpp: tmp)
      if(f.count(k - mpp.first))
        ans = min(f[k - mpp.first] + mpp.second, ans);

    for(auto mpp: tmp) {
      if(!f.count(mpp.first)) f.insert(mpp);
      else f[mpp.first] = min(mpp.second, f[mpp.first]);
    }
  }
  // cout << cen << ":\n";
  // for(auto p: f) cout << p.first << ' ' << p.second << endl;
  // cout << endl;
  for(auto p: g[cen]) if(!block[p.first]) ans = min(solve(p.first), ans);
  return ans;
}

int best_path(int _n, int _k, int H[][2], int L[]) {
  n = _n, k = _k;
  for (int i = 0; i < n-1; i++) {
    g[H[i][0]].emplace_back(H[i][1], L[i]);
    g[H[i][1]].emplace_back(H[i][0], L[i]);
  }
  int res = solve(0);
  return (res == INT_MAX ? -1 : res);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 6 ms 5224 KB Output is correct
5 Correct 6 ms 5224 KB Output is correct
6 Correct 5 ms 5344 KB Output is correct
7 Correct 6 ms 5344 KB Output is correct
8 Correct 7 ms 5344 KB Output is correct
9 Correct 7 ms 5344 KB Output is correct
10 Correct 6 ms 5344 KB Output is correct
11 Correct 6 ms 5344 KB Output is correct
12 Correct 6 ms 5344 KB Output is correct
13 Correct 6 ms 5344 KB Output is correct
14 Correct 6 ms 5344 KB Output is correct
15 Correct 6 ms 5344 KB Output is correct
16 Correct 6 ms 5344 KB Output is correct
17 Correct 8 ms 5344 KB Output is correct
18 Correct 6 ms 5344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 6 ms 5224 KB Output is correct
5 Correct 6 ms 5224 KB Output is correct
6 Correct 5 ms 5344 KB Output is correct
7 Correct 6 ms 5344 KB Output is correct
8 Correct 7 ms 5344 KB Output is correct
9 Correct 7 ms 5344 KB Output is correct
10 Correct 6 ms 5344 KB Output is correct
11 Correct 6 ms 5344 KB Output is correct
12 Correct 6 ms 5344 KB Output is correct
13 Correct 6 ms 5344 KB Output is correct
14 Correct 6 ms 5344 KB Output is correct
15 Correct 6 ms 5344 KB Output is correct
16 Correct 6 ms 5344 KB Output is correct
17 Correct 8 ms 5344 KB Output is correct
18 Correct 6 ms 5344 KB Output is correct
19 Correct 6 ms 5344 KB Output is correct
20 Correct 7 ms 5344 KB Output is correct
21 Correct 8 ms 5344 KB Output is correct
22 Correct 9 ms 5348 KB Output is correct
23 Correct 8 ms 5348 KB Output is correct
24 Correct 9 ms 5364 KB Output is correct
25 Correct 8 ms 5364 KB Output is correct
26 Correct 8 ms 5364 KB Output is correct
27 Correct 6 ms 5364 KB Output is correct
28 Correct 10 ms 5480 KB Output is correct
29 Correct 8 ms 5480 KB Output is correct
30 Correct 8 ms 5484 KB Output is correct
31 Correct 10 ms 5484 KB Output is correct
32 Correct 10 ms 5484 KB Output is correct
33 Correct 10 ms 5484 KB Output is correct
34 Correct 8 ms 5484 KB Output is correct
35 Correct 8 ms 5484 KB Output is correct
36 Correct 7 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 8 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 6 ms 5224 KB Output is correct
5 Correct 6 ms 5224 KB Output is correct
6 Correct 5 ms 5344 KB Output is correct
7 Correct 6 ms 5344 KB Output is correct
8 Correct 7 ms 5344 KB Output is correct
9 Correct 7 ms 5344 KB Output is correct
10 Correct 6 ms 5344 KB Output is correct
11 Correct 6 ms 5344 KB Output is correct
12 Correct 6 ms 5344 KB Output is correct
13 Correct 6 ms 5344 KB Output is correct
14 Correct 6 ms 5344 KB Output is correct
15 Correct 6 ms 5344 KB Output is correct
16 Correct 6 ms 5344 KB Output is correct
17 Correct 8 ms 5344 KB Output is correct
18 Correct 6 ms 5344 KB Output is correct
19 Correct 377 ms 10764 KB Output is correct
20 Correct 350 ms 10764 KB Output is correct
21 Correct 324 ms 10876 KB Output is correct
22 Correct 284 ms 10876 KB Output is correct
23 Correct 517 ms 11388 KB Output is correct
24 Correct 228 ms 11388 KB Output is correct
25 Correct 328 ms 17788 KB Output is correct
26 Correct 152 ms 17788 KB Output is correct
27 Correct 503 ms 17788 KB Output is correct
28 Correct 1736 ms 47808 KB Output is correct
29 Correct 1845 ms 47808 KB Output is correct
30 Correct 473 ms 47808 KB Output is correct
31 Correct 495 ms 47808 KB Output is correct
32 Correct 604 ms 47808 KB Output is correct
33 Correct 1000 ms 47808 KB Output is correct
34 Correct 2091 ms 47808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 6 ms 5224 KB Output is correct
5 Correct 6 ms 5224 KB Output is correct
6 Correct 5 ms 5344 KB Output is correct
7 Correct 6 ms 5344 KB Output is correct
8 Correct 7 ms 5344 KB Output is correct
9 Correct 7 ms 5344 KB Output is correct
10 Correct 6 ms 5344 KB Output is correct
11 Correct 6 ms 5344 KB Output is correct
12 Correct 6 ms 5344 KB Output is correct
13 Correct 6 ms 5344 KB Output is correct
14 Correct 6 ms 5344 KB Output is correct
15 Correct 6 ms 5344 KB Output is correct
16 Correct 6 ms 5344 KB Output is correct
17 Correct 8 ms 5344 KB Output is correct
18 Correct 6 ms 5344 KB Output is correct
19 Correct 6 ms 5344 KB Output is correct
20 Correct 7 ms 5344 KB Output is correct
21 Correct 8 ms 5344 KB Output is correct
22 Correct 9 ms 5348 KB Output is correct
23 Correct 8 ms 5348 KB Output is correct
24 Correct 9 ms 5364 KB Output is correct
25 Correct 8 ms 5364 KB Output is correct
26 Correct 8 ms 5364 KB Output is correct
27 Correct 6 ms 5364 KB Output is correct
28 Correct 10 ms 5480 KB Output is correct
29 Correct 8 ms 5480 KB Output is correct
30 Correct 8 ms 5484 KB Output is correct
31 Correct 10 ms 5484 KB Output is correct
32 Correct 10 ms 5484 KB Output is correct
33 Correct 10 ms 5484 KB Output is correct
34 Correct 8 ms 5484 KB Output is correct
35 Correct 8 ms 5484 KB Output is correct
36 Correct 7 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 8 ms 5484 KB Output is correct
39 Correct 377 ms 10764 KB Output is correct
40 Correct 350 ms 10764 KB Output is correct
41 Correct 324 ms 10876 KB Output is correct
42 Correct 284 ms 10876 KB Output is correct
43 Correct 517 ms 11388 KB Output is correct
44 Correct 228 ms 11388 KB Output is correct
45 Correct 328 ms 17788 KB Output is correct
46 Correct 152 ms 17788 KB Output is correct
47 Correct 503 ms 17788 KB Output is correct
48 Correct 1736 ms 47808 KB Output is correct
49 Correct 1845 ms 47808 KB Output is correct
50 Correct 473 ms 47808 KB Output is correct
51 Correct 495 ms 47808 KB Output is correct
52 Correct 604 ms 47808 KB Output is correct
53 Correct 1000 ms 47808 KB Output is correct
54 Correct 2091 ms 47808 KB Output is correct
55 Correct 49 ms 47808 KB Output is correct
56 Correct 29 ms 47808 KB Output is correct
57 Correct 220 ms 47808 KB Output is correct
58 Correct 76 ms 47808 KB Output is correct
59 Correct 646 ms 47808 KB Output is correct
60 Correct 2117 ms 47808 KB Output is correct
61 Correct 659 ms 47808 KB Output is correct
62 Correct 537 ms 47808 KB Output is correct
63 Correct 655 ms 47808 KB Output is correct
64 Correct 2101 ms 47808 KB Output is correct
65 Correct 1743 ms 47808 KB Output is correct
66 Correct 2177 ms 47808 KB Output is correct
67 Correct 261 ms 47808 KB Output is correct
68 Correct 895 ms 47808 KB Output is correct
69 Correct 917 ms 47808 KB Output is correct
70 Correct 890 ms 47808 KB Output is correct