#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "swap.h"
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 3e5 + 10;
ve<int> gr[N];
int p[N], deg[N], good[N], val[N]; int w = 0;
inline int par(int v) { if (p[v] == v) { return v; } return (p[v] = par(p[v])); }
inline void uni(int a, int b, int we) {
++deg[a], ++deg[b];
int pa = par(a), pb = par(b);
int ok = 0;
if (deg[a] >= 3) ok = 1;
if (deg[b] >= 3) ok = 1;
a = pa, b = pb;
if (a == b) ok = 1;
int c = w++;
p[c] = p[a] = p[b] = c;
val[c] = we, good[c] = ok;
gr[c].pb(a);
if (a ^ b) gr[c].pb(b);
}
int tim = 0;
const int lg = 19;
ve<int> eu;
int in[N], out[N], up[N][lg];
inline void dfs(int v, int p) {
in[v] = sz(eu); eu.pb(v); up[v][0] = p;
for (auto &to : gr[v]) dfs(to, v);
out[v] = sz(eu) - 1;
}
inline bool anc(int a, int b) { return in[a] <= in[b] && out[b] <= out[a]; }
int pf[N];
inline int fnd(int a, int b) {
for (int i = lg - 1; ~i; --i) {
int u = up[a][i];
if (!anc(u, b) || pf[out[u]] - (!in[u] ? 0 : pf[in[u] - 1]) == 0) {
a = u;
}
} return up[a][0];
}
void init(int n, int m, ve<int> v1, ve<int> v2, ve<int> we) {
for (int i = 0; i < n; ++i) p[i] = i, good[i] = 0, deg[i] = 0;
w = n;
ve<array<int,3>> e(m);
for (int i = 0; i < m; ++i) e[i] = {we[i], v1[i], v2[i]};
sort(all(e));
for (int i = 0; i < m; ++i) uni(e[i][1], e[i][2], e[i][0]);
for (int i = 0; i < w; ++i) if (par(i) == i) dfs(i, i);
for (int i = 1; i < lg; ++i) {
for (int v = 0; v < w; ++v) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
}
assert(sz(eu) == w);
for (int i = 0; i < sz(eu); ++i) pf[i] = good[eu[i]];
for (int i = 1; i < sz(eu); ++i) pf[i] += pf[i - 1];
}
int getMinimumFuelCapacity(int X, int Y) {
int ver = fnd(X, Y);
if (pf[out[ver]] - (!in[ver] ? 0 : pf[in[ver] - 1]) == 0) return -1;
return val[ver];
}
#ifdef LOCAL
int main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> X(Q), Y(Q);
for (int i = 0; i < Q; ++i) {
assert(2 == scanf("%d %d", &X[i], &Y[i]));
}
init(N, M, U, V, W);
std::vector<int> minimum_fuel_capacities(Q);
for (int i = 0; i < Q; ++i) {
minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
}
for (int i = 0; i < Q; ++i) {
printf("%d\n", minimum_fuel_capacities[i]);
}
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7388 KB |
Output is correct |
3 |
Correct |
5 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7632 KB |
Output is correct |
6 |
Correct |
4 ms |
7636 KB |
Output is correct |
7 |
Correct |
4 ms |
7636 KB |
Output is correct |
8 |
Correct |
4 ms |
7652 KB |
Output is correct |
9 |
Correct |
83 ms |
30280 KB |
Output is correct |
10 |
Correct |
104 ms |
35488 KB |
Output is correct |
11 |
Correct |
105 ms |
34984 KB |
Output is correct |
12 |
Correct |
142 ms |
36548 KB |
Output is correct |
13 |
Correct |
110 ms |
38212 KB |
Output is correct |
14 |
Correct |
93 ms |
30680 KB |
Output is correct |
15 |
Correct |
212 ms |
39464 KB |
Output is correct |
16 |
Correct |
202 ms |
38676 KB |
Output is correct |
17 |
Correct |
193 ms |
40428 KB |
Output is correct |
18 |
Correct |
189 ms |
41936 KB |
Output is correct |
19 |
Correct |
103 ms |
17056 KB |
Output is correct |
20 |
Correct |
200 ms |
39676 KB |
Output is correct |
21 |
Correct |
197 ms |
38956 KB |
Output is correct |
22 |
Correct |
228 ms |
40780 KB |
Output is correct |
23 |
Correct |
225 ms |
42304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7388 KB |
Output is correct |
3 |
Correct |
321 ms |
41388 KB |
Output is correct |
4 |
Correct |
288 ms |
42816 KB |
Output is correct |
5 |
Correct |
309 ms |
42168 KB |
Output is correct |
6 |
Correct |
295 ms |
42688 KB |
Output is correct |
7 |
Correct |
284 ms |
42636 KB |
Output is correct |
8 |
Correct |
318 ms |
41288 KB |
Output is correct |
9 |
Correct |
281 ms |
42252 KB |
Output is correct |
10 |
Correct |
305 ms |
40956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7388 KB |
Output is correct |
3 |
Correct |
5 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7632 KB |
Output is correct |
6 |
Correct |
4 ms |
7636 KB |
Output is correct |
7 |
Correct |
4 ms |
7636 KB |
Output is correct |
8 |
Correct |
4 ms |
7652 KB |
Output is correct |
9 |
Correct |
4 ms |
7368 KB |
Output is correct |
10 |
Correct |
4 ms |
7636 KB |
Output is correct |
11 |
Correct |
4 ms |
7636 KB |
Output is correct |
12 |
Correct |
5 ms |
7628 KB |
Output is correct |
13 |
Correct |
5 ms |
7632 KB |
Output is correct |
14 |
Correct |
4 ms |
7636 KB |
Output is correct |
15 |
Correct |
4 ms |
7652 KB |
Output is correct |
16 |
Correct |
5 ms |
7636 KB |
Output is correct |
17 |
Correct |
5 ms |
7636 KB |
Output is correct |
18 |
Correct |
6 ms |
7632 KB |
Output is correct |
19 |
Correct |
5 ms |
7636 KB |
Output is correct |
20 |
Correct |
5 ms |
7636 KB |
Output is correct |
21 |
Correct |
4 ms |
7632 KB |
Output is correct |
22 |
Correct |
5 ms |
7764 KB |
Output is correct |
23 |
Correct |
4 ms |
7636 KB |
Output is correct |
24 |
Correct |
5 ms |
7764 KB |
Output is correct |
25 |
Correct |
5 ms |
7892 KB |
Output is correct |
26 |
Correct |
5 ms |
7892 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
5 ms |
7896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7368 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7388 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
5 ms |
7508 KB |
Output is correct |
6 |
Correct |
4 ms |
7632 KB |
Output is correct |
7 |
Correct |
4 ms |
7636 KB |
Output is correct |
8 |
Correct |
4 ms |
7636 KB |
Output is correct |
9 |
Correct |
4 ms |
7652 KB |
Output is correct |
10 |
Correct |
83 ms |
30280 KB |
Output is correct |
11 |
Correct |
104 ms |
35488 KB |
Output is correct |
12 |
Correct |
105 ms |
34984 KB |
Output is correct |
13 |
Correct |
142 ms |
36548 KB |
Output is correct |
14 |
Correct |
110 ms |
38212 KB |
Output is correct |
15 |
Correct |
4 ms |
7636 KB |
Output is correct |
16 |
Correct |
4 ms |
7636 KB |
Output is correct |
17 |
Correct |
5 ms |
7628 KB |
Output is correct |
18 |
Correct |
5 ms |
7632 KB |
Output is correct |
19 |
Correct |
4 ms |
7636 KB |
Output is correct |
20 |
Correct |
4 ms |
7652 KB |
Output is correct |
21 |
Correct |
5 ms |
7636 KB |
Output is correct |
22 |
Correct |
5 ms |
7636 KB |
Output is correct |
23 |
Correct |
6 ms |
7632 KB |
Output is correct |
24 |
Correct |
5 ms |
7636 KB |
Output is correct |
25 |
Correct |
5 ms |
7636 KB |
Output is correct |
26 |
Correct |
4 ms |
7632 KB |
Output is correct |
27 |
Correct |
5 ms |
7764 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
5 ms |
7764 KB |
Output is correct |
30 |
Correct |
5 ms |
7892 KB |
Output is correct |
31 |
Correct |
5 ms |
7892 KB |
Output is correct |
32 |
Correct |
4 ms |
7636 KB |
Output is correct |
33 |
Correct |
5 ms |
7896 KB |
Output is correct |
34 |
Correct |
14 ms |
11388 KB |
Output is correct |
35 |
Correct |
109 ms |
36356 KB |
Output is correct |
36 |
Correct |
126 ms |
36352 KB |
Output is correct |
37 |
Correct |
111 ms |
36276 KB |
Output is correct |
38 |
Correct |
103 ms |
35904 KB |
Output is correct |
39 |
Correct |
112 ms |
35780 KB |
Output is correct |
40 |
Correct |
110 ms |
33464 KB |
Output is correct |
41 |
Correct |
115 ms |
36656 KB |
Output is correct |
42 |
Correct |
109 ms |
36300 KB |
Output is correct |
43 |
Correct |
96 ms |
38316 KB |
Output is correct |
44 |
Correct |
115 ms |
36680 KB |
Output is correct |
45 |
Correct |
153 ms |
45996 KB |
Output is correct |
46 |
Correct |
109 ms |
36304 KB |
Output is correct |
47 |
Correct |
105 ms |
36312 KB |
Output is correct |
48 |
Correct |
122 ms |
39364 KB |
Output is correct |
49 |
Correct |
116 ms |
41584 KB |
Output is correct |
50 |
Correct |
85 ms |
34252 KB |
Output is correct |
51 |
Correct |
129 ms |
40732 KB |
Output is correct |
52 |
Correct |
162 ms |
49988 KB |
Output is correct |
53 |
Correct |
217 ms |
53308 KB |
Output is correct |
54 |
Correct |
194 ms |
58228 KB |
Output is correct |
55 |
Correct |
99 ms |
38084 KB |
Output is correct |
56 |
Correct |
173 ms |
53292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7388 KB |
Output is correct |
3 |
Correct |
5 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7632 KB |
Output is correct |
6 |
Correct |
4 ms |
7636 KB |
Output is correct |
7 |
Correct |
4 ms |
7636 KB |
Output is correct |
8 |
Correct |
4 ms |
7652 KB |
Output is correct |
9 |
Correct |
83 ms |
30280 KB |
Output is correct |
10 |
Correct |
104 ms |
35488 KB |
Output is correct |
11 |
Correct |
105 ms |
34984 KB |
Output is correct |
12 |
Correct |
142 ms |
36548 KB |
Output is correct |
13 |
Correct |
110 ms |
38212 KB |
Output is correct |
14 |
Correct |
93 ms |
30680 KB |
Output is correct |
15 |
Correct |
212 ms |
39464 KB |
Output is correct |
16 |
Correct |
202 ms |
38676 KB |
Output is correct |
17 |
Correct |
193 ms |
40428 KB |
Output is correct |
18 |
Correct |
189 ms |
41936 KB |
Output is correct |
19 |
Correct |
321 ms |
41388 KB |
Output is correct |
20 |
Correct |
288 ms |
42816 KB |
Output is correct |
21 |
Correct |
309 ms |
42168 KB |
Output is correct |
22 |
Correct |
295 ms |
42688 KB |
Output is correct |
23 |
Correct |
284 ms |
42636 KB |
Output is correct |
24 |
Correct |
318 ms |
41288 KB |
Output is correct |
25 |
Correct |
281 ms |
42252 KB |
Output is correct |
26 |
Correct |
305 ms |
40956 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
5 ms |
7628 KB |
Output is correct |
30 |
Correct |
5 ms |
7632 KB |
Output is correct |
31 |
Correct |
4 ms |
7636 KB |
Output is correct |
32 |
Correct |
4 ms |
7652 KB |
Output is correct |
33 |
Correct |
5 ms |
7636 KB |
Output is correct |
34 |
Correct |
5 ms |
7636 KB |
Output is correct |
35 |
Correct |
6 ms |
7632 KB |
Output is correct |
36 |
Correct |
14 ms |
11388 KB |
Output is correct |
37 |
Correct |
109 ms |
36356 KB |
Output is correct |
38 |
Correct |
126 ms |
36352 KB |
Output is correct |
39 |
Correct |
111 ms |
36276 KB |
Output is correct |
40 |
Correct |
103 ms |
35904 KB |
Output is correct |
41 |
Correct |
112 ms |
35780 KB |
Output is correct |
42 |
Correct |
110 ms |
33464 KB |
Output is correct |
43 |
Correct |
115 ms |
36656 KB |
Output is correct |
44 |
Correct |
109 ms |
36300 KB |
Output is correct |
45 |
Correct |
96 ms |
38316 KB |
Output is correct |
46 |
Correct |
115 ms |
36680 KB |
Output is correct |
47 |
Correct |
23 ms |
11432 KB |
Output is correct |
48 |
Correct |
218 ms |
40140 KB |
Output is correct |
49 |
Correct |
218 ms |
40236 KB |
Output is correct |
50 |
Correct |
234 ms |
40144 KB |
Output is correct |
51 |
Correct |
215 ms |
39908 KB |
Output is correct |
52 |
Correct |
217 ms |
38304 KB |
Output is correct |
53 |
Correct |
170 ms |
31820 KB |
Output is correct |
54 |
Correct |
206 ms |
40756 KB |
Output is correct |
55 |
Correct |
219 ms |
40296 KB |
Output is correct |
56 |
Correct |
260 ms |
41712 KB |
Output is correct |
57 |
Correct |
233 ms |
41020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7368 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7388 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
5 ms |
7508 KB |
Output is correct |
6 |
Correct |
4 ms |
7632 KB |
Output is correct |
7 |
Correct |
4 ms |
7636 KB |
Output is correct |
8 |
Correct |
4 ms |
7636 KB |
Output is correct |
9 |
Correct |
4 ms |
7652 KB |
Output is correct |
10 |
Correct |
83 ms |
30280 KB |
Output is correct |
11 |
Correct |
104 ms |
35488 KB |
Output is correct |
12 |
Correct |
105 ms |
34984 KB |
Output is correct |
13 |
Correct |
142 ms |
36548 KB |
Output is correct |
14 |
Correct |
110 ms |
38212 KB |
Output is correct |
15 |
Correct |
93 ms |
30680 KB |
Output is correct |
16 |
Correct |
212 ms |
39464 KB |
Output is correct |
17 |
Correct |
202 ms |
38676 KB |
Output is correct |
18 |
Correct |
193 ms |
40428 KB |
Output is correct |
19 |
Correct |
189 ms |
41936 KB |
Output is correct |
20 |
Correct |
321 ms |
41388 KB |
Output is correct |
21 |
Correct |
288 ms |
42816 KB |
Output is correct |
22 |
Correct |
309 ms |
42168 KB |
Output is correct |
23 |
Correct |
295 ms |
42688 KB |
Output is correct |
24 |
Correct |
284 ms |
42636 KB |
Output is correct |
25 |
Correct |
318 ms |
41288 KB |
Output is correct |
26 |
Correct |
281 ms |
42252 KB |
Output is correct |
27 |
Correct |
305 ms |
40956 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
4 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7628 KB |
Output is correct |
31 |
Correct |
5 ms |
7632 KB |
Output is correct |
32 |
Correct |
4 ms |
7636 KB |
Output is correct |
33 |
Correct |
4 ms |
7652 KB |
Output is correct |
34 |
Correct |
5 ms |
7636 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
6 ms |
7632 KB |
Output is correct |
37 |
Correct |
14 ms |
11388 KB |
Output is correct |
38 |
Correct |
109 ms |
36356 KB |
Output is correct |
39 |
Correct |
126 ms |
36352 KB |
Output is correct |
40 |
Correct |
111 ms |
36276 KB |
Output is correct |
41 |
Correct |
103 ms |
35904 KB |
Output is correct |
42 |
Correct |
112 ms |
35780 KB |
Output is correct |
43 |
Correct |
110 ms |
33464 KB |
Output is correct |
44 |
Correct |
115 ms |
36656 KB |
Output is correct |
45 |
Correct |
109 ms |
36300 KB |
Output is correct |
46 |
Correct |
96 ms |
38316 KB |
Output is correct |
47 |
Correct |
115 ms |
36680 KB |
Output is correct |
48 |
Correct |
23 ms |
11432 KB |
Output is correct |
49 |
Correct |
218 ms |
40140 KB |
Output is correct |
50 |
Correct |
218 ms |
40236 KB |
Output is correct |
51 |
Correct |
234 ms |
40144 KB |
Output is correct |
52 |
Correct |
215 ms |
39908 KB |
Output is correct |
53 |
Correct |
217 ms |
38304 KB |
Output is correct |
54 |
Correct |
170 ms |
31820 KB |
Output is correct |
55 |
Correct |
206 ms |
40756 KB |
Output is correct |
56 |
Correct |
219 ms |
40296 KB |
Output is correct |
57 |
Correct |
260 ms |
41712 KB |
Output is correct |
58 |
Correct |
233 ms |
41020 KB |
Output is correct |
59 |
Correct |
103 ms |
17056 KB |
Output is correct |
60 |
Correct |
200 ms |
39676 KB |
Output is correct |
61 |
Correct |
197 ms |
38956 KB |
Output is correct |
62 |
Correct |
228 ms |
40780 KB |
Output is correct |
63 |
Correct |
225 ms |
42304 KB |
Output is correct |
64 |
Correct |
5 ms |
7636 KB |
Output is correct |
65 |
Correct |
5 ms |
7636 KB |
Output is correct |
66 |
Correct |
4 ms |
7632 KB |
Output is correct |
67 |
Correct |
5 ms |
7764 KB |
Output is correct |
68 |
Correct |
4 ms |
7636 KB |
Output is correct |
69 |
Correct |
5 ms |
7764 KB |
Output is correct |
70 |
Correct |
5 ms |
7892 KB |
Output is correct |
71 |
Correct |
5 ms |
7892 KB |
Output is correct |
72 |
Correct |
4 ms |
7636 KB |
Output is correct |
73 |
Correct |
5 ms |
7896 KB |
Output is correct |
74 |
Correct |
153 ms |
45996 KB |
Output is correct |
75 |
Correct |
109 ms |
36304 KB |
Output is correct |
76 |
Correct |
105 ms |
36312 KB |
Output is correct |
77 |
Correct |
122 ms |
39364 KB |
Output is correct |
78 |
Correct |
116 ms |
41584 KB |
Output is correct |
79 |
Correct |
85 ms |
34252 KB |
Output is correct |
80 |
Correct |
129 ms |
40732 KB |
Output is correct |
81 |
Correct |
162 ms |
49988 KB |
Output is correct |
82 |
Correct |
217 ms |
53308 KB |
Output is correct |
83 |
Correct |
194 ms |
58228 KB |
Output is correct |
84 |
Correct |
99 ms |
38084 KB |
Output is correct |
85 |
Correct |
173 ms |
53292 KB |
Output is correct |
86 |
Correct |
77 ms |
21884 KB |
Output is correct |
87 |
Correct |
211 ms |
40328 KB |
Output is correct |
88 |
Correct |
245 ms |
40172 KB |
Output is correct |
89 |
Correct |
297 ms |
41612 KB |
Output is correct |
90 |
Correct |
269 ms |
46928 KB |
Output is correct |
91 |
Correct |
252 ms |
44308 KB |
Output is correct |
92 |
Correct |
302 ms |
43216 KB |
Output is correct |
93 |
Correct |
303 ms |
53236 KB |
Output is correct |
94 |
Correct |
387 ms |
56912 KB |
Output is correct |
95 |
Correct |
294 ms |
62080 KB |
Output is correct |
96 |
Correct |
278 ms |
41832 KB |
Output is correct |
97 |
Correct |
337 ms |
48992 KB |
Output is correct |