답안 #95097

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95097 2019-01-27T10:04:41 Z Alexa2001 경주 (Race) (IOI11_race) C++17
100 / 100
1202 ms 56740 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 5, inf = 1e9;

int w[Nmax], best[10 * Nmax], All, K;
bool used[Nmax];

vector<int> v[Nmax], c[Nmax];



void dfs(int node, int dad = -1)
{
    w[node] = 1;
    for(auto it : v[node])
        if(!used[it] && it != dad)
        {
            dfs(it, node);
            w[node] += w[it];
        }
}

pair<int,int> centroid(int node, int dad = -1)
{
    int act = All - w[node];
    pair<int,int> ans = {All+1, All};

    for(auto it : v[node])
        if(!used[it] && it != dad)
        {
            ans = min(ans, centroid(it, node));
            act = max(act, w[it]);
        }
    ans = min(ans, {act, node});
    return ans;
}

void dfs2(int node, int dad, int up, int level, vector< pair<int,int> > &now)
{
    if(up <= K)
        now.push_back({up, level});

    int i, x, cost;
    for(i=0; i<v[node].size(); ++i)
    {
        x = v[node][i];
        cost = c[node][i];
        if(used[x] || x == dad) continue;

        dfs2(x, node, up + cost, level+1, now);
    }
}

int solve(int node)
{
    dfs(node);
    All = w[node];
    node = centroid(node).second;

    vector< pair<int,int> > now;
    vector<int> pos;
    int i, son, cost, ans = inf;

    best[0] = 0;

    for(i=0; i<v[node].size(); ++i)
    {
        son = v[node][i];
        cost = c[node][i];
        if(used[son]) continue;

        now.clear();
        dfs2(son, node, cost, 1, now);

        for(auto it : now)
            ans = min(ans, it.second + best[K - it.first]);

        for(auto it : now)
        {
            best[it.first] = min(best[it.first], it.second);
            pos.push_back(it.first);
        }
    }

    for(auto it : pos) best[it] = inf;

    used[node] = 1;

    for(auto it : v[node])
        if(!used[it])
            ans = min(ans, solve(it));
    return ans;
}

int best_path(int N, int _K, int H[][2], int L[])
{
    K = _K;
    int i;
    for(i=0; i<N-1; ++i)
    {
        int x, y, z;
        x = H[i][0];
        y = H[i][1];
        z = L[i];

        v[x].push_back(y);
        v[y].push_back(x);
        c[x].push_back(z);
        c[y].push_back(z);
    }

    for(i=0; i<=K; ++i) best[i] = inf;

    int X = solve(0);
    if(X == inf) return -1;
    return X;
}

Compilation message

race.cpp: In function 'void dfs2(int, int, int, int, std::vector<std::pair<int, int> >&)':
race.cpp:47:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<v[node].size(); ++i)
              ~^~~~~~~~~~~~~~~
race.cpp: In function 'int solve(int)':
race.cpp:69:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<v[node].size(); ++i)
              ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 10 ms 9724 KB Output is correct
