답안 #253027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253027 2020-07-26T16:30:56 Z Kubin 경주 (Race) (IOI11_race) C++17
100 / 100
1688 ms 87668 KB
#include <bits/stdc++.h>

using namespace std;

vector<vector<size_t>> adj;
vector<vector<pair<size_t, int>>> graph;

const int oo = INT_MAX / 3;

int& rd(unordered_map<int, int>& m, int key)
{
    auto it = m.find(key);
    if(it == m.end()) it = m.emplace_hint(it, key, +oo);
    return it->second;
}

void mini(unordered_map<int, int>& m, int key, int value)
{
    rd(m, key) = min(rd(m, key), value);
}

size_t gl_vertices;
vector<size_t> gl_parent, gl_subsize;
vector<bool> gl_lock;

void dfs_map_paths(size_t u, int s, int d, unordered_map<int, int>& m,
                   size_t lock = SIZE_MAX)
{
    if(d > +oo) return;
    mini(m, s, d);
    for(auto [v, w] : graph[u])
        if(v != lock and not gl_lock[v])
            dfs_map_paths(v, s + w, d + 1, m, u);
}

void dfs_root(size_t u, size_t glock = SIZE_MAX, size_t lock = SIZE_MAX)
{
    gl_vertices++;
    gl_subsize[u] = 1;
    for(auto v : adj[u])
        if(v != lock and not gl_lock[v])
            gl_parent[v] = u, dfs_root(v, glock, u), gl_subsize[u] += gl_subsize[v];
}

size_t subtree_size(size_t from, size_t to)
{
    return from == gl_parent[to] ? gl_subsize[to] : gl_vertices - gl_subsize[from];
}

size_t find_centroid(size_t init)
{
    gl_vertices = 0;

    dfs_root(init);
    gl_parent[init] = SIZE_MAX;

    size_t u = init;
    while(true)
    {
        bool any = false;
        for(auto v : adj[u])
            if(not gl_lock[v] and subtree_size(u, v) > subtree_size(v, u))
                { any = true; u = v; break; }
        if(not any) break;
    }

    return u;
}


int solve(size_t init, int k)
{
    auto fix = find_centroid(init);

    int result = +oo;

    unordered_map<int, int> pre = {{0, 0}};
    pre.reserve(4 * gl_vertices);
    gl_lock[fix] = true;
    for(auto [v, w] : graph[fix])
      if(not gl_lock[v])
    {
        unordered_map<int, int> cur;
        cur.reserve(4 * subtree_size(fix, v));
        dfs_map_paths(v, w, 1, cur);

        for(auto [s, d] : cur)
            result = min(result, d + rd(pre, k - s));
        for(auto [s, d] : cur)
            mini(pre, s, d);
    }

    for(auto v : adj[fix])
        if(not gl_lock[v])
            result = min(result, solve(v, k));

    return result;
}


int best_path(int _n, int k, int H[][2], int L[])
{
    const size_t n = _n;

    graph.resize(n);
    adj.resize(n);
    for(size_t i = 0; i < n - 1; i++)
    {
        graph[H[i][0]].emplace_back(H[i][1], L[i]);
        graph[H[i][1]].emplace_back(H[i][0], L[i]);
        adj[H[i][0]].push_back(H[i][1]);
        adj[H[i][1]].push_back(H[i][0]);
    }

    gl_lock.resize(n);
    gl_parent.resize(n);
    gl_subsize.resize(n);
    int result = solve(0, k);

    return result >= +oo ? -1 : result;
}

#ifdef XHOVA
#define MAX_N 500000

static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;

inline
void my_assert(int e) {if (!e) abort();}

void read_input()
{
  int i;
  my_assert(2==scanf("%d %d",&N,&K));
  for(i=0; i<N-1; i++)
    my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
  my_assert(1==scanf("%d",&solution));
}

int main()
{
  int ans;
  read_input();
  ans = best_path(N,K,H,L);
  if(ans==solution)
    printf("Correct.\n");
  else
    printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);

  return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 4 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 640 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 357 ms 22464 KB Output is correct
20 Correct 369 ms 22720 KB Output is correct
21 Correct 346 ms 22724 KB Output is correct
22 Correct 353 ms 22576 KB Output is correct
23 Correct 450 ms 22900 KB Output is correct
24 Correct 246 ms 20520 KB Output is correct
25 Correct 327 ms 26820 KB Output is correct
26 Correct 187 ms 31304 KB Output is correct
27 Correct 566 ms 42744 KB Output is correct
28 Correct 1688 ms 87668 KB Output is correct
29 Correct 1623 ms 86372 KB Output is correct
30 Correct 570 ms 42744 KB Output is correct
31 Correct 582 ms 42872 KB Output is correct
32 Correct 726 ms 42872 KB Output is correct
33 Correct 848 ms 47092 KB Output is correct
34 Correct 1503 ms 68236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 4 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 640 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
39 Correct 357 ms 22464 KB Output is correct
40 Correct 369 ms 22720 KB Output is correct
41 Correct 346 ms 22724 KB Output is correct
42 Correct 353 ms 22576 KB Output is correct
43 Correct 450 ms 22900 KB Output is correct
44 Correct 246 ms 20520 KB Output is correct
45 Correct 327 ms 26820 KB Output is correct
46 Correct 187 ms 31304 KB Output is correct
47 Correct 566 ms 42744 KB Output is correct
48 Correct 1688 ms 87668 KB Output is correct
49 Correct 1623 ms 86372 KB Output is correct
50 Correct 570 ms 42744 KB Output is correct
51 Correct 582 ms 42872 KB Output is correct
52 Correct 726 ms 42872 KB Output is correct
53 Correct 848 ms 47092 KB Output is correct
54 Correct 1503 ms 68236 KB Output is correct
55 Correct 29 ms 3432 KB Output is correct
56 Correct 21 ms 2652 KB Output is correct
57 Correct 217 ms 23960 KB Output is correct
58 Correct 86 ms 20712 KB Output is correct
59 Correct 447 ms 40504 KB Output is correct
60 Correct 1439 ms 84196 KB Output is correct
61 Correct 601 ms 44536 KB Output is correct
62 Correct 565 ms 42848 KB Output is correct
63 Correct 696 ms 43000 KB Output is correct
64 Correct 1283 ms 63452 KB Output is correct
65 Correct 1305 ms 70232 KB Output is correct
66 Correct 1655 ms 83944 KB Output is correct
67 Correct 326 ms 42464 KB Output is correct
68 Correct 783 ms 61784 KB Output is correct
69 Correct 860 ms 62692 KB Output is correct
70 Correct 727 ms 58972 KB Output is correct