#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
const int MAXN = 2e5;
const int MAXK = 1e6 + 1;
vector<pair<ll, ll> > g[MAXN];
vector<ll>* vec[MAXN];
ll sz[MAXN], big[MAXN];
//map<ll, ll> cnt[MAXK];
map<ll, map<ll, ll> > cnt;
ll dep[MAXN];
ll d[MAXN];
void sizes(ll v, ll p, ll depth = 0, ll dist = 0)
{
dep[v] = depth;
d[v] = dist;
sz[v] = 1;
ll b = -1, mx = 0;
for (auto& [u, w]: g[v])
{
if (u != p)
{
sizes(u, v, depth + 1, dist + w);
sz[v] += sz[u];
if (sz[u] > mx)
{
mx = sz[u];
b = u;
}
}
}
big[v] = b;
}
ll ans = 1e9, k, n;
void dfs(ll v, ll p, bool keep = 0)
{
for (auto& [u, w] : g[v])
{
if (u != p && u != big[v])
{
dfs(u, v, 0);
}
}
if (big[v] != -1)
{
dfs(big[v], v, 1);
vec[v] = vec[big[v]];
}
else
{
vec[v] = new vector<ll>();
}
vec[v]->push_back(v);
ll y = k + d[v];
if (cnt.count(y) && cnt[y].size() > 0 && cnt[y].begin()->first - dep[v] < ans)
{
ans = cnt[y].begin()->first - dep[v];
}
cnt[d[v]][dep[v]]++;
for (auto& [u, w] : g[v])
{
if (u != p && u != big[v])
{
for (auto& w : *vec[u])
{
ll x = k * 1ll + 2 * 1ll * d[v] - d[w];
if (cnt.count(x) && cnt[x].size() > 0)
{
ll cur = cnt[x].begin()->first + dep[w];
if (cur - 2 * 1ll * dep[v] < ans)
{
ans = cur - 2 * 1ll * dep[v];
}
}
vec[v]->push_back(w);
}
for (auto& w : *vec[u])
cnt[d[w]][dep[w]]++;
}
}
if (!keep)
{
for (auto& u : *vec[v])
{
cnt[d[u]][dep[u]]--;
if (cnt[d[u]][dep[u]] == 0)
cnt[d[u]].erase(dep[u]);
if (cnt[d[u]].empty())
cnt.erase(d[u]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N, k = K;
for (int i = 0; i < n - 1; i++)
{
int x = H[i][0], y = H[i][1], z = L[i];
g[x].push_back({y, z});
g[y].push_back({x, z});
}
sizes(0, 0);
dfs(0, 0);
return (ans == 1e9 ? -1 :ans);
}
/*
4 3
0 1 1
1 2 2
1 3 4
2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5252 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5284 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
5 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
5 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
4 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5276 KB |
Output is correct |
36 |
Correct |
4 ms |
5280 KB |
Output is correct |
37 |
Correct |
4 ms |
5204 KB |
Output is correct |
38 |
Correct |
4 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
237 ms |
23180 KB |
Output is correct |
20 |
Correct |
246 ms |
24352 KB |
Output is correct |
21 |
Correct |
233 ms |
24736 KB |
Output is correct |
22 |
Correct |
222 ms |
24396 KB |
Output is correct |
23 |
Correct |
401 ms |
26500 KB |
Output is correct |
24 |
Correct |
219 ms |
26228 KB |
Output is correct |
25 |
Correct |
119 ms |
32324 KB |
Output is correct |
26 |
Correct |
76 ms |
38900 KB |
Output is correct |
27 |
Correct |
265 ms |
42060 KB |
Output is correct |
28 |
Correct |
321 ms |
91652 KB |
Output is correct |
29 |
Correct |
323 ms |
89512 KB |
Output is correct |
30 |
Correct |
263 ms |
42040 KB |
Output is correct |
31 |
Correct |
287 ms |
41984 KB |
Output is correct |
32 |
Correct |
377 ms |
42180 KB |
Output is correct |
33 |
Correct |
312 ms |
36548 KB |
Output is correct |
34 |
Correct |
695 ms |
66332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5252 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5284 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
5 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
5 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
4 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5276 KB |
Output is correct |
36 |
Correct |
4 ms |
5280 KB |
Output is correct |
37 |
Correct |
4 ms |
5204 KB |
Output is correct |
38 |
Correct |
4 ms |
5332 KB |
Output is correct |
39 |
Correct |
237 ms |
23180 KB |
Output is correct |
40 |
Correct |
246 ms |
24352 KB |
Output is correct |
41 |
Correct |
233 ms |
24736 KB |
Output is correct |
42 |
Correct |
222 ms |
24396 KB |
Output is correct |
43 |
Correct |
401 ms |
26500 KB |
Output is correct |
44 |
Correct |
219 ms |
26228 KB |
Output is correct |
45 |
Correct |
119 ms |
32324 KB |
Output is correct |
46 |
Correct |
76 ms |
38900 KB |
Output is correct |
47 |
Correct |
265 ms |
42060 KB |
Output is correct |
48 |
Correct |
321 ms |
91652 KB |
Output is correct |
49 |
Correct |
323 ms |
89512 KB |
Output is correct |
50 |
Correct |
263 ms |
42040 KB |
Output is correct |
51 |
Correct |
287 ms |
41984 KB |
Output is correct |
52 |
Correct |
377 ms |
42180 KB |
Output is correct |
53 |
Correct |
312 ms |
36548 KB |
Output is correct |
54 |
Correct |
695 ms |
66332 KB |
Output is correct |
55 |
Correct |
27 ms |
7960 KB |
Output is correct |
56 |
Correct |
15 ms |
6868 KB |
Output is correct |
57 |
Correct |
149 ms |
26352 KB |
Output is correct |
58 |
Correct |
68 ms |
23856 KB |
Output is correct |
59 |
Correct |
106 ms |
48192 KB |
Output is correct |
60 |
Correct |
314 ms |
90012 KB |
Output is correct |
61 |
Correct |
410 ms |
48644 KB |
Output is correct |
62 |
Correct |
284 ms |
42128 KB |
Output is correct |
63 |
Correct |
339 ms |
42252 KB |
Output is correct |
64 |
Correct |
989 ms |
65300 KB |
Output is correct |
65 |
Correct |
995 ms |
75496 KB |
Output is correct |
66 |
Correct |
334 ms |
84748 KB |
Output is correct |
67 |
Correct |
228 ms |
43856 KB |
Output is correct |
68 |
Correct |
611 ms |
68924 KB |
Output is correct |
69 |
Correct |
661 ms |
69276 KB |
Output is correct |
70 |
Correct |
668 ms |
66076 KB |
Output is correct |