Submission #49440

# Submission time Handle Problem Language Result Execution time Memory
49440 2018-05-29T00:48:34 Z ho94949 Race (IOI11_race) C++17
100 / 100
2712 ms 94096 KB
#include "race.h"
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int MAXN = 202020;
const int MAXK = 1010101;

int N, K;
vector<pair<int, int> > conn[MAXN];
bool visit_centroid[MAXN];

//solve for one vertex
namespace solver
{

set<int> used;
map<int, int> V[MAXK];

//added color & weight
void dfs(int a, int pa, int c, int len, int depth)
{
    if(len>K) return;
    
    used.insert(len);
    if(V[len][c] == 0) V[len][c] = depth;
    else V[len][c] = min(V[len][c], depth);
    
    for(auto tmp: conn[a])
    {
        int x, w; tie(x, w) = tmp;
        if(visit_centroid[x] || x == pa) continue;
        dfs(x, a, c, len + w, depth+1);
    }
}

int solve(int a) 
{
    
    int tp = 0;
    for(auto tmp: conn[a])
    {
        int x, w; tie(x, w) = tmp;
        if(visit_centroid[x]) continue;
        dfs(x, a, ++tp, w, 1);
    }
    used.insert(0);
    V[0][-1] = 0;
    
    int ans = N;
    for(int x: used)
    {
        pair<int, int> min1(N, 0), min2(N, 0);
        for(auto y: V[x])
        {
            auto yrev = make_pair(y.second, y.first);
            if(min1 > yrev)
            {
                min2 = min1;
                min1 = yrev;
            }
            else if(min2 > yrev) min2 = yrev;
        }
        for(auto y: V[K-x])
        {
            if(y.first != min1.second)
                ans = min(ans, y.second+min1.first);
            if(y.first != min2.second)
                ans = min(ans, y.second+min2.first);
        }
    }
    
    //cleanup
    for(int x: used) V[x].clear();
    used.clear();
    
    return ans;
}


}

//context getting centroid
namespace centroid
{
int size[MAXN];
int max_subsize[MAXN];
vector<int> subtree;
void dfs(int a, int pa)
{
    subtree.push_back(a);
    size[a] = 1; max_subsize[a] = 0;
    for(auto tmp: conn[a])
    {
        int x, _; tie(x, _) = tmp;
        if(visit_centroid[x] || x == pa) continue;
        dfs(x, a);
        size[a] += size[x];
        max_subsize[a] = max(max_subsize[a], size[x]);
    }
}
int get_centroid(int x)
{
    subtree.clear();
    dfs(x, -1);
    int minv = subtree.size();
    int mini = -1;
    for(auto y: subtree)
    {
        int v = max((int)subtree.size()-size[y], max_subsize[y]);
        if(minv > v)
        {
            minv = v;
            mini = y;
        }
    }
    return mini;
}
}

