#include "race.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define chmin(x, v) x = min(x, v)
#define pb push_back
#define sz(x) (int)x.size()
using namespace std;
const int N_NOEUDS = 1e6;
struct SousArbre {
map<int, int> profMin;
};
vector<SousArbre> arbre;
bool vu[N_NOEUDS];
int prof[N_NOEUDS], distRacine[N_NOEUDS];
vector<pii> voisins[N_NOEUDS];
int ans = N_NOEUDS, k;
int fusion(int pere, int fils, int noeud){
if (sz(arbre[pere].profMin) < sz(arbre[fils].profMin))
return fusion(fils, pere, noeud);
for (pii couple : arbre[fils].profMin){
int cible = k + 2 * distRacine[noeud] - couple.first;
if (arbre[pere].profMin.count(cible) == 1)
chmin(ans, couple.second + arbre[pere].profMin[cible] - 2 * prof[noeud]);
}
for (pii couple : arbre[fils].profMin){
if (arbre[pere].profMin.count(couple.first) == 1)
chmin(arbre[pere].profMin[couple.first], couple.second);
else
arbre[pere].profMin[couple.first] = couple.second;
}
return pere;
}
int parcours(int noeud, int etage, int dRoot){
vu[noeud] = true;
prof[noeud] = etage;
distRacine[noeud] = dRoot;
int ind = sz(arbre);
map<int, int> p; p[distRacine[noeud]] = prof[noeud];
arbre.pb({p});
for (pii sortant : voisins[noeud]){
int enfant = sortant.first, distSup = sortant.second;
if (vu[enfant]) continue;
ind = fusion(ind, parcours(enfant, etage + 1, dRoot + distSup), noeud);
}
return ind;
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
for (int ind = 0; ind < N - 1; ++ind){
int poids = L[ind], noeud = H[ind][0], voisin = H[ind][1];
voisins[noeud].pb({voisin, poids});
voisins[voisin].pb({noeud, poids});
}
int racine = parcours(0, 0, 0);
/*for (pii couple : arbre[racine].profMin)
cout << "paire " << couple.first << " " << couple.second << endl;*/
if (ans == N_NOEUDS) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:68:6: warning: unused variable 'racine' [-Wunused-variable]
68 | int racine = parcours(0, 0, 0);
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23804 KB |
Output is correct |
2 |
Correct |
17 ms |
23804 KB |
Output is correct |
3 |
Correct |
14 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
18 ms |
23772 KB |
Output is correct |
6 |
Correct |
15 ms |
23768 KB |
Output is correct |
7 |
Correct |
14 ms |
23816 KB |
Output is correct |
8 |
Correct |
15 ms |
23812 KB |
Output is correct |
9 |
Correct |
15 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23796 KB |
Output is correct |
12 |
Correct |
14 ms |
23756 KB |
Output is correct |
13 |
Correct |
15 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23804 KB |
Output is correct |
15 |
Correct |
18 ms |
23788 KB |
Output is correct |
16 |
Correct |
17 ms |
23708 KB |
Output is correct |
17 |
Correct |
16 ms |
23836 KB |
Output is correct |
18 |
Correct |
15 ms |
23776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23804 KB |
Output is correct |
2 |
Correct |
17 ms |
23804 KB |
Output is correct |
3 |
Correct |
14 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
18 ms |
23772 KB |
Output is correct |
6 |
Correct |
15 ms |
23768 KB |
Output is correct |
7 |
Correct |
14 ms |
23816 KB |
Output is correct |
8 |
Correct |
15 ms |
23812 KB |
Output is correct |
9 |
Correct |
15 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23796 KB |
Output is correct |
12 |
Correct |
14 ms |
23756 KB |
Output is correct |
13 |
Correct |
15 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23804 KB |
Output is correct |
15 |
Correct |
18 ms |
23788 KB |
Output is correct |
16 |
Correct |
17 ms |
23708 KB |
Output is correct |
17 |
Correct |
16 ms |
23836 KB |
Output is correct |
18 |
Correct |
15 ms |
23776 KB |
Output is correct |
19 |
Correct |
14 ms |
23752 KB |
Output is correct |
20 |
Correct |
14 ms |
23824 KB |
Output is correct |
21 |
Correct |
16 ms |
24012 KB |
Output is correct |
22 |
Correct |
16 ms |
24004 KB |
Output is correct |
23 |
Correct |
19 ms |
24012 KB |
Output is correct |
24 |
Correct |
16 ms |
24004 KB |
Output is correct |
25 |
Correct |
20 ms |
24040 KB |
Output is correct |
26 |
Correct |
14 ms |
24012 KB |
Output is correct |
27 |
Correct |
14 ms |
24012 KB |
Output is correct |
28 |
Correct |
15 ms |
24012 KB |
Output is correct |
29 |
Correct |
16 ms |
24012 KB |
Output is correct |
30 |
Correct |
33 ms |
24060 KB |
Output is correct |
31 |
Correct |
17 ms |
24056 KB |
Output is correct |
32 |
Correct |
20 ms |
24068 KB |
Output is correct |
33 |
Correct |
16 ms |
24092 KB |
Output is correct |
34 |
Correct |
15 ms |
24012 KB |
Output is correct |
35 |
Correct |
17 ms |
24040 KB |
Output is correct |
36 |
Correct |
16 ms |
24096 KB |
Output is correct |
37 |
Correct |
16 ms |
24012 KB |
Output is correct |
38 |
Correct |
19 ms |
24084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23804 KB |
Output is correct |
2 |
Correct |
17 ms |
23804 KB |
Output is correct |
3 |
Correct |
14 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
18 ms |
23772 KB |
Output is correct |
6 |
Correct |
15 ms |
23768 KB |
Output is correct |
7 |
Correct |
14 ms |
23816 KB |
Output is correct |
8 |
Correct |
15 ms |
23812 KB |
Output is correct |
9 |
Correct |
15 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23796 KB |
Output is correct |
12 |
Correct |
14 ms |
23756 KB |
Output is correct |
13 |
Correct |
15 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23804 KB |
Output is correct |
15 |
Correct |
18 ms |
23788 KB |
Output is correct |
16 |
Correct |
17 ms |
23708 KB |
Output is correct |
17 |
Correct |
16 ms |
23836 KB |
Output is correct |
18 |
Correct |
15 ms |
23776 KB |
Output is correct |
19 |
Correct |
160 ms |
47204 KB |
Output is correct |
20 |
Correct |
177 ms |
47384 KB |
Output is correct |
21 |
Correct |
169 ms |
47124 KB |
Output is correct |
22 |
Correct |
190 ms |
46824 KB |
Output is correct |
23 |
Correct |
192 ms |
56388 KB |
Output is correct |
24 |
Correct |
178 ms |
49508 KB |
Output is correct |
25 |
Correct |
161 ms |
52256 KB |
Output is correct |
26 |
Correct |
103 ms |
63104 KB |
Output is correct |
27 |
Correct |
257 ms |
61968 KB |
Output is correct |
28 |
Correct |
384 ms |
102860 KB |
Output is correct |
29 |
Correct |
372 ms |
100808 KB |
Output is correct |
30 |
Correct |
321 ms |
61972 KB |
Output is correct |
31 |
Correct |
306 ms |
61984 KB |
Output is correct |
32 |
Correct |
362 ms |
62188 KB |
Output is correct |
33 |
Correct |
368 ms |
65564 KB |
Output is correct |
34 |
Correct |
483 ms |
90412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23804 KB |
Output is correct |
2 |
Correct |
17 ms |
23804 KB |
Output is correct |
3 |
Correct |
14 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
18 ms |
23772 KB |
Output is correct |
6 |
Correct |
15 ms |
23768 KB |
Output is correct |
7 |
Correct |
14 ms |
23816 KB |
Output is correct |
8 |
Correct |
15 ms |
23812 KB |
Output is correct |
9 |
Correct |
15 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23796 KB |
Output is correct |
12 |
Correct |
14 ms |
23756 KB |
Output is correct |
13 |
Correct |
15 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23804 KB |
Output is correct |
15 |
Correct |
18 ms |
23788 KB |
Output is correct |
16 |
Correct |
17 ms |
23708 KB |
Output is correct |
17 |
Correct |
16 ms |
23836 KB |
Output is correct |
18 |
Correct |
15 ms |
23776 KB |
Output is correct |
19 |
Correct |
14 ms |
23752 KB |
Output is correct |
20 |
Correct |
14 ms |
23824 KB |
Output is correct |
21 |
Correct |
16 ms |
24012 KB |
Output is correct |
22 |
Correct |
16 ms |
24004 KB |
Output is correct |
23 |
Correct |
19 ms |
24012 KB |
Output is correct |
24 |
Correct |
16 ms |
24004 KB |
Output is correct |
25 |
Correct |
20 ms |
24040 KB |
Output is correct |
26 |
Correct |
14 ms |
24012 KB |
Output is correct |
27 |
Correct |
14 ms |
24012 KB |
Output is correct |
28 |
Correct |
15 ms |
24012 KB |
Output is correct |
29 |
Correct |
16 ms |
24012 KB |
Output is correct |
30 |
Correct |
33 ms |
24060 KB |
Output is correct |
31 |
Correct |
17 ms |
24056 KB |
Output is correct |
32 |
Correct |
20 ms |
24068 KB |
Output is correct |
33 |
Correct |
16 ms |
24092 KB |
Output is correct |
34 |
Correct |
15 ms |
24012 KB |
Output is correct |
35 |
Correct |
17 ms |
24040 KB |
Output is correct |
36 |
Correct |
16 ms |
24096 KB |
Output is correct |
37 |
Correct |
16 ms |
24012 KB |
Output is correct |
38 |
Correct |
19 ms |
24084 KB |
Output is correct |
39 |
Correct |
160 ms |
47204 KB |
Output is correct |
40 |
Correct |
177 ms |
47384 KB |
Output is correct |
41 |
Correct |
169 ms |
47124 KB |
Output is correct |
42 |
Correct |
190 ms |
46824 KB |
Output is correct |
43 |
Correct |
192 ms |
56388 KB |
Output is correct |
44 |
Correct |
178 ms |
49508 KB |
Output is correct |
45 |
Correct |
161 ms |
52256 KB |
Output is correct |
46 |
Correct |
103 ms |
63104 KB |
Output is correct |
47 |
Correct |
257 ms |
61968 KB |
Output is correct |
48 |
Correct |
384 ms |
102860 KB |
Output is correct |
49 |
Correct |
372 ms |
100808 KB |
Output is correct |
50 |
Correct |
321 ms |
61972 KB |
Output is correct |
51 |
Correct |
306 ms |
61984 KB |
Output is correct |
52 |
Correct |
362 ms |
62188 KB |
Output is correct |
53 |
Correct |
368 ms |
65564 KB |
Output is correct |
54 |
Correct |
483 ms |
90412 KB |
Output is correct |
55 |
Correct |
37 ms |
26804 KB |
Output is correct |
56 |
Correct |
29 ms |
26008 KB |
Output is correct |
57 |
Correct |
123 ms |
45288 KB |
Output is correct |
58 |
Correct |
87 ms |
40224 KB |
Output is correct |
59 |
Correct |
181 ms |
63140 KB |
Output is correct |
60 |
Correct |
378 ms |
101380 KB |
Output is correct |
61 |
Correct |
407 ms |
64632 KB |
Output is correct |
62 |
Correct |
317 ms |
62084 KB |
Output is correct |
63 |
Correct |
321 ms |
62176 KB |
Output is correct |
64 |
Correct |
665 ms |
103724 KB |
Output is correct |
65 |
Correct |
553 ms |
102288 KB |
Output is correct |
66 |
Correct |
452 ms |
96264 KB |
Output is correct |
67 |
Correct |
283 ms |
58100 KB |
Output is correct |
68 |
Correct |
508 ms |
74856 KB |
Output is correct |
69 |
Correct |
555 ms |
78832 KB |
Output is correct |
70 |
Correct |
491 ms |
72788 KB |
Output is correct |