#include "race.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll maxN = 200507;
const ll LG = 18;
ll n, m, k;
set<pair<ll, ll>>g[maxN];
ll sz[maxN];
ll ans = INF;
map<ll, ll>cnt;
void dfs(ll v, ll par = -1) {
sz[v] = 1;
for (auto to : g[v]) {
if (to.ff != par) {
dfs(to.ff, v);
sz[v] += sz[to.ff];
}
}
}
ll centroid(ll v, ll size, ll par = -1) {
for (auto to : g[v]) {
if (to.ff != par && sz[to.ff] > size / 2) {
return centroid(to.ff, size, v);
}
}
return v;
}
void upd_ans(ll v, ll dpt, ll size, ll cur, ll par = -1) {
if (dpt <= k) {
//cout << dpt << " ";
if (k == dpt) {
ans = min(ans, cur);
//cout << "E ARECI HETO?\n";
}
else {
ans = min(ans, (cnt[k - dpt] == 0 ? INF : cnt[k - dpt]) + cur);
}
}
for (auto to : g[v]) {
if (to.ff != par) {
upd_ans(to.ff, dpt + to.ss, size, cur + 1, v);
}
}
}
void upd_cnt(ll v, ll dpt, ll cur, ll par = -1) {
if (dpt != 0) {
//cout << dpt << " ";
if (cnt[dpt] == 0) {
cnt[dpt] = cur;
}
else {
cnt[dpt] = min(cnt[dpt], cur);
}
//cout << v << ": " << dpt << ": " << cnt[dpt] << endl;
}
for (auto to : g[v]) {
if (to.ff != par) {
upd_cnt(to.ff, dpt + to.ss, cur + 1, v);
}
}
}
void mshak(ll c, ll size) {
//cout << "MSHAKUM ENQ: " << c << endl;
cnt.clear();
for (auto to : g[c]) {
upd_ans(to.ff, to.ss, size, 1, c);
upd_cnt(to.ff, to.ss, 1, c);
}
/*for (auto x : cnt) {
if(x.ff==k)cout << x.ff << ": " << x.ss << endl;
}*/
}
void cntr_dec(ll v, ll par = -1) {
dfs(v, par);
ll c = centroid(v, sz[v], par);
mshak(c, sz[v]);
set<pair<ll, ll>>g_copy(g[c]);
for (auto to : g_copy) {
g[c].erase(to);
g[to.ff].erase({ c,to.ss });
cntr_dec(to.ff, c);
}
}
int best_path(int NN, int K, int H[][2], int L[]) {
n = NN;
k = K;
for (int i = 0; i < n - 1; i++) {
g[H[i][0]].insert({ H[i][1],L[i] });
g[H[i][1]].insert({ H[i][0],L[i] });
}
cntr_dec(1);
return (ans == INF ? -1 : ans);
}
/// ---- - -------- ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - -------- ------ -------- -- - - -
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9772 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9736 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9772 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9736 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9732 KB |
Output is correct |
20 |
Correct |
5 ms |
9812 KB |
Output is correct |
21 |
Correct |
7 ms |
9872 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9940 KB |
Output is correct |
31 |
Correct |
7 ms |
9952 KB |
Output is correct |
32 |
Correct |
7 ms |
9940 KB |
Output is correct |
33 |
Correct |
8 ms |
10004 KB |
Output is correct |
34 |
Correct |
7 ms |
9872 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9864 KB |
Output is correct |
37 |
Correct |
8 ms |
9872 KB |
Output is correct |
38 |
Correct |
7 ms |
10000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9772 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9736 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
363 ms |
25680 KB |
Output is correct |
20 |
Correct |
355 ms |
25676 KB |
Output is correct |
21 |
Correct |
362 ms |
25568 KB |
Output is correct |
22 |
Correct |
344 ms |
25592 KB |
Output is correct |
23 |
Correct |
403 ms |
25804 KB |
Output is correct |
24 |
Correct |
218 ms |
25548 KB |
Output is correct |
25 |
Correct |
325 ms |
31532 KB |
Output is correct |
26 |
Correct |
137 ms |
31692 KB |
Output is correct |
27 |
Correct |
460 ms |
41740 KB |
Output is correct |
28 |
Correct |
1126 ms |
65808 KB |
Output is correct |
29 |
Correct |
1104 ms |
65168 KB |
Output is correct |
30 |
Correct |
461 ms |
41784 KB |
Output is correct |
31 |
Correct |
454 ms |
41640 KB |
Output is correct |
32 |
Correct |
540 ms |
41888 KB |
Output is correct |
33 |
Correct |
742 ms |
41800 KB |
Output is correct |
34 |
Correct |
954 ms |
54888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9772 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9736 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9732 KB |
Output is correct |
20 |
Correct |
5 ms |
9812 KB |
Output is correct |
21 |
Correct |
7 ms |
9872 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9940 KB |
Output is correct |
31 |
Correct |
7 ms |
9952 KB |
Output is correct |
32 |
Correct |
7 ms |
9940 KB |
Output is correct |
33 |
Correct |
8 ms |
10004 KB |
Output is correct |
34 |
Correct |
7 ms |
9872 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9864 KB |
Output is correct |
37 |
Correct |
8 ms |
9872 KB |
Output is correct |
38 |
Correct |
7 ms |
10000 KB |
Output is correct |
39 |
Correct |
363 ms |
25680 KB |
Output is correct |
40 |
Correct |
355 ms |
25676 KB |
Output is correct |
41 |
Correct |
362 ms |
25568 KB |
Output is correct |
42 |
Correct |
344 ms |
25592 KB |
Output is correct |
43 |
Correct |
403 ms |
25804 KB |
Output is correct |
44 |
Correct |
218 ms |
25548 KB |
Output is correct |
45 |
Correct |
325 ms |
31532 KB |
Output is correct |
46 |
Correct |
137 ms |
31692 KB |
Output is correct |
47 |
Correct |
460 ms |
41740 KB |
Output is correct |
48 |
Correct |
1126 ms |
65808 KB |
Output is correct |
49 |
Correct |
1104 ms |
65168 KB |
Output is correct |
50 |
Correct |
461 ms |
41784 KB |
Output is correct |
51 |
Correct |
454 ms |
41640 KB |
Output is correct |
52 |
Correct |
540 ms |
41888 KB |
Output is correct |
53 |
Correct |
742 ms |
41800 KB |
Output is correct |
54 |
Correct |
954 ms |
54888 KB |
Output is correct |
55 |
Correct |
34 ms |
11728 KB |
Output is correct |
56 |
Correct |
24 ms |
11220 KB |
Output is correct |
57 |
Correct |
219 ms |
25468 KB |
Output is correct |
58 |
Correct |
110 ms |
31436 KB |
Output is correct |
59 |
Correct |
440 ms |
40584 KB |
Output is correct |
60 |
Correct |
1526 ms |
74316 KB |
Output is correct |
61 |
Correct |
529 ms |
43464 KB |
Output is correct |
62 |
Correct |
503 ms |
41976 KB |
Output is correct |
63 |
Correct |
612 ms |
41868 KB |
Output is correct |
64 |
Correct |
1662 ms |
54316 KB |
Output is correct |
65 |
Correct |
975 ms |
54988 KB |
Output is correct |
66 |
Correct |
1409 ms |
65600 KB |
Output is correct |
67 |
Correct |
390 ms |
54588 KB |
Output is correct |
68 |
Correct |
870 ms |
57180 KB |
Output is correct |
69 |
Correct |
882 ms |
57460 KB |
Output is correct |
70 |
Correct |
759 ms |
55116 KB |
Output is correct |