답안 #500235

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
500235 2021-12-30T13:36:59 Z danikoynov 경주 (Race) (IOI11_race) C++14
100 / 100
2506 ms 80636 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 10, maxk = 1e6 + 10, inf = 1e9;

vector < pair < ll, ll > > g[maxn];
ll lvl[maxn], depth[maxn], sub[maxn];
unordered_map < ll, ll > mp, vis;
void pre_dfs(ll v, ll par)
{
    sub[v] = 1;
    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == par)
            continue;
        lvl[u] = lvl[v] + 1;
        depth[u] = depth[v] + g[v][i].second;
        pre_dfs(u, v);
        sub[v] += sub[u];
    }
}

ll cnt[maxk], ans, k;
void check_dfs(ll v, ll p, ll root)
{
    ll len = (k + depth[root]) - depth[v] + depth[root];
    ///cout << depth[v] << " " << depth[root] << " " << len << endl;
    if (vis[len])
    {
        ans = min(ans, lvl[v] - lvl[root] + mp[len] - lvl[root]);
        ///cout << lvl[v] << " - " << lvl[root] << " - " << mp[len] << endl;
    }
    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == p)
            continue;
        check_dfs(u, v, root);
    }
}

vector < ll > path;
void add_dfs(ll v, ll p, ll root)
{
    ll len = depth[v];
    if (vis[len])
        mp[len] = min(mp[len], lvl[v]);
    else
    {
        vis[len] = 1;
        mp[len] = lvl[v];
    }
    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == p)
            continue;
        add_dfs(u, v, root);
    }
}
void dfs(ll v, ll p, bool big, ll last_depth)
{
    ll mx = -1, new_depth = 0;
    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == p)
            continue;
        if (mx == -1 || sub[u] > sub[mx])
            mx = u, new_depth = depth[u] - depth[v];
    }

    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == p || u == mx)
            continue;
        dfs(u, v, false, 0);
    }

    if (mx != -1)
    {
        dfs(mx, v, true, new_depth);
    }

    vis[depth[v]] = 1;
    mp[depth[v]] = lvl[v];
    if (vis[depth[v] + k])
        ans = min(ans, mp[depth[v] + k] - lvl[v]);

    for (ll i = 0; i < g[v].size(); i ++)
    {
        ll u = g[v][i].first;
        if (u == p || u == mx)
            continue;
        check_dfs(u, v, v);
        add_dfs(u, v, v);
    }

    /**cout << v << endl;
    for (ll i = 0; i <= k; i ++)
        cout << cnt[i] << " ";
    cout << endl;*/

    if (!big)
    {
        vis.clear();
        mp.clear();
    }



}
int best_path(int N, int K, int H[][2], int L[])
{
    k = K;
    for (ll i = 0; i < N - 1; i ++)
    {
        g[H[i][0]].push_back({H[i][1], L[i]});
        g[H[i][1]].push_back({H[i][0], L[i]});
    }

    pre_dfs(0, -1);
    for (ll i = 0; i <= K; i ++)
        cnt[i] = inf;
    ans = inf;
    dfs(0, -1, 1, 0);
    if (ans == inf)
        return -1;
    return ans;
}

Compilation message

race.cpp: In function 'void pre_dfs(ll, ll)':
race.cpp:13:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
race.cpp: In function 'void check_dfs(ll, ll, ll)':
race.cpp:35:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
race.cpp: In function 'void add_dfs(ll, ll, ll)':
race.cpp:55:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(ll, ll, bool, ll)':
race.cpp:66:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
race.cpp:75:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
race.cpp:93:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |     for (ll i = 0; i < g[v].size(); i ++)
      |                    ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 5068 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 5012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 5068 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 5012 KB Output is correct
