#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> p;
typedef pair<ll, p> tri;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<p> vp;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<vv> vvv;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef vector<vvvp> vvvvp;
#define double long double
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define loop(a) for (ll i = 0; i < a; i++)
#define setmin(a, b) a = min(a, b)
#define setmax(a, b) a = max(a, b)
#define all(v) v.begin(), v.end()
#include "swap.h"
ll res = -1;
vvp graph;
ll maxW = -INF;
ll N;
v weightInflate;
ll getParent(ll i, v &dsuPar, v ×, ll time)
{
if (times[i] > time) return i;
return getParent(dsuPar[i], dsuPar, times, time);
}
v dsuParent, timeBecomeNotRoot, dsuSize, timeValid;
void init(int Nn, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
N = Nn;
for (auto x : W) weightInflate.pb(x);
sort(all(weightInflate));
weightInflate.erase(unique(all(weightInflate)), weightInflate.end());
map<ll, ll> weightShrink;
for (ll i = 0; i < weightInflate.size(); i++) weightShrink[weightInflate[i]] = i;
maxW = weightInflate.size() - 1;
graph = vvp(N);
for (ll i = 0; i < M; i++)
{
graph[U[i]].eb(V[i], weightShrink[W[i]]);
graph[V[i]].eb(U[i], weightShrink[W[i]]);
}
dsuParent = v(N);
timeBecomeNotRoot = v(N, INF);
dsuSize = v(N, 1);
timeValid = v(N, INF);
for (ll i = 0; i < N; i++) dsuParent[i] = i;
vtri edgesByW;
for (ll i = 0; i < M; i++) edgesByW.eb(weightShrink[W[i]], p(U[i], V[i]));
sort(all(edgesByW));
v curDegree(N, 0);
for (auto x : edgesByW)
{
ll l = x.s.f, r = x.s.s;
ll p1 = getParent(l, dsuParent, timeBecomeNotRoot, x.f), p2 = getParent(r, dsuParent, timeBecomeNotRoot, x.f);
if (p1 == p2)
{
timeValid[p1] = min(timeValid[p1], x.f);
continue;
}
if (dsuSize[p1] > dsuSize[p2]) swap(p1, p2);
dsuParent[p1] = p2;
dsuSize[p2] += dsuSize[p1];
timeBecomeNotRoot[p1] = x.f;
curDegree[l]++; curDegree[r]++;
if ((timeValid[p1] < INF || curDegree[l] >= 3 || curDegree[r] >= 3) && timeValid[p2] == INF) timeValid[p2] = x.f;
}
}
int getMinimumFuelCapacity(int X, int Y) {
ll mini = 0, maxi = maxW + 1;
while (mini < maxi)
{
ll mid = (mini + maxi) / 2;
ll p1 = getParent(X, dsuParent, timeBecomeNotRoot, mid), p2 = getParent(Y, dsuParent, timeBecomeNotRoot, mid);
bool poss = (p1 == p2 && timeValid[p1] <= mid);
if (poss)
{
maxi = mid;
}
else
{
mini = mid + 1;
}
}
if (mini > maxW) return -1;
return weightInflate[mini];
}
/*
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;
}
/**/
Compilation message
swap.cpp:142:1: warning: "/*" within comment [-Wcomment]
142 | /**/
|
swap.cpp:27:16: warning: overflow in conversion from 'double' to 'll' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
27 | const ll INF = 1e18;
| ^~~~
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:57:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (ll i = 0; i < weightInflate.size(); i++) weightShrink[weightInflate[i]] = i;
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
92 ms |
13640 KB |
Output is correct |
10 |
Correct |
125 ms |
16144 KB |
Output is correct |
11 |
Correct |
131 ms |
15944 KB |
Output is correct |
12 |
Correct |
127 ms |
16712 KB |
Output is correct |
13 |
Correct |
140 ms |
16708 KB |
Output is correct |
14 |
Correct |
104 ms |
13832 KB |
Output is correct |
15 |
Correct |
254 ms |
17708 KB |
Output is correct |
16 |
Correct |
220 ms |
17336 KB |
Output is correct |
17 |
Correct |
229 ms |
18244 KB |
Output is correct |
18 |
Correct |
216 ms |
18304 KB |
Output is correct |
19 |
Correct |
96 ms |
7512 KB |
Output is correct |
20 |
Correct |
254 ms |
21908 KB |
Output is correct |
21 |
Correct |
239 ms |
21468 KB |
Output is correct |
22 |
Correct |
252 ms |
22840 KB |
Output is correct |
23 |
Correct |
208 ms |
22676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
208 ms |
18512 KB |
Output is correct |
4 |
Correct |
182 ms |
23104 KB |
Output is correct |
5 |
Correct |
187 ms |
22756 KB |
Output is correct |
6 |
Correct |
186 ms |
23132 KB |
Output is correct |
7 |
Correct |
181 ms |
23040 KB |
Output is correct |
8 |
Correct |
184 ms |
22228 KB |
Output is correct |
9 |
Correct |
184 ms |
22728 KB |
Output is correct |
10 |
Correct |
180 ms |
22060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
572 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
704 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
92 ms |
13640 KB |
Output is correct |
11 |
Correct |
125 ms |
16144 KB |
Output is correct |
12 |
Correct |
131 ms |
15944 KB |
Output is correct |
13 |
Correct |
127 ms |
16712 KB |
Output is correct |
14 |
Correct |
140 ms |
16708 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
572 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
704 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
856 KB |
Output is correct |
34 |
Correct |
13 ms |
2640 KB |
Output is correct |
35 |
Correct |
123 ms |
16596 KB |
Output is correct |
36 |
Correct |
142 ms |
16580 KB |
Output is correct |
37 |
Correct |
150 ms |
16440 KB |
Output is correct |
38 |
Correct |
128 ms |
16232 KB |
Output is correct |
39 |
Correct |
120 ms |
16332 KB |
Output is correct |
40 |
Correct |
114 ms |
15356 KB |
Output is correct |
41 |
Correct |
136 ms |
16712 KB |
Output is correct |
42 |
Correct |
118 ms |
16456 KB |
Output is correct |
43 |
Correct |
119 ms |
16380 KB |
Output is correct |
44 |
Correct |
130 ms |
17108 KB |
Output is correct |
45 |
Correct |
187 ms |
22804 KB |
Output is correct |
46 |
Correct |
117 ms |
16448 KB |
Output is correct |
47 |
Correct |
120 ms |
16568 KB |
Output is correct |
48 |
Correct |
138 ms |
17556 KB |
Output is correct |
49 |
Correct |
188 ms |
20932 KB |
Output is correct |
50 |
Correct |
134 ms |
14852 KB |
Output is correct |
51 |
Correct |
172 ms |
18324 KB |
Output is correct |
52 |
Correct |
236 ms |
25284 KB |
Output is correct |
53 |
Correct |
246 ms |
28636 KB |
Output is correct |
54 |
Correct |
306 ms |
30144 KB |
Output is correct |
55 |
Correct |
117 ms |
16456 KB |
Output is correct |
56 |
Correct |
254 ms |
26308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
92 ms |
13640 KB |
Output is correct |
10 |
Correct |
125 ms |
16144 KB |
Output is correct |
11 |
Correct |
131 ms |
15944 KB |
Output is correct |
12 |
Correct |
127 ms |
16712 KB |
Output is correct |
13 |
Correct |
140 ms |
16708 KB |
Output is correct |
14 |
Correct |
104 ms |
13832 KB |
Output is correct |
15 |
Correct |
254 ms |
17708 KB |
Output is correct |
16 |
Correct |
220 ms |
17336 KB |
Output is correct |
17 |
Correct |
229 ms |
18244 KB |
Output is correct |
18 |
Correct |
216 ms |
18304 KB |
Output is correct |
19 |
Correct |
208 ms |
18512 KB |
Output is correct |
20 |
Correct |
182 ms |
23104 KB |
Output is correct |
21 |
Correct |
187 ms |
22756 KB |
Output is correct |
22 |
Correct |
186 ms |
23132 KB |
Output is correct |
23 |
Correct |
181 ms |
23040 KB |
Output is correct |
24 |
Correct |
184 ms |
22228 KB |
Output is correct |
25 |
Correct |
184 ms |
22728 KB |
Output is correct |
26 |
Correct |
180 ms |
22060 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
572 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
13 ms |
2640 KB |
Output is correct |
37 |
Correct |
123 ms |
16596 KB |
Output is correct |
38 |
Correct |
142 ms |
16580 KB |
Output is correct |
39 |
Correct |
150 ms |
16440 KB |
Output is correct |
40 |
Correct |
128 ms |
16232 KB |
Output is correct |
41 |
Correct |
120 ms |
16332 KB |
Output is correct |
42 |
Correct |
114 ms |
15356 KB |
Output is correct |
43 |
Correct |
136 ms |
16712 KB |
Output is correct |
44 |
Correct |
118 ms |
16456 KB |
Output is correct |
45 |
Correct |
119 ms |
16380 KB |
Output is correct |
46 |
Correct |
130 ms |
17108 KB |
Output is correct |
47 |
Correct |
21 ms |
3212 KB |
Output is correct |
48 |
Correct |
229 ms |
22100 KB |
Output is correct |
49 |
Correct |
243 ms |
22104 KB |
Output is correct |
50 |
Correct |
228 ms |
22244 KB |
Output is correct |
51 |
Correct |
222 ms |
22128 KB |
Output is correct |
52 |
Correct |
220 ms |
21240 KB |
Output is correct |
53 |
Correct |
175 ms |
17416 KB |
Output is correct |
54 |
Correct |
236 ms |
22724 KB |
Output is correct |
55 |
Correct |
229 ms |
22260 KB |
Output is correct |
56 |
Correct |
193 ms |
22088 KB |
Output is correct |
57 |
Correct |
244 ms |
23364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
92 ms |
13640 KB |
Output is correct |
11 |
Correct |
125 ms |
16144 KB |
Output is correct |
12 |
Correct |
131 ms |
15944 KB |
Output is correct |
13 |
Correct |
127 ms |
16712 KB |
Output is correct |
14 |
Correct |
140 ms |
16708 KB |
Output is correct |
15 |
Correct |
104 ms |
13832 KB |
Output is correct |
16 |
Correct |
254 ms |
17708 KB |
Output is correct |
17 |
Correct |
220 ms |
17336 KB |
Output is correct |
18 |
Correct |
229 ms |
18244 KB |
Output is correct |
19 |
Correct |
216 ms |
18304 KB |
Output is correct |
20 |
Correct |
208 ms |
18512 KB |
Output is correct |
21 |
Correct |
182 ms |
23104 KB |
Output is correct |
22 |
Correct |
187 ms |
22756 KB |
Output is correct |
23 |
Correct |
186 ms |
23132 KB |
Output is correct |
24 |
Correct |
181 ms |
23040 KB |
Output is correct |
25 |
Correct |
184 ms |
22228 KB |
Output is correct |
26 |
Correct |
184 ms |
22728 KB |
Output is correct |
27 |
Correct |
180 ms |
22060 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
572 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
13 ms |
2640 KB |
Output is correct |
38 |
Correct |
123 ms |
16596 KB |
Output is correct |
39 |
Correct |
142 ms |
16580 KB |
Output is correct |
40 |
Correct |
150 ms |
16440 KB |
Output is correct |
41 |
Correct |
128 ms |
16232 KB |
Output is correct |
42 |
Correct |
120 ms |
16332 KB |
Output is correct |
43 |
Correct |
114 ms |
15356 KB |
Output is correct |
44 |
Correct |
136 ms |
16712 KB |
Output is correct |
45 |
Correct |
118 ms |
16456 KB |
Output is correct |
46 |
Correct |
119 ms |
16380 KB |
Output is correct |
47 |
Correct |
130 ms |
17108 KB |
Output is correct |
48 |
Correct |
21 ms |
3212 KB |
Output is correct |
49 |
Correct |
229 ms |
22100 KB |
Output is correct |
50 |
Correct |
243 ms |
22104 KB |
Output is correct |
51 |
Correct |
228 ms |
22244 KB |
Output is correct |
52 |
Correct |
222 ms |
22128 KB |
Output is correct |
53 |
Correct |
220 ms |
21240 KB |
Output is correct |
54 |
Correct |
175 ms |
17416 KB |
Output is correct |
55 |
Correct |
236 ms |
22724 KB |
Output is correct |
56 |
Correct |
229 ms |
22260 KB |
Output is correct |
57 |
Correct |
193 ms |
22088 KB |
Output is correct |
58 |
Correct |
244 ms |
23364 KB |
Output is correct |
59 |
Correct |
96 ms |
7512 KB |
Output is correct |
60 |
Correct |
254 ms |
21908 KB |
Output is correct |
61 |
Correct |
239 ms |
21468 KB |
Output is correct |
62 |
Correct |
252 ms |
22840 KB |
Output is correct |
63 |
Correct |
208 ms |
22676 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
604 KB |
Output is correct |
66 |
Correct |
1 ms |
604 KB |
Output is correct |
67 |
Correct |
2 ms |
604 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
2 ms |
604 KB |
Output is correct |
70 |
Correct |
2 ms |
704 KB |
Output is correct |
71 |
Correct |
2 ms |
604 KB |
Output is correct |
72 |
Correct |
1 ms |
604 KB |
Output is correct |
73 |
Correct |
2 ms |
856 KB |
Output is correct |
74 |
Correct |
187 ms |
22804 KB |
Output is correct |
75 |
Correct |
117 ms |
16448 KB |
Output is correct |
76 |
Correct |
120 ms |
16568 KB |
Output is correct |
77 |
Correct |
138 ms |
17556 KB |
Output is correct |
78 |
Correct |
188 ms |
20932 KB |
Output is correct |
79 |
Correct |
134 ms |
14852 KB |
Output is correct |
80 |
Correct |
172 ms |
18324 KB |
Output is correct |
81 |
Correct |
236 ms |
25284 KB |
Output is correct |
82 |
Correct |
246 ms |
28636 KB |
Output is correct |
83 |
Correct |
306 ms |
30144 KB |
Output is correct |
84 |
Correct |
117 ms |
16456 KB |
Output is correct |
85 |
Correct |
254 ms |
26308 KB |
Output is correct |
86 |
Correct |
78 ms |
9904 KB |
Output is correct |
87 |
Correct |
229 ms |
22284 KB |
Output is correct |
88 |
Correct |
231 ms |
22084 KB |
Output is correct |
89 |
Correct |
219 ms |
22520 KB |
Output is correct |
90 |
Correct |
298 ms |
27164 KB |
Output is correct |
91 |
Correct |
257 ms |
25984 KB |
Output is correct |
92 |
Correct |
248 ms |
24020 KB |
Output is correct |
93 |
Correct |
348 ms |
33364 KB |
Output is correct |
94 |
Correct |
401 ms |
34352 KB |
Output is correct |
95 |
Correct |
444 ms |
36796 KB |
Output is correct |
96 |
Correct |
196 ms |
22632 KB |
Output is correct |
97 |
Correct |
267 ms |
29780 KB |
Output is correct |