/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
typedef long long ll;
const int INF = INT_MAX / 2;
struct Edge
{
int u, v;
int w;
int other (int x)
{
return (u ^ v ^ x);
}
bool primary;
};
int N, M;
vector <Edge> edges;
vector <vector <Edge*>> adj;
vector <int> special;
vector <int> cost;
vector <int> parent;
vector <int> parw;
vector <int> depth;
int BITS;
vector <vector <int>> anc;
vector <vector <int>> ancw;
int ancestor (int &u, int k)
{
int maxw = 0;
for(int bit = BITS - 1; bit >= 0; bit--)
if((k >> bit) & 1)
{
maxw = max(maxw, ancw[u][bit]);
u = anc[u][bit];
}
return maxw;
}
int dist (int u, int v)
{
int maxw = 0;
if(depth[u] > depth[v])
maxw = max(maxw, ancestor(u, depth[u] - depth[v]));
if(depth[v] > depth[u])
maxw = max(maxw, ancestor(v, depth[v] - depth[u]));
if(u == v)
return maxw;
for(int bit = BITS - 1; bit >= 0; bit--)
if(anc[u][bit] != anc[v][bit])
{
maxw = max(maxw, ancw[u][bit]);
maxw = max(maxw, ancw[v][bit]);
u = anc[u][bit];
v = anc[v][bit];
}
maxw = max(maxw, parw[u]);
maxw = max(maxw, parw[v]);
return maxw;
}
void init (int _N, int _M, vector <int> U, vector <int> V, vector <int> W)
{
N = _N;
M = _M;
/// Process edges
edges = vector <Edge> (M);
for(int i = 0; i < M; i++)
edges[i] = Edge{U[i], V[i], W[i], false};
adj = vector <vector <Edge*>> (N);
for(int i = 0; i < M; i++)
{
adj[edges[i].u].push_back(&edges[i]);
adj[edges[i].v].push_back(&edges[i]);
}
/// Use DSU to find minimum spanning tree
{
vector <int> par (N);
for(int u = 0; u < N; u++)
par[u] = u;
function <int (int)> findRoot = [&] (int u)
{
if(par[u] == u)
return u;
return par[u] = findRoot(par[u]);
};
function <bool (int, int)> join = [&] (int u, int v)
{
u = findRoot(u);
v = findRoot(v);
if(u == v)
return false;
par[u] = v;
return true;
};
// Sort edges by weight
vector <int> edgeOrder (M);
for(int i = 0; i < M; i++)
edgeOrder[i] = i;
sort(edgeOrder.begin(), edgeOrder.end(),
[&] (const int &x, const int &y)
{
return edges[x].w < edges[y].w;
});
// Find "primary" edges that form the spanning tree
for(int j = 0; j < M; j++)
{
int i = edgeOrder[j];
if(join(edges[i].u, edges[i].v) == true)
edges[i].primary = true;
}
}
/// Use DFS to find the structure of the tree
parent = vector <int> (N, -1);
parw = vector <int> (N, 0);
depth = vector <int> (N, 0);
vector <bool> visited (N, false);
function <void (int)> dfs = [&] (int u)
{
visited[u] = true;
for(Edge* e : adj[u])
if(e->primary == true)
{
int v = e->other(u);
if(visited[v] == false)
{
depth[v] = depth[u] + 1;
parent[v] = u;
parw[v] = e->w;
dfs(v);
}
}
};
for(int u = 0; u < N; u++)
if(visited[u] == false)
dfs(u);
/// Use binary lifting to get ancestors and be able to compute distance
BITS = 0;
while((1 << BITS) <= N)
BITS++;
anc = vector <vector <int>> (N, vector <int> (BITS, -1));
ancw = vector <vector <int>> (N, vector <int> (BITS, 0));
for(int u = 0; u < N; u++)
{
anc[u][0] = parent[u];
ancw[u][0] = parw[u];
}
for(int bit = 1; bit < BITS; bit++)
for(int u = 0; u < N; u++)
{
if(anc[u][bit - 1] != -1)
{
anc[u][bit] = anc[anc[u][bit - 1]][bit - 1];
ancw[u][bit] = max(ancw[u][bit - 1], ancw[anc[u][bit - 1]][bit - 1]);
}
}
/// Find special cost for each node
// Intersection nodes
special = vector <int> (N, INF);
for(int u = 0; u < N; u++)
if((int) adj[u].size() >= 3)
{
vector <int> aux;
for(Edge* e : adj[u])
aux.push_back(e->w);
sort(aux.begin(), aux.end());
special[u] = aux[2];
}
// Cycle nodes
for(int i = 0; i < M; i++)
if(edges[i].primary == false)
special[edges[i].u] = edges[i].w;
/// Use DSU to find additional cost for each node
{
vector <int> par (N);
for(int u = 0; u < N; u++)
par[u] = u;
vector <int> wpar (N, 0);
vector <int> dim (N, 1);
vector <int> minCost (N, INF);
function <int (int)> findRoot = [&] (int u)
{
if(par[u] == u)
return u;
return findRoot(par[u]);
};
function <void (int, int, int)> join = [&] (int u, int v, int w)
{
u = findRoot(u);
v = findRoot(v);
if(u == v)
return;
if(dim[u] > dim[v])
swap(u, v);
par[u] = v;
wpar[u] = w;
dim[v] += dim[u];
if(minCost[v] == INF && minCost[u] != INF)
minCost[v] = w;
};
vector <int> edgeOrder (M);
for(int i = 0; i < M; i++)
edgeOrder[i] = i;
sort(edgeOrder.begin(), edgeOrder.end(),
[&] (const int &x, const int &y)
{
return edges[x].w < edges[y].w;
});
vector <int> specialOrder (N);
for(int u = 0; u < N; u++)
specialOrder[u] = u;
sort(specialOrder.begin(), specialOrder.end(),
[&] (const int &u, const int &v)
{
return special[u] < special[v];
});
int currEdge = 0;
int currSpecial = 0;
while(currEdge < M || currSpecial < N)
{
if(currSpecial == N || (currEdge < M
&& edges[edgeOrder[currEdge]].w
< special[specialOrder[currSpecial]]))
{
int i = edgeOrder[currEdge];
join(edges[i].u, edges[i].v, edges[i].w);
currEdge++;
}
else
{
int u = specialOrder[currSpecial];
int r = findRoot(u);
if(minCost[r] == INF)
minCost[r] = special[u];
currSpecial++;
}
}
cost = vector <int> (N);
for(int u = 0; u < N; u++)
{
cost[u] = minCost[u];
int v = u;
int maxw = 0;
while(par[v] != v)
{
maxw = max(maxw, wpar[v]);
v = par[v];
cost[u] = min(cost[u], max(maxw, minCost[v]));
}
}
}
}
int getMinimumFuelCapacity (int U, int V)
{
int answer = max(dist(U, V), cost[U]);
if(answer == INF)
return -1;
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
203 ms |
32332 KB |
Output is correct |
10 |
Correct |
232 ms |
40912 KB |
Output is correct |
11 |
Correct |
228 ms |
39840 KB |
Output is correct |
12 |
Correct |
246 ms |
42364 KB |
Output is correct |
13 |
Correct |
240 ms |
44128 KB |
Output is correct |
14 |
Correct |
212 ms |
31964 KB |
Output is correct |
15 |
Correct |
601 ms |
44684 KB |
Output is correct |
16 |
Correct |
589 ms |
42004 KB |
Output is correct |
17 |
Correct |
569 ms |
48020 KB |
Output is correct |
18 |
Correct |
597 ms |
46640 KB |
Output is correct |
19 |
Correct |
142 ms |
11808 KB |
Output is correct |
20 |
Correct |
560 ms |
44716 KB |
Output is correct |
21 |
Correct |
601 ms |
42660 KB |
Output is correct |
22 |
Correct |
692 ms |
46500 KB |
Output is correct |
23 |
Correct |
627 ms |
47108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
250 ms |
39376 KB |
Output is correct |
4 |
Correct |
231 ms |
41108 KB |
Output is correct |
5 |
Correct |
235 ms |
40132 KB |
Output is correct |
6 |
Correct |
251 ms |
40796 KB |
Output is correct |
7 |
Correct |
242 ms |
40676 KB |
Output is correct |
8 |
Correct |
229 ms |
39120 KB |
Output is correct |
9 |
Correct |
232 ms |
40320 KB |
Output is correct |
10 |
Correct |
244 ms |
38732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
560 KB |
Output is correct |
17 |
Correct |
2 ms |
556 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
4 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
203 ms |
32332 KB |
Output is correct |
11 |
Correct |
232 ms |
40912 KB |
Output is correct |
12 |
Correct |
228 ms |
39840 KB |
Output is correct |
13 |
Correct |
246 ms |
42364 KB |
Output is correct |
14 |
Correct |
240 ms |
44128 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
560 KB |
Output is correct |
22 |
Correct |
2 ms |
556 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
203 ms |
32332 KB |
Output is correct |
10 |
Correct |
232 ms |
40912 KB |
Output is correct |
11 |
Correct |
228 ms |
39840 KB |
Output is correct |
12 |
Correct |
246 ms |
42364 KB |
Output is correct |
13 |
Correct |
240 ms |
44128 KB |
Output is correct |
14 |
Correct |
212 ms |
31964 KB |
Output is correct |
15 |
Correct |
601 ms |
44684 KB |
Output is correct |
16 |
Correct |
589 ms |
42004 KB |
Output is correct |
17 |
Correct |
569 ms |
48020 KB |
Output is correct |
18 |
Correct |
597 ms |
46640 KB |
Output is correct |
19 |
Correct |
250 ms |
39376 KB |
Output is correct |
20 |
Correct |
231 ms |
41108 KB |
Output is correct |
21 |
Correct |
235 ms |
40132 KB |
Output is correct |
22 |
Correct |
251 ms |
40796 KB |
Output is correct |
23 |
Correct |
242 ms |
40676 KB |
Output is correct |
24 |
Correct |
229 ms |
39120 KB |
Output is correct |
25 |
Correct |
232 ms |
40320 KB |
Output is correct |
26 |
Correct |
244 ms |
38732 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
560 KB |
Output is correct |
34 |
Correct |
2 ms |
556 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
19 ms |
4836 KB |
Output is correct |
37 |
Correct |
255 ms |
41532 KB |
Output is correct |
38 |
Correct |
227 ms |
38952 KB |
Output is correct |
39 |
Correct |
215 ms |
37028 KB |
Output is correct |
40 |
Correct |
203 ms |
36124 KB |
Output is correct |
41 |
Correct |
205 ms |
35524 KB |
Output is correct |
42 |
Correct |
200 ms |
32756 KB |
Output is correct |
43 |
Correct |
241 ms |
40064 KB |
Output is correct |
44 |
Correct |
245 ms |
40872 KB |
Output is correct |
45 |
Correct |
222 ms |
42128 KB |
Output is correct |
46 |
Correct |
206 ms |
37028 KB |
Output is correct |
47 |
Correct |
29 ms |
4932 KB |
Output is correct |
48 |
Correct |
647 ms |
43916 KB |
Output is correct |
49 |
Correct |
614 ms |
42252 KB |
Output is correct |
50 |
Correct |
626 ms |
41112 KB |
Output is correct |
51 |
Correct |
506 ms |
40484 KB |
Output is correct |
52 |
Correct |
451 ms |
38040 KB |
Output is correct |
53 |
Correct |
335 ms |
29680 KB |
Output is correct |
54 |
Correct |
629 ms |
43932 KB |
Output is correct |
55 |
Correct |
598 ms |
44396 KB |
Output is correct |
56 |
Correct |
673 ms |
47020 KB |
Output is correct |
57 |
Correct |
424 ms |
41904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
203 ms |
32332 KB |
Output is correct |
11 |
Correct |
232 ms |
40912 KB |
Output is correct |
12 |
Correct |
228 ms |
39840 KB |
Output is correct |
13 |
Correct |
246 ms |
42364 KB |
Output is correct |
14 |
Correct |
240 ms |
44128 KB |
Output is correct |
15 |
Correct |
212 ms |
31964 KB |
Output is correct |
16 |
Correct |
601 ms |
44684 KB |
Output is correct |
17 |
Correct |
589 ms |
42004 KB |
Output is correct |
18 |
Correct |
569 ms |
48020 KB |
Output is correct |
19 |
Correct |
597 ms |
46640 KB |
Output is correct |
20 |
Correct |
250 ms |
39376 KB |
Output is correct |
21 |
Correct |
231 ms |
41108 KB |
Output is correct |
22 |
Correct |
235 ms |
40132 KB |
Output is correct |
23 |
Correct |
251 ms |
40796 KB |
Output is correct |
24 |
Correct |
242 ms |
40676 KB |
Output is correct |
25 |
Correct |
229 ms |
39120 KB |
Output is correct |
26 |
Correct |
232 ms |
40320 KB |
Output is correct |
27 |
Correct |
244 ms |
38732 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
560 KB |
Output is correct |
35 |
Correct |
2 ms |
556 KB |
Output is correct |
36 |
Correct |
2 ms |
588 KB |
Output is correct |
37 |
Correct |
19 ms |
4836 KB |
Output is correct |
38 |
Correct |
255 ms |
41532 KB |
Output is correct |
39 |
Correct |
227 ms |
38952 KB |
Output is correct |
40 |
Correct |
215 ms |
37028 KB |
Output is correct |
41 |
Correct |
203 ms |
36124 KB |
Output is correct |
42 |
Correct |
205 ms |
35524 KB |
Output is correct |
43 |
Correct |
200 ms |
32756 KB |
Output is correct |
44 |
Correct |
241 ms |
40064 KB |
Output is correct |
45 |
Correct |
245 ms |
40872 KB |
Output is correct |
46 |
Correct |
222 ms |
42128 KB |
Output is correct |
47 |
Correct |
206 ms |
37028 KB |
Output is correct |
48 |
Correct |
29 ms |
4932 KB |
Output is correct |
49 |
Correct |
647 ms |
43916 KB |
Output is correct |
50 |
Correct |
614 ms |
42252 KB |
Output is correct |
51 |
Correct |
626 ms |
41112 KB |
Output is correct |
52 |
Correct |
506 ms |
40484 KB |
Output is correct |
53 |
Correct |
451 ms |
38040 KB |
Output is correct |
54 |
Correct |
335 ms |
29680 KB |
Output is correct |
55 |
Correct |
629 ms |
43932 KB |
Output is correct |
56 |
Correct |
598 ms |
44396 KB |
Output is correct |
57 |
Correct |
673 ms |
47020 KB |
Output is correct |
58 |
Correct |
424 ms |
41904 KB |
Output is correct |
59 |
Correct |
142 ms |
11808 KB |
Output is correct |
60 |
Correct |
560 ms |
44716 KB |
Output is correct |
61 |
Correct |
601 ms |
42660 KB |
Output is correct |
62 |
Correct |
692 ms |
46500 KB |
Output is correct |
63 |
Correct |
627 ms |
47108 KB |
Output is correct |
64 |
Correct |
4 ms |
460 KB |
Output is correct |
65 |
Correct |
2 ms |
588 KB |
Output is correct |
66 |
Correct |
2 ms |
460 KB |
Output is correct |
67 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |