#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i,a) for (int i=0; i<(a); i++)
#define FORI(i,a,b) for (int i=a; i<(b); i++)
#define FORd(i,a) for (int i=(a)-1; i>=0; i--)
#define FORId(i,a,b) for (int i=(a)-1; i>=(b); i--)
#define trav(a,x) for (auto& a : x)
#define uid(a,b) uniform_int_distribution<int>(a, b)(rng)
#define printv(i,x) trav(i,x) cout << i << " "; cout << "\n";
#define sz(x) (int) (x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define LEFT(x) (2*x)
#define RIGHT(x) (2*x+1)
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
#define MAX 200000
int num_of_vertices, kilometers;
vi edges[MAX];
vl costs[MAX];
vector<set<ll>> sumk(MAX);
vector<map<ll,int>> highway(MAX);
vi sub_size(MAX);
int ans, mini = INF;
void p(int u, ll cost, int qnts, int ant)
{
// cout << u << " " << ant << "\n";
sub_size[u] = 1;
int maxi = -1, qual = -1;
FOR(i,sz(edges[u]))
{
int v = edges[u][i];
int w = costs[u][i];
if (v == ant) continue;
p(v,cost+w,qnts+1,u);
sub_size[u] += sub_size[v];
if (sub_size[v] > maxi)
{
maxi = sub_size[v];
qual = v;
}
}
// cout << u << " " << ant << ":\n";
// cout << qual << " " << maxi << "\n";
if (qual != -1)
{
swap(sumk[u],sumk[qual]); // O(1)
swap(highway[u],highway[qual]);
// Update
sumk[u].insert(cost);
highway[u][cost] = qnts;
FOR(i,sz(edges[u]))
{
int v = edges[u][i];
if (v == qual or v == ant) continue;
for (auto j : sumk[v])
{
/*
k = d[a]-d[u] + d[b]-d[u]
d[b] = k-d[a]+2*d[u]
*/
// Queries
if (sumk[u].count(kilometers-(j-cost)+cost))
mini = min(mini,highway[u][kilometers-(j-cost)+cost]-qnts + highway[v][j]-qnts);
}
for (auto j : sumk[v])
{
// Updates
sumk[u].insert(j);
if(highway[u].count(j)){
highway[u][j] = min(highway[u][j], highway[v][j]);
}
else{
highway[u][j] = highway[v][j];
}
}
}
}
else
{
sumk[u].insert(cost);
highway[u][cost] = qnts;
}
// Query
if (sumk[u].count(kilometers+cost))
mini = min(mini,highway[u][kilometers+cost]-qnts);
// cout << "mini: " << mini << "\n";
// cout << "sumk de " << u << ": ";
// for (auto i : sumk[u])
// cout << i << " ";
// cout << "\n";
// cout << "highway de " << u << ": ";
// for (auto i : highway[u])
// cout << i.f << ": " << i.s << " | ";
// cout << "\n";
}
int best_path(int n, int k, int (*h)[2], int* l)
{
FOR(i,n)
{
edges[i].clear();
costs[i].clear();
sumk[i].clear();
highway[i].clear();
sub_size[i] = 0;
mini = INF;
}
FOR(i,n-1)
{
ll u, v, w;
u = h[i][0];
v = h[i][1];
w = l[i];
edges[u].pb(v);
edges[v].pb(u);
costs[u].pb(w);
costs[v].pb(w);
}
num_of_vertices = n;
kilometers = k;
p(0,0,0,-1);
ans = mini;
if (ans == INF)
ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
29268 KB |
Output is correct |
2 |
Correct |
14 ms |
29300 KB |
Output is correct |
3 |
Correct |
13 ms |
29296 KB |
Output is correct |
4 |
Correct |
13 ms |
29288 KB |
Output is correct |
5 |
Correct |
15 ms |
29268 KB |
Output is correct |
6 |
Correct |
13 ms |
29188 KB |
Output is correct |
7 |
Correct |
14 ms |
29268 KB |
Output is correct |
8 |
Correct |
14 ms |
29268 KB |
Output is correct |
9 |
Correct |
16 ms |
29292 KB |
Output is correct |
10 |
Correct |
13 ms |
29268 KB |
Output is correct |
11 |
Correct |
14 ms |
29200 KB |
Output is correct |
12 |
Correct |
14 ms |
29268 KB |
Output is correct |
13 |
Correct |
13 ms |
29268 KB |
Output is correct |
14 |
Correct |
14 ms |
29244 KB |
Output is correct |
15 |
Correct |
15 ms |
29272 KB |
Output is correct |
16 |
Correct |
15 ms |
29268 KB |
Output is correct |
17 |
Correct |
14 ms |
29232 KB |
Output is correct |
18 |
Correct |
15 ms |
29268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
29268 KB |
Output is correct |
2 |
Correct |
14 ms |
29300 KB |
Output is correct |
3 |
Correct |
13 ms |
29296 KB |
Output is correct |
4 |
Correct |
13 ms |
29288 KB |
Output is correct |
5 |
Correct |
15 ms |
29268 KB |
Output is correct |
6 |
Correct |
13 ms |
29188 KB |
Output is correct |
7 |
Correct |
14 ms |
29268 KB |
Output is correct |
8 |
Correct |
14 ms |
29268 KB |
Output is correct |
9 |
Correct |
16 ms |
29292 KB |
Output is correct |
10 |
Correct |
13 ms |
29268 KB |
Output is correct |
11 |
Correct |
14 ms |
29200 KB |
Output is correct |
12 |
Correct |
14 ms |
29268 KB |
Output is correct |
13 |
Correct |
13 ms |
29268 KB |
Output is correct |
14 |
Correct |
14 ms |
29244 KB |
Output is correct |
15 |
Correct |
15 ms |
29272 KB |
Output is correct |
16 |
Correct |
15 ms |
29268 KB |
Output is correct |
17 |
Correct |
14 ms |
29232 KB |
Output is correct |
18 |
Correct |
15 ms |
29268 KB |
Output is correct |
19 |
Correct |
15 ms |
29240 KB |
Output is correct |
20 |
Correct |
15 ms |
29204 KB |
Output is correct |
21 |
Correct |
15 ms |
29524 KB |
Output is correct |
22 |
Correct |
16 ms |
29652 KB |
Output is correct |
23 |
Correct |
16 ms |
29652 KB |
Output is correct |
24 |
Correct |
15 ms |
29652 KB |
Output is correct |
25 |
Correct |
15 ms |
29652 KB |
Output is correct |
26 |
Correct |
17 ms |
29636 KB |
Output is correct |
27 |
Correct |
15 ms |
29384 KB |
Output is correct |
28 |
Correct |
17 ms |
29608 KB |
Output is correct |
29 |
Correct |
16 ms |
29712 KB |
Output is correct |
30 |
Correct |
19 ms |
29684 KB |
Output is correct |
31 |
Correct |
16 ms |
29792 KB |
Output is correct |
32 |
Correct |
16 ms |
29604 KB |
Output is correct |
33 |
Correct |
15 ms |
29660 KB |
Output is correct |
34 |
Correct |
16 ms |
29524 KB |
Output is correct |
35 |
Correct |
15 ms |
29524 KB |
Output is correct |
36 |
Correct |
15 ms |
29456 KB |
Output is correct |
37 |
Correct |
15 ms |
29524 KB |
Output is correct |
38 |
Correct |
15 ms |
29524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
29268 KB |
Output is correct |
2 |
Correct |
14 ms |
29300 KB |
Output is correct |
3 |
Correct |
13 ms |
29296 KB |
Output is correct |
4 |
Correct |
13 ms |
29288 KB |
Output is correct |
5 |
Correct |
15 ms |
29268 KB |
Output is correct |
6 |
Correct |
13 ms |
29188 KB |
Output is correct |
7 |
Correct |
14 ms |
29268 KB |
Output is correct |
8 |
Correct |
14 ms |
29268 KB |
Output is correct |
9 |
Correct |
16 ms |
29292 KB |
Output is correct |
10 |
Correct |
13 ms |
29268 KB |
Output is correct |
11 |
Correct |
14 ms |
29200 KB |
Output is correct |
12 |
Correct |
14 ms |
29268 KB |
Output is correct |
13 |
Correct |
13 ms |
29268 KB |
Output is correct |
14 |
Correct |
14 ms |
29244 KB |
Output is correct |
15 |
Correct |
15 ms |
29272 KB |
Output is correct |
16 |
Correct |
15 ms |
29268 KB |
Output is correct |
17 |
Correct |
14 ms |
29232 KB |
Output is correct |
18 |
Correct |
15 ms |
29268 KB |
Output is correct |
19 |
Correct |
180 ms |
56680 KB |
Output is correct |
20 |
Correct |
176 ms |
56636 KB |
Output is correct |
21 |
Correct |
178 ms |
56644 KB |
Output is correct |
22 |
Correct |
195 ms |
56908 KB |
Output is correct |
23 |
Correct |
270 ms |
80348 KB |
Output is correct |
24 |
Correct |
215 ms |
67788 KB |
Output is correct |
25 |
Correct |
106 ms |
46108 KB |
Output is correct |
26 |
Correct |
67 ms |
52380 KB |
Output is correct |
27 |
Correct |
305 ms |
68460 KB |
Output is correct |
28 |
Correct |
307 ms |
100368 KB |
Output is correct |
29 |
Correct |
334 ms |
99668 KB |
Output is correct |
30 |
Correct |
310 ms |
71372 KB |
Output is correct |
31 |
Correct |
305 ms |
71336 KB |
Output is correct |
32 |
Correct |
362 ms |
71476 KB |
Output is correct |
33 |
Correct |
301 ms |
69560 KB |
Output is correct |
34 |
Correct |
483 ms |
126216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
29268 KB |
Output is correct |
2 |
Correct |
14 ms |
29300 KB |
Output is correct |
3 |
Correct |
13 ms |
29296 KB |
Output is correct |
4 |
Correct |
13 ms |
29288 KB |
Output is correct |
5 |
Correct |
15 ms |
29268 KB |
Output is correct |
6 |
Correct |
13 ms |
29188 KB |
Output is correct |
7 |
Correct |
14 ms |
29268 KB |
Output is correct |
8 |
Correct |
14 ms |
29268 KB |
Output is correct |
9 |
Correct |
16 ms |
29292 KB |
Output is correct |
10 |
Correct |
13 ms |
29268 KB |
Output is correct |
11 |
Correct |
14 ms |
29200 KB |
Output is correct |
12 |
Correct |
14 ms |
29268 KB |
Output is correct |
13 |
Correct |
13 ms |
29268 KB |
Output is correct |
14 |
Correct |
14 ms |
29244 KB |
Output is correct |
15 |
Correct |
15 ms |
29272 KB |
Output is correct |
16 |
Correct |
15 ms |
29268 KB |
Output is correct |
17 |
Correct |
14 ms |
29232 KB |
Output is correct |
18 |
Correct |
15 ms |
29268 KB |
Output is correct |
19 |
Correct |
15 ms |
29240 KB |
Output is correct |
20 |
Correct |
15 ms |
29204 KB |
Output is correct |
21 |
Correct |
15 ms |
29524 KB |
Output is correct |
22 |
Correct |
16 ms |
29652 KB |
Output is correct |
23 |
Correct |
16 ms |
29652 KB |
Output is correct |
24 |
Correct |
15 ms |
29652 KB |
Output is correct |
25 |
Correct |
15 ms |
29652 KB |
Output is correct |
26 |
Correct |
17 ms |
29636 KB |
Output is correct |
27 |
Correct |
15 ms |
29384 KB |
Output is correct |
28 |
Correct |
17 ms |
29608 KB |
Output is correct |
29 |
Correct |
16 ms |
29712 KB |
Output is correct |
30 |
Correct |
19 ms |
29684 KB |
Output is correct |
31 |
Correct |
16 ms |
29792 KB |
Output is correct |
32 |
Correct |
16 ms |
29604 KB |
Output is correct |
33 |
Correct |
15 ms |
29660 KB |
Output is correct |
34 |
Correct |
16 ms |
29524 KB |
Output is correct |
35 |
Correct |
15 ms |
29524 KB |
Output is correct |
36 |
Correct |
15 ms |
29456 KB |
Output is correct |
37 |
Correct |
15 ms |
29524 KB |
Output is correct |
38 |
Correct |
15 ms |
29524 KB |
Output is correct |
39 |
Correct |
180 ms |
56680 KB |
Output is correct |
40 |
Correct |
176 ms |
56636 KB |
Output is correct |
41 |
Correct |
178 ms |
56644 KB |
Output is correct |
42 |
Correct |
195 ms |
56908 KB |
Output is correct |
43 |
Correct |
270 ms |
80348 KB |
Output is correct |
44 |
Correct |
215 ms |
67788 KB |
Output is correct |
45 |
Correct |
106 ms |
46108 KB |
Output is correct |
46 |
Correct |
67 ms |
52380 KB |
Output is correct |
47 |
Correct |
305 ms |
68460 KB |
Output is correct |
48 |
Correct |
307 ms |
100368 KB |
Output is correct |
49 |
Correct |
334 ms |
99668 KB |
Output is correct |
50 |
Correct |
310 ms |
71372 KB |
Output is correct |
51 |
Correct |
305 ms |
71336 KB |
Output is correct |
52 |
Correct |
362 ms |
71476 KB |
Output is correct |
53 |
Correct |
301 ms |
69560 KB |
Output is correct |
54 |
Correct |
483 ms |
126216 KB |
Output is correct |
55 |
Correct |
35 ms |
33956 KB |
Output is correct |
56 |
Correct |
24 ms |
31648 KB |
Output is correct |
57 |
Correct |
115 ms |
55788 KB |
Output is correct |
58 |
Correct |
73 ms |
49920 KB |
Output is correct |
59 |
Correct |
106 ms |
64624 KB |
Output is correct |
60 |
Correct |
312 ms |
99752 KB |
Output is correct |
61 |
Correct |
362 ms |
77432 KB |
Output is correct |
62 |
Correct |
305 ms |
71404 KB |
Output is correct |
63 |
Correct |
368 ms |
71404 KB |
Output is correct |
64 |
Correct |
707 ms |
159376 KB |
Output is correct |
65 |
Correct |
702 ms |
160172 KB |
Output is correct |
66 |
Correct |
355 ms |
98180 KB |
Output is correct |
67 |
Correct |
295 ms |
72012 KB |
Output is correct |
68 |
Correct |
559 ms |
103780 KB |
Output is correct |
69 |
Correct |
573 ms |
104800 KB |
Output is correct |
70 |
Correct |
520 ms |
100672 KB |
Output is correct |