답안 #426075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
426075 2021-06-13T13:40:56 Z JeanBombeur 경주 (Race) (IOI11_race) C++17
100 / 100
583 ms 46772 KB
#include <cstdio>
#include <iostream>
#include <vector>
#include "race.h"
using namespace std;

const int INFINI = (1000 * 1000 * 1000);
const int MAX_NOEUDS = (200 * 1000);
const int DIST_MAX = (1000 * 1000);

vector <pair <int, int>> Voisins[MAX_NOEUDS];

pair <pair <int, int>, pair <int, int>> DistancesMin[DIST_MAX];

vector <int> Update;

bool Traite[MAX_NOEUDS];

int NbFils[MAX_NOEUDS];

int cible;

void Init() {
    for (int i = 0; i < DIST_MAX; i ++)
    {
        DistancesMin[i].first = DistancesMin[i].second = {INFINI, -1};
    }
    return;
}

int Count(int noeud, int pere) {
    int sum = 1;
    for (pair <int, int> dest : Voisins[noeud])
    {
        if (!Traite[dest.first] && dest.first != pere)
            sum += Count(dest.first, noeud);
    }
    return NbFils[noeud] = sum;
}

int FindCentroide(int noeud, int pere, int borne) {
    for (pair <int, int> dest : Voisins[noeud])
    {
        if (!Traite[dest.first] && dest.first != pere && NbFils[dest.first] > borne)
            return FindCentroide(dest.first, noeud, borne);
    }
    return noeud;
}

void Explore(int noeud, int pere, int dist, int prof, int compo) {
    if (dist > cible)
        return;
    Update.push_back(dist);
    if (prof < DistancesMin[dist].first.first)
    {
        if (compo != DistancesMin[dist].first.second)
            DistancesMin[dist].second = DistancesMin[dist].first;
        DistancesMin[dist].first = {prof, compo};
    }
    else if (prof < DistancesMin[dist].second.first && compo != DistancesMin[dist].first.second)
        DistancesMin[dist].second = {prof, compo};
    for (pair <int, int> dest : Voisins[noeud])
    {
        if (!Traite[dest.first] && dest.first != pere)
            Explore(dest.first, noeud, dist + dest.second, prof + 1, compo);
    }
    return;
}

int TraiteCentroide(int noeud) {
    int nb = 0;
    DistancesMin[0].first = {0, -1};
    for (pair <int, int> dest : Voisins[noeud])
    {
        if (!Traite[dest.first])
        {
            Explore(dest.first, noeud, dest.second, 1, nb ++);
        }
    }
    int ans = INFINI;
    for (int a : Update)
    {
        if (DistancesMin[a].first.second != DistancesMin[cible - a].first.second)
            ans = min(ans, DistancesMin[a].first.first + DistancesMin[cible - a].first.first);
        else
        {
            ans = min(ans, DistancesMin[a].first.first + DistancesMin[cible - a].second.first);
            ans = min(ans, DistancesMin[a].second.first + DistancesMin[cible - a].first.first);
        }
    }
    for (int a : Update)
    {
        DistancesMin[a].first = DistancesMin[a].second = {INFINI, -1};
    }
    Update.clear();
    Traite[noeud] = true;
    for (pair <int, int> dest : Voisins[noeud])
    {
        if (!Traite[dest.first])
        {
            Count(dest.first, noeud);
            ans = min(ans, TraiteCentroide(FindCentroide(dest.first, noeud, NbFils[dest.first] / 2)));
        }
    }
    return ans;
}

