답안 #255133

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255133 2020-07-31T10:11:08 Z SamAnd 경주 (Race) (IOI11_race) C++17
100 / 100
1330 ms 33724 KB
#include "race.h"
//#define "grader.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef long long ll;
const int N = 200005, K = 1000006;

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

int ans;

bool c[N];

int q[N];
void dfs0(int x, int p)
{
    q[x] = 1;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i].fi;
        if (h == p || c[h])
            continue;
        dfs0(h, x);
        q[x] += q[h];
    }
}

int dfsc(int x, int p, int xx)
{
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i].fi;
        if (h == p || c[h])
            continue;
        if (q[h] > q[xx] / 2)
            return dfsc(h, x, xx);
    }
    return x;
}

int minu[K];

void dfs1(int x, int p, int d, int q)
{
    if (d > k)
        return;
    minu[d] = min(minu[d], q);
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i].fi;
        int hd = g[x][i].se;
        if (h == p || c[h])
            continue;
        dfs1(h, x, d + hd, q + 1);
    }
}

void dfs2(int x, int p, int d, int q)
{
    if (d > k)
        return;
    minu[d] = N;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i].fi;
        int hd = g[x][i].se;
        if (h == p || c[h])
            continue;
        dfs2(h, x, d + hd, q + 1);
    }
}

void dfs3(int x, int p, int d, int q)
{
    if (d > k)
        return;
    ans = min(ans, q + minu[k - d]);
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i].fi;
        int hd = g[x][i].se;
        if (h == p || c[h])
            continue;
        dfs3(h, x, d + hd, q + 1);
    }
}

void solvv(int r)
{
    minu[0] = 0;
    for (int i = 0; i < g[r].size(); ++i)
    {
        int x = g[r][i].fi;
        int d = g[r][i].se;
        if (c[x])
            continue;
        dfs3(x, r, d, 1);
        dfs1(x, r, d, 1);
    }
    dfs2(r, r, 0, 0);
    reverse(all(g[r]));
    minu[0] = 0;
    for (int i = 0; i < g[r].size(); ++i)
    {
        int x = g[r][i].fi;
        int d = g[r][i].se;
        if (c[x])
            continue;
        dfs3(x, r, d, 1);
        dfs1(x, r, d, 1);
    }
    dfs2(r, r, 0, 0);
}

void solv()
{
    ans = N;

    for (int i = 0; i <= k; ++i)
        minu[i] = N;

    queue<int> q;
    q.push(1);
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        dfs0(x, x);
        x = dfsc(x, x, x);
        solvv(x);
        c[x] = true;
        for (int i = 0; i < g[x].size(); ++i)
        {
            int h = g[x][i].fi;
            if (!c[h])
                q.push(h);
        }
    }

    if (ans == N)
        ans = -1;
}

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 x = H[i][0];
        int y = H[i][1];
        int z = L[i];
        ++x;
        ++y;
        g[x].push_back(m_p(y, z));
        g[y].push_back(m_p(x, z));
    }
    solv();
    return ans;
}

Compilation message

race.cpp: In function 'void dfs0(int, int)':
race.cpp:24:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'int dfsc(int, int, int)':
race.cpp:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:69:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs3(int, int, int, int)':
race.cpp:84:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void solvv(int)':
race.cpp:97:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[r].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp:109:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[r].size(); ++i)
                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void solv()':
race.cpp:138:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < g[x].size(); ++i)
                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 5 ms 4992 KB Output is correct
