답안 #349163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
349163 2021-01-16T20:47:10 Z idk321 경주 (Race) (IOI11_race) C++11
100 / 100
618 ms 41296 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 200001;
const int K = 1000001;

vector<array<int, 2>> adj[N];
//int par[N];
int n, k;
bool vis[N];
int dist[K];


int sizOf(int node, int parent)
{
    if (vis[node]) return 0;
    int res = 1;
    for (auto next : adj[node])
    {
        if (next[0] == parent) continue;
        res += sizOf(next[0], node);
    }

    return res;
}

array<int, 2> dfs2(int node, int par, int siz)
{
    if (vis[node]) return {0, -1};

    bool good = true;
    int down = 0;
    for (auto next : adj[node])
    {
        if (next[0] == par) continue;
        auto curr = dfs2(next[0], node, siz);
        if (curr[1] != -1) return {-1, curr[1]};
        if (curr[0] > siz / 2) good = false;
        down += curr[0];
    }

    good = good && ((siz - down - 1) <= siz / 2);
    array<int, 2> res = {down + 1, -1};
    if (good) res[1] = node;
    return res;
}



int dfs1(int node, int parent, int edges, int cdist, vector<array<int, 2>>& cvis)
{
    if (vis[node]) return N;
    if (cdist > k) return N;
    int res = N;
    //cout << node << " a " << cdist << " " << k <<endl;

    res = min(res, edges + dist[k - cdist]);
    cvis.push_back({cdist, edges});
    for (auto next : adj[node])
    {
        if (next[0] == parent) continue;
        res = min(res, dfs1(next[0], node, edges + 1, cdist + next[1], cvis));
    }

    return res;
}

int solveRoot(int node)
{
    if (vis[node]) return N;
    int res = N;
    vector<int> allVis;
    dist[0] = 0;
    for (auto next : adj[node])
    {

        vector<array<int, 2>> cvis;
        res = min(res, dfs1(next[0], node, 1, next[1], cvis));
        for (auto other : cvis)
        {
            allVis.push_back(other[0]);
            dist[other[0]] = min(dist[other[0]], other[1]);
        }
    }
    for (int other : allVis) dist[other] = N;

    //cout << node << " " << res << endl;
    //cout << node << " " << res << endl;
    return res;
}

int solve(int node)
{
    if (vis[node]) return N;
    int res = N;
    int root = dfs2(node, -1, sizOf(node, -1))[1];
    res = min(res, solveRoot(root));
    vis[root] = true;
    for (auto next : adj[root])
    {
        //cout << root << " " << next[0] << endl;
        res = min(res, solve(next[0]));
    }

    return res;
}





int best_path(int n1, int k1, int H[][2], int L[])
{
    n = n1;
    k = k1;

    for (int i = 0; i < n - 1; i++)
    {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }

    for (int i = 1; i < K; i++) dist[i] = N;
    dist[0] = 0;
    int res = solve(0);
    if (res == N) return -1;
    return res;
}

/*

6 200
0 1 25
1 2 35
2 3 40
3 4 50
4 5 50

*/

# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 6 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 7 ms 8960 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 6 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 6 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 7 ms 8960 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 6 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 6 ms 9196 KB Output is correct
20 Correct 6 ms 8940 KB Output is correct
21 Correct 7 ms 8940 KB Output is correct
22 Correct 7 ms 8940 KB Output is correct
23 Correct 7 ms 8940 KB Output is correct
24 Correct 7 ms 8940 KB Output is correct
25 Correct 8 ms 9068 KB Output is correct
26 Correct 7 ms 8940 KB Output is correct
27 Correct 6 ms 8940 KB Output is correct
28 Correct 7 ms 8940 KB Output is correct
29 Correct 7 ms 8940 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 7 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 8940 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 6 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 7 ms 9088 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 6 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 7 ms 8960 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 6 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 187 ms 13984 KB Output is correct
20 Correct 180 ms 15464 KB Output is correct
21 Correct 182 ms 15676 KB Output is correct
22 Correct 176 ms 16032 KB Output is correct
23 Correct 115 ms 15628 KB Output is correct
24 Correct 85 ms 15468 KB Output is correct
25 Correct 163 ms 19692 KB Output is correct
26 Correct 140 ms 25572 KB Output is correct
27 Correct 239 ms 21996 KB Output is correct
28 Correct 369 ms 39276 KB Output is correct
29 Correct 365 ms 37868 KB Output is correct
30 Correct 236 ms 21868 KB Output is correct
31 Correct 229 ms 21868 KB Output is correct
32 Correct 292 ms 21976 KB Output is correct
33 Correct 340 ms 20972 KB Output is correct
34 Correct 246 ms 20716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 6 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 7 ms 8960 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 6 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 6 ms 9196 KB Output is correct
20 Correct 6 ms 8940 KB Output is correct
21 Correct 7 ms 8940 KB Output is correct
22 Correct 7 ms 8940 KB Output is correct
23 Correct 7 ms 8940 KB Output is correct
24 Correct 7 ms 8940 KB Output is correct
25 Correct 8 ms 9068 KB Output is correct
26 Correct 7 ms 8940 KB Output is correct
27 Correct 6 ms 8940 KB Output is correct
28 Correct 7 ms 8940 KB Output is correct
29 Correct 7 ms 8940 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 7 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 8940 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 6 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 7 ms 9088 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
39 Correct 187 ms 13984 KB Output is correct
40 Correct 180 ms 15464 KB Output is correct
41 Correct 182 ms 15676 KB Output is correct
42 Correct 176 ms 16032 KB Output is correct
43 Correct 115 ms 15628 KB Output is correct
44 Correct 85 ms 15468 KB Output is correct
45 Correct 163 ms 19692 KB Output is correct
46 Correct 140 ms 25572 KB Output is correct
47 Correct 239 ms 21996 KB Output is correct
48 Correct 369 ms 39276 KB Output is correct
49 Correct 365 ms 37868 KB Output is correct
50 Correct 236 ms 21868 KB Output is correct
51 Correct 229 ms 21868 KB Output is correct
52 Correct 292 ms 21976 KB Output is correct
53 Correct 340 ms 20972 KB Output is correct
54 Correct 246 ms 20716 KB Output is correct
55 Correct 16 ms 9708 KB Output is correct
56 Correct 17 ms 9708 KB Output is correct
57 Correct 128 ms 16472 KB Output is correct
58 Correct 52 ms 16096 KB Output is correct
59 Correct 142 ms 25572 KB Output is correct
60 Correct 526 ms 41296 KB Output is correct
61 Correct 248 ms 22000 KB Output is correct
62 Correct 269 ms 22748 KB Output is correct
63 Correct 330 ms 22876 KB Output is correct
64 Correct 618 ms 23552 KB Output is correct
65 Correct 310 ms 21996 KB Output is correct
66 Correct 504 ms 34796 KB Output is correct
67 Correct 149 ms 23760 KB Output is correct
68 Correct 343 ms 22908 KB Output is correct
69 Correct 343 ms 23044 KB Output is correct
70 Correct 306 ms 22532 KB Output is correct