int best_path(int nbSommets, int target, int Aretes[][2], int Poids[]) {
    for (int i = 0; i < nbSommets - 1; i ++)
    {
        Voisins[Aretes[i][0]].push_back({Aretes[i][1], Poids[i]});
        Voisins[Aretes[i][1]].push_back({Aretes[i][0], Poids[i]});
    }
    cible = target;
    Init();
    Count(0, -1);
    int r = TraiteCentroide(FindCentroide(0, -1, NbFils[0] / 2));
    if (r == INFINI)
        r = -1;
    return r;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 20548 KB Output is correct
2 Correct 13 ms 20600 KB Output is correct
3 Correct 12 ms 20584 KB Output is correct
4 Correct 13 ms 20684 KB Output is correct
5 Correct 13 ms 20564 KB Output is correct
6 Correct 13 ms 20684 KB Output is correct
7 Correct 13 ms 20596 KB Output is correct
8 Correct 12 ms 20556 KB Output is correct
9 Correct 13 ms 20684 KB Output is correct
10 Correct 13 ms 20628 KB Output is correct
11 Correct 14 ms 20592 KB Output is correct
12 Correct 13 ms 20556 KB Output is correct
13 Correct 12 ms 20624 KB Output is correct
14 Correct 13 ms 20684 KB Output is correct
15 Correct 13 ms 20556 KB Output is correct
16 Correct 13 ms 20556 KB Output is correct
17 Correct 12 ms 20660 KB Output is correct
18 Correct 13 ms 20684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 20548 KB Output is correct
2 Correct 13 ms 20600 KB Output is correct
3 Correct 12 ms 20584 KB Output is correct
4 Correct 13 ms 20684 KB Output is correct
5 Correct 13 ms 20564 KB Output is correct
6 Correct 13 ms 20684 KB Output is correct
7 Correct 13 ms 20596 KB Output is correct
8 Correct 12 ms 20556 KB Output is correct
9 Correct 13 ms 20684 KB Output is correct
10 Correct 13 ms 20628 KB Output is correct
11 Correct 14 ms 20592 KB Output is correct
12 Correct 13 ms 20556 KB Output is correct
13 Correct 12 ms 20624 KB Output is correct
14 Correct 13 ms 20684 KB Output is correct
15 Correct 13 ms 20556 KB Output is correct
16 Correct 13 ms 20556 KB Output is correct
17 Correct 12 ms 20660 KB Output is correct
18 Correct 13 ms 20684 KB Output is correct
19 Correct 12 ms 20624 KB Output is correct
20 Correct 13 ms 20556 KB Output is correct
21 Correct 12 ms 20692 KB Output is correct
22 Correct 13 ms 20684 KB Output is correct
23 Correct 13 ms 20628 KB Output is correct
24 Correct 14 ms 20684 KB Output is correct
25 Correct 17 ms 20640 KB Output is correct
26 Correct 13 ms 20684 KB Output is correct
27 Correct 12 ms 20636 KB Output is correct
28 Correct 13 ms 20696 KB Output is correct
29 Correct 13 ms 20684 KB Output is correct
30 Correct 13 ms 20684 KB Output is correct
31 Correct 14 ms 20684 KB Output is correct
32 Correct 14 ms 20636 KB Output is correct
33 Correct 13 ms 20808 KB Output is correct
34 Correct 14 ms 20684 KB Output is correct
35 Correct 14 ms 20628 KB Output is correct
36 Correct 14 ms 20684 KB Output is correct
37 Correct 17 ms 20692 KB Output is correct
38 Correct 15 ms 20740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 20548 KB Output is correct
2 Correct 13 ms 20600 KB Output is correct
3 Correct 12 ms 20584 KB Output is correct
4 Correct 13 ms 20684 KB Output is correct
5 Correct 13 ms 20564 KB Output is correct
6 Correct 13 ms 20684 KB Output is correct
7 Correct 13 ms 20596 KB Output is correct
8 Correct 12 ms 20556 KB Output is correct
9 Correct 13 ms 20684 KB Output is correct
10 Correct 13 ms 20628 KB Output is correct
11 Correct 14 ms 20592 KB Output is correct
12 Correct 13 ms 20556 KB Output is correct
13 Correct 12 ms 20624 KB Output is correct
14 Correct 13 ms 20684 KB Output is correct
15 Correct 13 ms 20556 KB Output is correct
16 Correct 13 ms 20556 KB Output is correct
17 Correct 12 ms 20660 KB Output is correct
18 Correct 13 ms 20684 KB Output is correct
19 Correct 154 ms 26844 KB Output is correct
20 Correct 152 ms 26476 KB Output is correct
21 Correct 168 ms 26652 KB Output is correct
22 Correct 158 ms 26956 KB Output is correct
23 Correct 123 ms 26564 KB Output is correct
24 Correct 79 ms 26488 KB Output is correct
25 Correct 133 ms 29976 KB Output is correct
26 Correct 105 ms 34132 KB Output is correct
27 Correct 241 ms 32296 KB Output is correct
28 Correct 340 ms 46532 KB Output is correct
29 Correct 360 ms 45372 KB Output is correct
30 Correct 213 ms 32324 KB Output is correct
31 Correct 192 ms 32272 KB Output is correct
32 Correct 245 ms 32344 KB Output is correct
33 Correct 265 ms 31196 KB Output is correct
34 Correct 221 ms 31148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 20548 KB Output is correct
2 Correct 13 ms 20600 KB Output is correct
3 Correct 12 ms 20584 KB Output is correct
4 Correct 13 ms 20684 KB Output is correct
5 Correct 13 ms 20564 KB Output is correct
6 Correct 13 ms 20684 KB Output is correct
7 Correct 13 ms 20596 KB Output is correct
8 Correct 12 ms 20556 KB Output is correct
9 Correct 13 ms 20684 KB Output is correct
10 Correct 13 ms 20628 KB Output is correct
11 Correct 14 ms 20592 KB Output is correct
12 Correct 13 ms 20556 KB Output is correct
13 Correct 12 ms 20624 KB Output is correct
14 Correct 13 ms 20684 KB Output is correct
15 Correct 13 ms 20556 KB Output is correct
16 Correct 13 ms 20556 KB Output is correct
17 Correct 12 ms 20660 KB Output is correct
18 Correct 13 ms 20684 KB Output is correct
19 Correct 12 ms 20624 KB Output is correct
20 Correct 13 ms 20556 KB Output is correct
21 Correct 12 ms 20692 KB Output is correct
22 Correct 13 ms 20684 KB Output is correct
23 Correct 13 ms 20628 KB Output is correct
24 Correct 14 ms 20684 KB Output is correct
25 Correct 17 ms 20640 KB Output is correct
26 Correct 13 ms 20684 KB Output is correct
27 Correct 12 ms 20636 KB Output is correct
28 Correct 13 ms 20696 KB Output is correct
29 Correct 13 ms 20684 KB Output is correct
30 Correct 13 ms 20684 KB Output is correct
31 Correct 14 ms 20684 KB Output is correct
32 Correct 14 ms 20636 KB Output is correct
33 Correct 13 ms 20808 KB Output is correct
34 Correct 14 ms 20684 KB Output is correct
35 Correct 14 ms 20628 KB Output is correct
36 Correct 14 ms 20684 KB Output is correct
37 Correct 17 ms 20692 KB Output is correct
38 Correct 15 ms 20740 KB Output is correct
39 Correct 154 ms 26844 KB Output is correct
40 Correct 152 ms 26476 KB Output is correct
41 Correct 168 ms 26652 KB Output is correct
42 Correct 158 ms 26956 KB Output is correct
43 Correct 123 ms 26564 KB Output is correct
44 Correct 79 ms 26488 KB Output is correct
45 Correct 133 ms 29976 KB Output is correct
46 Correct 105 ms 34132 KB Output is correct
47 Correct 241 ms 32296 KB Output is correct
48 Correct 340 ms 46532 KB Output is correct
49 Correct 360 ms 45372 KB Output is correct
50 Correct 213 ms 32324 KB Output is correct
51 Correct 192 ms 32272 KB Output is correct
52 Correct 245 ms 32344 KB Output is correct
53 Correct 265 ms 31196 KB Output is correct
54 Correct 221 ms 31148 KB Output is correct
55 Correct 21 ms 21324 KB Output is correct
56 Correct 23 ms 21324 KB Output is correct
57 Correct 94 ms 27076 KB Output is correct
58 Correct 50 ms 27492 KB Output is correct
59 Correct 110 ms 34084 KB Output is correct
60 Correct 484 ms 46772 KB Output is correct
61 Correct 210 ms 32568 KB Output is correct
62 Correct 231 ms 33184 KB Output is correct
63 Correct 288 ms 33392 KB Output is correct
64 Correct 526 ms 33008 KB Output is correct
65 Correct 310 ms 32224 KB Output is correct
66 Correct 583 ms 42904 KB Output is correct
67 Correct 172 ms 34352 KB Output is correct
68 Correct 305 ms 32852 KB Output is correct
69 Correct 358 ms 33092 KB Output is correct
70 Correct 286 ms 32480 KB Output is correct