17 Correct 5 ms 4992 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 5 ms 4992 KB Output is correct
17 Correct 5 ms 4992 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 5 ms 4992 KB Output is correct
20 Correct 3 ms 4992 KB Output is correct
21 Correct 6 ms 5120 KB Output is correct
22 Correct 6 ms 8704 KB Output is correct
23 Correct 8 ms 8064 KB Output is correct
24 Correct 6 ms 8448 KB Output is correct
25 Correct 6 ms 8448 KB Output is correct
26 Correct 5 ms 6400 KB Output is correct
27 Correct 6 ms 8192 KB Output is correct
28 Correct 7 ms 5888 KB Output is correct
29 Correct 5 ms 6400 KB Output is correct
30 Correct 5 ms 6528 KB Output is correct
31 Correct 6 ms 7680 KB Output is correct
32 Correct 10 ms 7936 KB Output is correct
33 Correct 9 ms 8192 KB Output is correct
34 Correct 8 ms 7424 KB Output is correct
35 Correct 6 ms 8320 KB Output is correct
36 Correct 6 ms 8704 KB Output is correct
37 Correct 9 ms 8192 KB Output is correct
38 Correct 5 ms 7040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 5 ms 4992 KB Output is correct
17 Correct 5 ms 4992 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 274 ms 11768 KB Output is correct
20 Correct 303 ms 11808 KB Output is correct
21 Correct 404 ms 11768 KB Output is correct
22 Correct 258 ms 11896 KB Output is correct
23 Correct 110 ms 12152 KB Output is correct
24 Correct 88 ms 12408 KB Output is correct
25 Correct 207 ms 14656 KB Output is correct
26 Correct 164 ms 17656 KB Output is correct
27 Correct 369 ms 19064 KB Output is correct
28 Correct 505 ms 30456 KB Output is correct
29 Correct 378 ms 29560 KB Output is correct
30 Correct 342 ms 19196 KB Output is correct
31 Correct 276 ms 19192 KB Output is correct
32 Correct 492 ms 19320 KB Output is correct
33 Correct 482 ms 18028 KB Output is correct
34 Correct 367 ms 18936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 5 ms 4992 KB Output is correct
17 Correct 5 ms 4992 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 5 ms 4992 KB Output is correct
20 Correct 3 ms 4992 KB Output is correct
21 Correct 6 ms 5120 KB Output is correct
22 Correct 6 ms 8704 KB Output is correct
23 Correct 8 ms 8064 KB Output is correct
24 Correct 6 ms 8448 KB Output is correct
25 Correct 6 ms 8448 KB Output is correct
26 Correct 5 ms 6400 KB Output is correct
27 Correct 6 ms 8192 KB Output is correct
28 Correct 7 ms 5888 KB Output is correct
29 Correct 5 ms 6400 KB Output is correct
30 Correct 5 ms 6528 KB Output is correct
31 Correct 6 ms 7680 KB Output is correct
32 Correct 10 ms 7936 KB Output is correct
33 Correct 9 ms 8192 KB Output is correct
34 Correct 8 ms 7424 KB Output is correct
35 Correct 6 ms 8320 KB Output is correct
36 Correct 6 ms 8704 KB Output is correct
37 Correct 9 ms 8192 KB Output is correct
38 Correct 5 ms 7040 KB Output is correct
39 Correct 274 ms 11768 KB Output is correct
40 Correct 303 ms 11808 KB Output is correct
41 Correct 404 ms 11768 KB Output is correct
42 Correct 258 ms 11896 KB Output is correct
43 Correct 110 ms 12152 KB Output is correct
44 Correct 88 ms 12408 KB Output is correct
45 Correct 207 ms 14656 KB Output is correct
46 Correct 164 ms 17656 KB Output is correct
47 Correct 369 ms 19064 KB Output is correct
48 Correct 505 ms 30456 KB Output is correct
49 Correct 378 ms 29560 KB Output is correct
50 Correct 342 ms 19196 KB Output is correct
51 Correct 276 ms 19192 KB Output is correct
52 Correct 492 ms 19320 KB Output is correct
53 Correct 482 ms 18028 KB Output is correct
54 Correct 367 ms 18936 KB Output is correct
55 Correct 18 ms 5760 KB Output is correct
56 Correct 20 ms 5760 KB Output is correct
57 Correct 152 ms 12152 KB Output is correct
58 Correct 45 ms 12140 KB Output is correct
59 Correct 169 ms 18396 KB Output is correct
60 Correct 1068 ms 33724 KB Output is correct
61 Correct 365 ms 19192 KB Output is correct
62 Correct 463 ms 23032 KB Output is correct
63 Correct 575 ms 23032 KB Output is correct
64 Correct 1330 ms 21500 KB Output is correct
65 Correct 385 ms 19832 KB Output is correct
66 Correct 665 ms 31624 KB Output is correct
67 Correct 143 ms 24292 KB Output is correct
68 Correct 535 ms 23840 KB Output is correct
69 Correct 530 ms 24184 KB Output is correct
70 Correct 479 ms 23288 KB Output is correct