19 Correct 3 ms 4984 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 8 ms 12364 KB Output is correct
23 Correct 6 ms 11084 KB Output is correct
24 Correct 7 ms 11888 KB Output is correct
25 Correct 7 ms 11800 KB Output is correct
26 Correct 5 ms 7884 KB Output is correct
27 Correct 6 ms 11340 KB Output is correct
28 Correct 4 ms 6680 KB Output is correct
29 Correct 5 ms 7756 KB Output is correct
30 Correct 5 ms 8012 KB Output is correct
31 Correct 6 ms 10316 KB Output is correct
32 Correct 6 ms 10700 KB Output is correct
33 Correct 6 ms 11340 KB Output is correct
34 Correct 5 ms 9804 KB Output is correct
35 Correct 6 ms 11592 KB Output is correct
36 Correct 9 ms 12492 KB Output is correct
37 Correct 6 ms 11468 KB Output is correct
38 Correct 5 ms 9164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 5068 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 5012 KB Output is correct
19 Correct 150 ms 13608 KB Output is correct
20 Correct 136 ms 13656 KB Output is correct
21 Correct 149 ms 13800 KB Output is correct
22 Correct 167 ms 13680 KB Output is correct
23 Correct 204 ms 14112 KB Output is correct
24 Correct 124 ms 13272 KB Output is correct
25 Correct 66 ms 19780 KB Output is correct
26 Correct 50 ms 25576 KB Output is correct
27 Correct 283 ms 25880 KB Output is correct
28 Correct 369 ms 73832 KB Output is correct
29 Correct 378 ms 72256 KB Output is correct
30 Correct 323 ms 25888 KB Output is correct
31 Correct 233 ms 25816 KB Output is correct
32 Correct 349 ms 25988 KB Output is correct
33 Correct 207 ms 24800 KB Output is correct
34 Correct 659 ms 54180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 5068 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 5012 KB Output is correct
19 Correct 3 ms 4984 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 8 ms 12364 KB Output is correct
23 Correct 6 ms 11084 KB Output is correct
24 Correct 7 ms 11888 KB Output is correct
25 Correct 7 ms 11800 KB Output is correct
26 Correct 5 ms 7884 KB Output is correct
27 Correct 6 ms 11340 KB Output is correct
28 Correct 4 ms 6680 KB Output is correct
29 Correct 5 ms 7756 KB Output is correct
30 Correct 5 ms 8012 KB Output is correct
31 Correct 6 ms 10316 KB Output is correct
32 Correct 6 ms 10700 KB Output is correct
33 Correct 6 ms 11340 KB Output is correct
34 Correct 5 ms 9804 KB Output is correct
35 Correct 6 ms 11592 KB Output is correct
36 Correct 9 ms 12492 KB Output is correct
37 Correct 6 ms 11468 KB Output is correct
38 Correct 5 ms 9164 KB Output is correct
39 Correct 150 ms 13608 KB Output is correct
40 Correct 136 ms 13656 KB Output is correct
41 Correct 149 ms 13800 KB Output is correct
42 Correct 167 ms 13680 KB Output is correct
43 Correct 204 ms 14112 KB Output is correct
44 Correct 124 ms 13272 KB Output is correct
45 Correct 66 ms 19780 KB Output is correct
46 Correct 50 ms 25576 KB Output is correct
47 Correct 283 ms 25880 KB Output is correct
48 Correct 369 ms 73832 KB Output is correct
49 Correct 378 ms 72256 KB Output is correct
50 Correct 323 ms 25888 KB Output is correct
51 Correct 233 ms 25816 KB Output is correct
52 Correct 349 ms 25988 KB Output is correct
53 Correct 207 ms 24800 KB Output is correct
54 Correct 659 ms 54180 KB Output is correct
55 Correct 21 ms 6676 KB Output is correct
56 Correct 16 ms 5916 KB Output is correct
57 Correct 103 ms 15268 KB Output is correct
58 Correct 44 ms 14224 KB Output is correct
59 Correct 95 ms 40544 KB Output is correct
60 Correct 348 ms 80636 KB Output is correct
61 Correct 298 ms 29200 KB Output is correct
62 Correct 258 ms 33620 KB Output is correct
63 Correct 344 ms 33908 KB Output is correct
64 Correct 2359 ms 43712 KB Output is correct
65 Correct 2506 ms 53048 KB Output is correct
66 Correct 456 ms 76124 KB Output is correct
67 Correct 198 ms 32740 KB Output is correct
68 Correct 715 ms 53340 KB Output is correct
69 Correct 632 ms 53416 KB Output is correct
70 Correct 586 ms 51700 KB Output is correct