Submission #63015

# Submission time Handle Problem Language Result Execution time Memory
63015 2018-07-31T09:51:20 Z zubec Race (IOI11_race) C++14
100 / 100
1818 ms 80492 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 200100;

vector <pair<int, int> > g[200100];

int needLen, n, sz[N], pr[N];
bool used[N];

int ans = 1e9;
map<int, int> mp1;

int fnd_cntr(int v, int p, int szcmp){
    int pos = -1;
    bool bb = 0;
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i].first;
        if (to == p || used[to])
            continue;
        if (sz[to]-1 >= szcmp/2){
            bb = 1;
            pos = to;
            break;
        }
    }
    if (bb == 0)
        return v;
    return fnd_cntr(pos, v, szcmp);
}

void dfs1(int v, int p, int len, int pth){
    sz[v] = 1;
    pr[v] = p;
    if (mp1.find(needLen-len)!=mp1.end()){
        ans = min(ans, mp1[needLen-len]+pth);
    }
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i].first, curlen = g[v][i].second;
        if (to == p || used[to])
            continue;
        dfs1(to, v, len+curlen, pth+1);
        sz[v] += sz[to];
    }
}

void dfs2(int v, int p, int len, int pth){
    if (mp1.find(len) != mp1.end())
        mp1[len] = min(mp1[len], pth); else
        mp1[len] = pth;
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i].first, curlen = g[v][i].second;
        if (to == p || used[to])
            continue;
        dfs2(to, v, len+curlen, pth+1);
    }
}

void build(int v, int p){
    if (used[v])
        return;
    int cntr = fnd_cntr(v, pr[v], sz[v]);
    used[cntr] = 1;
    mp1[0] = 0;
    for (int i = 0; i < g[cntr].size(); i++){
        int to = g[cntr][i].first, len = g[cntr][i].second;
        if (used[to])
            continue;
        dfs1(to, 0, len, 1);
        dfs2(to, 0, len, 1);
    }
    if (mp1.find(needLen) != mp1.end())
        ans = min(ans, mp1[needLen]);
    mp1.clear();
    for (int i = 0; i < g[cntr].size(); i++){
        int to = g[cntr][i].first;
        if (!used[to])
            build(to, cntr);
    }
}

int best_path(int N, int K, int H[][2], int L[]){
    needLen = K;
    n = N;
    for (int i = 1; i < n; i++){
        int u = H[i-1][0], v = H[i-1][1], len = L[i-1];
        g[u].push_back({v, len});
        g[v].push_back({u, len});
    }
    dfs1(1, 0, 0, 0);
    mp1.clear();
    build(1, 0);
    if (ans == 1e9)
        ans = -1;
    return ans;
}

Compilation message