int best_path(int _N, int _K, int H[][2], int L[])
{
    N = _N; K = _K;
    for(int i=0; i<N-1; ++i)
    {
        int s = H[i][0], e = H[i][1], x = L[i];
        conn[s].emplace_back(e, x);
        conn[e].emplace_back(s, x);
    }
    int ans = N;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty())
    {
        int v = centroid::get_centroid(Q.front()); Q.pop();
        ans = min(ans, solver::solve(v));
        visit_centroid[v] = true;
        for(auto x: conn[v])
            if(!visit_centroid[x.first])
                Q.push(x.first);
    }
    if(ans == N) ans = -1;
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 52472 KB Output is correct
2 Correct 42 ms 52580 KB Output is correct
3 Correct 43 ms 52656 KB Output is correct
4 Correct 54 ms 52728 KB Output is correct
5 Correct 51 ms 52748 KB Output is correct
6 Correct 44 ms 52784 KB Output is correct
7 Correct 44 ms 52784 KB Output is correct
8 Correct 43 ms 52784 KB Output is correct
9 Correct 43 ms 52792 KB Output is correct
10 Correct 43 ms 52792 KB Output is correct
11 Correct 45 ms 52792 KB Output is correct
12 Correct 42 ms 52832 KB Output is correct
13 Correct 44 ms 52832 KB Output is correct
14 Correct 42 ms 52836 KB Output is correct
15 Correct 44 ms 52836 KB Output is correct
16 Correct 47 ms 52836 KB Output is correct
17 Correct 48 ms 52840 KB Output is correct
18 Correct 44 ms 52840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 52472 KB Output is correct
2 Correct 42 ms 52580 KB Output is correct
3 Correct 43 ms 52656 KB Output is correct
4 Correct 54 ms 52728 KB Output is correct
5 Correct 51 ms 52748 KB Output is correct
6 Correct 44 ms 52784 KB Output is correct
7 Correct 44 ms 52784 KB Output is correct
8 Correct 43 ms 52784 KB Output is correct
9 Correct 43 ms 52792 KB Output is correct
10 Correct 43 ms 52792 KB Output is correct
11 Correct 45 ms 52792 KB Output is correct
12 Correct 42 ms 52832 KB Output is correct
13 Correct 44 ms 52832 KB Output is correct
14 Correct 42 ms 52836 KB Output is correct
15 Correct 44 ms 52836 KB Output is correct
16 Correct 47 ms 52836 KB Output is correct
17 Correct 48 ms 52840 KB Output is correct
18 Correct 44 ms 52840 KB Output is correct
19 Correct 43 ms 52840 KB Output is correct
20 Correct 44 ms 52840 KB Output is correct
21 Correct 45 ms 52840 KB Output is correct
22 Correct 51 ms 52844 KB Output is correct
23 Correct 43 ms 52860 KB Output is correct
24 Correct 43 ms 52860 KB Output is correct
25 Correct 44 ms 52860 KB Output is correct
26 Correct 45 ms 52872 KB Output is correct
27 Correct 44 ms 52872 KB Output is correct
28 Correct 44 ms 52872 KB Output is correct
29 Correct 54 ms 52872 KB Output is correct
30 Correct 50 ms 52872 KB Output is correct
31 Correct 49 ms 52880 KB Output is correct
32 Correct 49 ms 52880 KB Output is correct
33 Correct 51 ms 52880 KB Output is correct
34 Correct 50 ms 52880 KB Output is correct
35 Correct 49 ms 52880 KB Output is correct
36 Correct 48 ms 52880 KB Output is correct
37 Correct 49 ms 52988 KB Output is correct
38 Correct 50 ms 52988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 52472 KB Output is correct
2 Correct 42 ms 52580 KB Output is correct
3 Correct 43 ms 52656 KB Output is correct
4 Correct 54 ms 52728 KB Output is correct
5 Correct 51 ms 52748 KB Output is correct
6 Correct 44 ms 52784 KB Output is correct
7 Correct 44 ms 52784 KB Output is correct
8 Correct 43 ms 52784 KB Output is correct
9 Correct 43 ms 52792 KB Output is correct
10 Correct 43 ms 52792 KB Output is correct
11 Correct 45 ms 52792 KB Output is correct
12 Correct 42 ms 52832 KB Output is correct
13 Correct 44 ms 52832 KB Output is correct
14 Correct 42 ms 52836 KB Output is correct
15 Correct 44 ms 52836 KB Output is correct
16 Correct 47 ms 52836 KB Output is correct
17 Correct 48 ms 52840 KB Output is correct
18 Correct 44 ms 52840 KB Output is correct
19 Correct 481 ms 59072 KB Output is correct
20 Correct 477 ms 59072 KB Output is correct
21 Correct 406 ms 59124 KB Output is correct
22 Correct 442 ms 59128 KB Output is correct
23 Correct 348 ms 59380 KB Output is correct
24 Correct 226 ms 59380 KB Output is correct
25 Correct 471 ms 61816 KB Output is correct
26 Correct 207 ms 64884 KB Output is correct
27 Correct 449 ms 65396 KB Output is correct
28 Correct 798 ms 76840 KB Output is correct
29 Correct 790 ms 76840 KB Output is correct
30 Correct 472 ms 76840 KB Output is correct
31 Correct 458 ms 76840 KB Output is correct
32 Correct 754 ms 76840 KB Output is correct
33 Correct 985 ms 76840 KB Output is correct
34 Correct 825 ms 76840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 52472 KB Output is correct
2 Correct 42 ms 52580 KB Output is correct
3 Correct 43 ms 52656 KB Output is correct
4 Correct 54 ms 52728 KB Output is correct
5 Correct 51 ms 52748 KB Output is correct
6 Correct 44 ms 52784 KB Output is correct
7 Correct 44 ms 52784 KB Output is correct
8 Correct 43 ms 52784 KB Output is correct
9 Correct 43 ms 52792 KB Output is correct
10 Correct 43 ms 52792 KB Output is correct
11 Correct 45 ms 52792 KB Output is correct
12 Correct 42 ms 52832 KB Output is correct
13 Correct 44 ms 52832 KB Output is correct
14 Correct 42 ms 52836 KB Output is correct
15 Correct 44 ms 52836 KB Output is correct
16 Correct 47 ms 52836 KB Output is correct
17 Correct 48 ms 52840 KB Output is correct
18 Correct 44 ms 52840 KB Output is correct
19 Correct 43 ms 52840 KB Output is correct
20 Correct 44 ms 52840 KB Output is correct
21 Correct 45 ms 52840 KB Output is correct
22 Correct 51 ms 52844 KB Output is correct
23 Correct 43 ms 52860 KB Output is correct
24 Correct 43 ms 52860 KB Output is correct
25 Correct 44 ms 52860 KB Output is correct
26 Correct 45 ms 52872 KB Output is correct
27 Correct 44 ms 52872 KB Output is correct
28 Correct 44 ms 52872 KB Output is correct
29 Correct 54 ms 52872 KB Output is correct
30 Correct 50 ms 52872 KB Output is correct
31 Correct 49 ms 52880 KB Output is correct
32 Correct 49 ms 52880 KB Output is correct
33 Correct 51 ms 52880 KB Output is correct
34 Correct 50 ms 52880 KB Output is correct
35 Correct 49 ms 52880 KB Output is correct
36 Correct 48 ms 52880 KB Output is correct
37 Correct 49 ms 52988 KB Output is correct
38 Correct 50 ms 52988 KB Output is correct
39 Correct 481 ms 59072 KB Output is correct
40 Correct 477 ms 59072 KB Output is correct
41 Correct 406 ms 59124 KB Output is correct
42 Correct 442 ms 59128 KB Output is correct
43 Correct 348 ms 59380 KB Output is correct
44 Correct 226 ms 59380 KB Output is correct
45 Correct 471 ms 61816 KB Output is correct
46 Correct 207 ms 64884 KB Output is correct
47 Correct 449 ms 65396 KB Output is correct
48 Correct 798 ms 76840 KB Output is correct
49 Correct 790 ms 76840 KB Output is correct
50 Correct 472 ms 76840 KB Output is correct
51 Correct 458 ms 76840 KB Output is correct
52 Correct 754 ms 76840 KB Output is correct
53 Correct 985 ms 76840 KB Output is correct
54 Correct 825 ms 76840 KB Output is correct
55 Correct 103 ms 76840 KB Output is correct
56 Correct 73 ms 76840 KB Output is correct
57 Correct 292 ms 76840 KB Output is correct
58 Correct 149 ms 76840 KB Output is correct
59 Correct 693 ms 76840 KB Output is correct
60 Correct 2712 ms 94096 KB Output is correct
61 Correct 639 ms 94096 KB Output is correct
62 Correct 755 ms 94096 KB Output is correct
63 Correct 1038 ms 94096 KB Output is correct
64 Correct 2699 ms 94096 KB Output is correct
65 Correct 761 ms 94096 KB Output is correct
66 Correct 1928 ms 94096 KB Output is correct
67 Correct 482 ms 94096 KB Output is correct
68 Correct 1183 ms 94096 KB Output is correct
69 Correct 1133 ms 94096 KB Output is correct
70 Correct 1105 ms 94096 KB Output is correct