3 Correct 9 ms 9720 KB Output is correct
4 Correct 10 ms 9720 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 10 ms 9720 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9720 KB Output is correct
10 Correct 10 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 10 ms 9720 KB Output is correct
13 Correct 10 ms 9720 KB Output is correct
14 Correct 9 ms 9720 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 10 ms 9772 KB Output is correct
17 Correct 10 ms 9848 KB Output is correct
18 Correct 10 ms 9720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 10 ms 9724 KB Output is correct
3 Correct 9 ms 9720 KB Output is correct
4 Correct 10 ms 9720 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 10 ms 9720 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9720 KB Output is correct
10 Correct 10 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 10 ms 9720 KB Output is correct
13 Correct 10 ms 9720 KB Output is correct
14 Correct 9 ms 9720 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 10 ms 9772 KB Output is correct
17 Correct 10 ms 9848 KB Output is correct
18 Correct 10 ms 9720 KB Output is correct
19 Correct 10 ms 9692 KB Output is correct
20 Correct 8 ms 9720 KB Output is correct
21 Correct 9 ms 9848 KB Output is correct
22 Correct 12 ms 13432 KB Output is correct
23 Correct 12 ms 12792 KB Output is correct
24 Correct 12 ms 13176 KB Output is correct
25 Correct 13 ms 13048 KB Output is correct
26 Correct 12 ms 11128 KB Output is correct
27 Correct 13 ms 12920 KB Output is correct
28 Correct 11 ms 10616 KB Output is correct
29 Correct 12 ms 11132 KB Output is correct
30 Correct 11 ms 11256 KB Output is correct
31 Correct 14 ms 12408 KB Output is correct
32 Correct 13 ms 12664 KB Output is correct
33 Correct 13 ms 12924 KB Output is correct
34 Correct 13 ms 12152 KB Output is correct
35 Correct 13 ms 13048 KB Output is correct
36 Correct 15 ms 13436 KB Output is correct
37 Correct 14 ms 12996 KB Output is correct
38 Correct 12 ms 11896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 10 ms 9724 KB Output is correct
3 Correct 9 ms 9720 KB Output is correct
4 Correct 10 ms 9720 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 10 ms 9720 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9720 KB Output is correct
10 Correct 10 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 10 ms 9720 KB Output is correct
13 Correct 10 ms 9720 KB Output is correct
14 Correct 9 ms 9720 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 10 ms 9772 KB Output is correct
17 Correct 10 ms 9848 KB Output is correct
18 Correct 10 ms 9720 KB Output is correct
19 Correct 309 ms 19472 KB Output is correct
20 Correct 313 ms 19672 KB Output is correct
21 Correct 289 ms 20028 KB Output is correct
22 Correct 246 ms 20592 KB Output is correct
23 Correct 256 ms 19192 KB Output is correct
24 Correct 141 ms 19576 KB Output is correct
25 Correct 318 ms 25000 KB Output is correct
26 Correct 164 ms 31744 KB Output is correct
27 Correct 462 ms 29836 KB Output is correct
28 Correct 834 ms 50552 KB Output is correct
29 Correct 786 ms 49016 KB Output is correct
30 Correct 421 ms 29816 KB Output is correct
31 Correct 441 ms 29816 KB Output is correct
32 Correct 541 ms 29876 KB Output is correct
33 Correct 893 ms 28792 KB Output is correct
34 Correct 862 ms 29688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 10 ms 9724 KB Output is correct
3 Correct 9 ms 9720 KB Output is correct
4 Correct 10 ms 9720 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 10 ms 9720 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9720 KB Output is correct
10 Correct 10 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 10 ms 9720 KB Output is correct
13 Correct 10 ms 9720 KB Output is correct
14 Correct 9 ms 9720 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 10 ms 9772 KB Output is correct
17 Correct 10 ms 9848 KB Output is correct
18 Correct 10 ms 9720 KB Output is correct
19 Correct 10 ms 9692 KB Output is correct
20 Correct 8 ms 9720 KB Output is correct
21 Correct 9 ms 9848 KB Output is correct
22 Correct 12 ms 13432 KB Output is correct
23 Correct 12 ms 12792 KB Output is correct
24 Correct 12 ms 13176 KB Output is correct
25 Correct 13 ms 13048 KB Output is correct
26 Correct 12 ms 11128 KB Output is correct
27 Correct 13 ms 12920 KB Output is correct
28 Correct 11 ms 10616 KB Output is correct
29 Correct 12 ms 11132 KB Output is correct
30 Correct 11 ms 11256 KB Output is correct
31 Correct 14 ms 12408 KB Output is correct
32 Correct 13 ms 12664 KB Output is correct
33 Correct 13 ms 12924 KB Output is correct
34 Correct 13 ms 12152 KB Output is correct
35 Correct 13 ms 13048 KB Output is correct
36 Correct 15 ms 13436 KB Output is correct
37 Correct 14 ms 12996 KB Output is correct
38 Correct 12 ms 11896 KB Output is correct
39 Correct 309 ms 19472 KB Output is correct
40 Correct 313 ms 19672 KB Output is correct
41 Correct 289 ms 20028 KB Output is correct
42 Correct 246 ms 20592 KB Output is correct
43 Correct 256 ms 19192 KB Output is correct
44 Correct 141 ms 19576 KB Output is correct
45 Correct 318 ms 25000 KB Output is correct
46 Correct 164 ms 31744 KB Output is correct
47 Correct 462 ms 29836 KB Output is correct
48 Correct 834 ms 50552 KB Output is correct
49 Correct 786 ms 49016 KB Output is correct
50 Correct 421 ms 29816 KB Output is correct
51 Correct 441 ms 29816 KB Output is correct
52 Correct 541 ms 29876 KB Output is correct
53 Correct 893 ms 28792 KB Output is correct
54 Correct 862 ms 29688 KB Output is correct
55 Correct 23 ms 10872 KB Output is correct
56 Correct 23 ms 10744 KB Output is correct
57 Correct 140 ms 20236 KB Output is correct
58 Correct 55 ms 20184 KB Output is correct
59 Correct 172 ms 32468 KB Output is correct
60 Correct 1081 ms 56740 KB Output is correct
61 Correct 400 ms 30300 KB Output is correct
62 Correct 395 ms 34812 KB Output is correct
63 Correct 483 ms 34812 KB Output is correct
64 Correct 791 ms 34472 KB Output is correct
65 Correct 705 ms 29980 KB Output is correct
66 Correct 1202 ms 49808 KB Output is correct
67 Correct 298 ms 35672 KB Output is correct
68 Correct 598 ms 35596 KB Output is correct
69 Correct 602 ms 35852 KB Output is correct
70 Correct 502 ms 34784 KB Output is correct