race.cpp: In function 'int fnd_cntr(int, int, int)':
race.cpp:18:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:39:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:52:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void build(int, int)':
race.cpp:66:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[cntr].size(); i++){
                     ~~^~~~~~~~~~~~~~~~
race.cpp:76:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[cntr].size(); i++){
                     ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 9 ms 5172 KB Output is correct
4 Correct 7 ms 5212 KB Output is correct
5 Correct 9 ms 5292 KB Output is correct
6 Correct 9 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5420 KB Output is correct
9 Correct 8 ms 5420 KB Output is correct
10 Correct 8 ms 5420 KB Output is correct
11 Correct 7 ms 5420 KB Output is correct
12 Correct 7 ms 5420 KB Output is correct
13 Correct 9 ms 5420 KB Output is correct
14 Correct 8 ms 5420 KB Output is correct
15 Correct 8 ms 5420 KB Output is correct
16 Correct 10 ms 5420 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 8 ms 5420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 9 ms 5172 KB Output is correct
4 Correct 7 ms 5212 KB Output is correct
5 Correct 9 ms 5292 KB Output is correct
6 Correct 9 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5420 KB Output is correct
9 Correct 8 ms 5420 KB Output is correct
10 Correct 8 ms 5420 KB Output is correct
11 Correct 7 ms 5420 KB Output is correct
12 Correct 7 ms 5420 KB Output is correct
13 Correct 9 ms 5420 KB Output is correct
14 Correct 8 ms 5420 KB Output is correct
15 Correct 8 ms 5420 KB Output is correct
16 Correct 10 ms 5420 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 8 ms 5420 KB Output is correct
19 Correct 6 ms 5420 KB Output is correct
20 Correct 7 ms 5420 KB Output is correct
21 Correct 8 ms 5432 KB Output is correct
22 Correct 9 ms 5444 KB Output is correct
23 Correct 9 ms 5460 KB Output is correct
24 Correct 9 ms 5476 KB Output is correct
25 Correct 8 ms 5492 KB Output is correct
26 Correct 10 ms 5512 KB Output is correct
27 Correct 7 ms 5528 KB Output is correct
28 Correct 8 ms 5548 KB Output is correct
29 Correct 8 ms 5560 KB Output is correct
30 Correct 9 ms 5832 KB Output is correct
31 Correct 9 ms 5832 KB Output is correct
32 Correct 10 ms 5832 KB Output is correct
33 Correct 12 ms 5832 KB Output is correct
34 Correct 11 ms 5832 KB Output is correct
35 Correct 8 ms 5832 KB Output is correct
36 Correct 8 ms 5832 KB Output is correct
37 Correct 10 ms 5832 KB Output is correct
38 Correct 10 ms 5832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 9 ms 5172 KB Output is correct
4 Correct 7 ms 5212 KB Output is correct
5 Correct 9 ms 5292 KB Output is correct
6 Correct 9 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5420 KB Output is correct
9 Correct 8 ms 5420 KB Output is correct
10 Correct 8 ms 5420 KB Output is correct
11 Correct 7 ms 5420 KB Output is correct
12 Correct 7 ms 5420 KB Output is correct
13 Correct 9 ms 5420 KB Output is correct
14 Correct 8 ms 5420 KB Output is correct
15 Correct 8 ms 5420 KB Output is correct
16 Correct 10 ms 5420 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 8 ms 5420 KB Output is correct
19 Correct 500 ms 12780 KB Output is correct
20 Correct 399 ms 14112 KB Output is correct
21 Correct 440 ms 15464 KB Output is correct
22 Correct 301 ms 16944 KB Output is correct
23 Correct 598 ms 18712 KB Output is correct
24 Correct 234 ms 19892 KB Output is correct
25 Correct 393 ms 30572 KB Output is correct
26 Correct 173 ms 32956 KB Output is correct
27 Correct 541 ms 32956 KB Output is correct
28 Correct 1658 ms 62680 KB Output is correct
29 Correct 1624 ms 62680 KB Output is correct
30 Correct 574 ms 62680 KB Output is correct
31 Correct 558 ms 62680 KB Output is correct
32 Correct 562 ms 62680 KB Output is correct
33 Correct 839 ms 62680 KB Output is correct
34 Correct 1566 ms 62680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 9 ms 5172 KB Output is correct
4 Correct 7 ms 5212 KB Output is correct
5 Correct 9 ms 5292 KB Output is correct
6 Correct 9 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5420 KB Output is correct
9 Correct 8 ms 5420 KB Output is correct
10 Correct 8 ms 5420 KB Output is correct
11 Correct 7 ms 5420 KB Output is correct
12 Correct 7 ms 5420 KB Output is correct
13 Correct 9 ms 5420 KB Output is correct
14 Correct 8 ms 5420 KB Output is correct
15 Correct 8 ms 5420 KB Output is correct
16 Correct 10 ms 5420 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 8 ms 5420 KB Output is correct
19 Correct 6 ms 5420 KB Output is correct
20 Correct 7 ms 5420 KB Output is correct
21 Correct 8 ms 5432 KB Output is correct
22 Correct 9 ms 5444 KB Output is correct
23 Correct 9 ms 5460 KB Output is correct
24 Correct 9 ms 5476 KB Output is correct
25 Correct 8 ms 5492 KB Output is correct
26 Correct 10 ms 5512 KB Output is correct
27 Correct 7 ms 5528 KB Output is correct
28 Correct 8 ms 5548 KB Output is correct
29 Correct 8 ms 5560 KB Output is correct
30 Correct 9 ms 5832 KB Output is correct
31 Correct 9 ms 5832 KB Output is correct
32 Correct 10 ms 5832 KB Output is correct
33 Correct 12 ms 5832 KB Output is correct
34 Correct 11 ms 5832 KB Output is correct
35 Correct 8 ms 5832 KB Output is correct
36 Correct 8 ms 5832 KB Output is correct
37 Correct 10 ms 5832 KB Output is correct
38 Correct 10 ms 5832 KB Output is correct
39 Correct 500 ms 12780 KB Output is correct
40 Correct 399 ms 14112 KB Output is correct
41 Correct 440 ms 15464 KB Output is correct
42 Correct 301 ms 16944 KB Output is correct
43 Correct 598 ms 18712 KB Output is correct
44 Correct 234 ms 19892 KB Output is correct
45 Correct 393 ms 30572 KB Output is correct
46 Correct 173 ms 32956 KB Output is correct
47 Correct 541 ms 32956 KB Output is correct
48 Correct 1658 ms 62680 KB Output is correct
49 Correct 1624 ms 62680 KB Output is correct
50 Correct 574 ms 62680 KB Output is correct
51 Correct 558 ms 62680 KB Output is correct
52 Correct 562 ms 62680 KB Output is correct
53 Correct 839 ms 62680 KB Output is correct
54 Correct 1566 ms 62680 KB Output is correct
55 Correct 37 ms 62680 KB Output is correct
56 Correct 27 ms 62680 KB Output is correct
57 Correct 231 ms 62680 KB Output is correct
58 Correct 89 ms 62680 KB Output is correct
59 Correct 495 ms 62680 KB Output is correct
60 Correct 1413 ms 62680 KB Output is correct
61 Correct 652 ms 62680 KB Output is correct
62 Correct 650 ms 62680 KB Output is correct
63 Correct 631 ms 62680 KB Output is correct
64 Correct 1818 ms 62680 KB Output is correct
65 Correct 1796 ms 63052 KB Output is correct
66 Correct 1658 ms 80492 KB Output is correct
67 Correct 286 ms 80492 KB Output is correct
68 Correct 1010 ms 80492 KB Output is correct
69 Correct 958 ms 80492 KB Output is correct
70 Correct 888 ms 80492 KB Output is correct