#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
struct Edge {
int u, v, w;
} edge[2 * MAXN];
ii minw[3];
vector<ii> adj[MAXN];
int pa[MAXN], wEdge[MAXN], cntLeaf[MAXN], maxw, numNode, numEdge;
bool dp[1003][1003], dx[MAXN], check_sub1, check_sub2;
void init(int _N, int _M, vector<int> _U, vector<int> _V, vector<int> _W) {
numNode = _N, numEdge = _M;
minw[0] = minw[1] = minw[2] = {1e9+7, -1};
check_sub2 = (numEdge + 1 == numNode);
for (int i = 0; i < numEdge; ++i) {
edge[i] = {_U[i], _V[i], _W[i]};
adj[edge[i].u].push_back(ii(edge[i].v, edge[i].w));
adj[edge[i].v].push_back(ii(edge[i].u, edge[i].w));
check_sub2 &= (edge[i].u == 0);
maxw = max(maxw, edge[i].w);
wEdge[edge[i].v] = edge[i].w;
if(minw[0].fi > edge[i].w) {
minw[2] = minw[1], minw[1] = minw[0];
minw[0] = {edge[i].w, edge[i].v};
} else
if(minw[1].fi > edge[i].w) {
minw[2] = minw[1];
minw[1] = {edge[i].w, edge[i].v};
} else
if(minw[2].fi > edge[i].w) {
minw[2] = {edge[i].w, edge[i].v};
}
}
check_sub1 = 1;
for (int i = 0; i < numNode; ++i)
check_sub1 &= (sz(adj[i]) <= 2);
sort(edge, edge + numEdge, [](const Edge &a, const Edge &b) {
return (a.w < b.w);
});
}
int sub2(int x, int y) {
if(numNode <= 3)
return -1;
int res = max(wEdge[x], wEdge[y]), cnt(1 + (x == 0));
for (int k = 0; k < 3; ++k) {
if(minw[k].se != x && minw[k].se != y) {
if(--cnt == 0) {
res = max(res, minw[k].fi);
break;
}
}
}
return res;
}
int sub3(int x, int y) {
int l(0), r(numEdge - 1), ans(-1);
while(l <= r) {
int mid = (l + r) >> 1;
for (int i = 0; i < numNode; ++i) {
for (int j = 0; j < numNode; ++j)
dp[i][j] = 0;
}
queue<ii> qu;
qu.push(ii(x, y));
dp[x][y] = 1;
int xt(x), yt(y);
bool check(0);
while(sz(qu)) {
int x(qu.front().fi), y(qu.front().se);
qu.pop();
if(x == yt && y == xt) {
check = 1;
break;
}
for (int it = 0; it < sz(adj[x]); ++it) {
int w(adj[x][it].fi), c(adj[x][it].se);
if(c <= edge[mid].w && !dp[w][y] && w != y) {
dp[w][y] = 1;
qu.push(ii(w, y));
}
}
for (int it = 0; it < sz(adj[y]); ++it) {
int w(adj[y][it].fi), c(adj[y][it].se);
if(c <= edge[mid].w && !dp[x][w] && x != w) {
dp[x][w] = 1;
qu.push(ii(x, w));
}
}
}
if(check) {
ans = edge[mid].w;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
ii dfs(int u) {
dx[u] = 1;
cntLeaf[u] = 0;
ii res = {sz(adj[u]), 1};
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it].fi);
if(!dx[v]) {
pa[v] = u;
ii tmp = dfs(v);
res = {res.fi + tmp.fi, res.se + tmp.se};
cntLeaf[u] += cntLeaf[v];
}
}
if(cntLeaf[u] == 0)
cntLeaf[u] = 1;
return res;
}
int sub4(int x, int y) {
int l(0), r(numEdge - 1), ans(-1);
while(l <= r) {
int mid = (l + r) >> 1;
for (int i = 0; i < numNode; ++i) {
adj[i].clear();
dx[i] = 0;
}
for (int i = 0; i <= mid; ++i) {
int u(edge[i].u), v(edge[i].v);
adj[u].push_back(ii(v, 0));
adj[v].push_back(ii(u, 0));
}
pa[x] = -1;
ii res = dfs(x);
bool check(0);
if(dx[y]) {
int u(y);
while(pa[u] != x)
u = pa[u];
check = (res.fi / 2 >= res.se || cntLeaf[y] > 1 || cntLeaf[x] - cntLeaf[u] > 1 || cntLeaf[u] - cntLeaf[y] > 0);
//cout << edge[mid].w << ' ' << res.fi << ' ' << res.se << ' ' << cntLeaf[x] << ' ' << cntLeaf[y] << ' ' << cntLeaf[u] << '\n';
}
if(check) {
ans = edge[mid].w;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
int getMinimumFuelCapacity(int x, int y) {
if(check_sub1)
return (numNode == numEdge) ? maxw : -1;
if(check_sub2)
return sub2(x, y);
if(numNode <= 1000)
return sub3(x, y);
return sub4(x, y);
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "swap"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _U, _V, _W;
int _N, _M, _Q;
cin >> _N >> _M >> _Q;
_U.resize(_M), _V.resize(_M), _W.resize(_M);
for (int i = 0; i < _M; ++i) {
cin >> _U[i] >> _V[i] >> _W[i];
}
init(_N, _M, _U, _V, _W);
for (int t = 0; t < _Q; ++t) {
int _X, _Y;
cin >> _X >> _Y;
cout << "MINIMUM FUEL CAPACITY " << _X << " TO " << _Y << ": " << getMinimumFuelCapacity(_X, _Y) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6584 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
31 ms |
10920 KB |
Output is correct |
10 |
Correct |
40 ms |
11736 KB |
Output is correct |
11 |
Correct |
37 ms |
11612 KB |
Output is correct |
12 |
Correct |
40 ms |
12080 KB |
Output is correct |
13 |
Correct |
38 ms |
11860 KB |
Output is correct |
14 |
Correct |
37 ms |
10836 KB |
Output is correct |
15 |
Correct |
78 ms |
13532 KB |
Output is correct |
16 |
Correct |
78 ms |
13516 KB |
Output is correct |
17 |
Correct |
93 ms |
14008 KB |
Output is correct |
18 |
Correct |
90 ms |
13756 KB |
Output is correct |
19 |
Correct |
41 ms |
10860 KB |
Output is correct |
20 |
Correct |
78 ms |
14804 KB |
Output is correct |
21 |
Correct |
87 ms |
15116 KB |
Output is correct |
22 |
Correct |
86 ms |
15080 KB |
Output is correct |
23 |
Correct |
83 ms |
15068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
76 ms |
15468 KB |
Output is correct |
4 |
Correct |
76 ms |
15660 KB |
Output is correct |
5 |
Correct |
79 ms |
15620 KB |
Output is correct |
6 |
Correct |
77 ms |
15552 KB |
Output is correct |
7 |
Correct |
79 ms |
15652 KB |
Output is correct |
8 |
Correct |
77 ms |
15432 KB |
Output is correct |
9 |
Correct |
76 ms |
15672 KB |
Output is correct |
10 |
Correct |
75 ms |
15656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6584 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
132 ms |
6744 KB |
Output is correct |
11 |
Correct |
127 ms |
6716 KB |
Output is correct |
12 |
Correct |
116 ms |
6736 KB |
Output is correct |
13 |
Correct |
106 ms |
6488 KB |
Output is correct |
14 |
Correct |
178 ms |
7052 KB |
Output is correct |
15 |
Correct |
55 ms |
6492 KB |
Output is correct |
16 |
Correct |
139 ms |
6492 KB |
Output is correct |
17 |
Correct |
157 ms |
6712 KB |
Output is correct |
18 |
Correct |
218 ms |
7000 KB |
Output is correct |
19 |
Correct |
211 ms |
7104 KB |
Output is correct |
20 |
Correct |
178 ms |
6492 KB |
Output is correct |
21 |
Correct |
113 ms |
7168 KB |
Output is correct |
22 |
Correct |
87 ms |
6952 KB |
Output is correct |
23 |
Correct |
137 ms |
6848 KB |
Output is correct |
24 |
Correct |
466 ms |
8116 KB |
Output is correct |
25 |
Correct |
363 ms |
8028 KB |
Output is correct |
26 |
Correct |
368 ms |
7000 KB |
Output is correct |
27 |
Correct |
100 ms |
6492 KB |
Output is correct |
28 |
Correct |
582 ms |
7952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6584 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
31 ms |
10920 KB |
Output is correct |
11 |
Correct |
40 ms |
11736 KB |
Output is correct |
12 |
Correct |
37 ms |
11612 KB |
Output is correct |
13 |
Correct |
40 ms |
12080 KB |
Output is correct |
14 |
Correct |
38 ms |
11860 KB |
Output is correct |
15 |
Correct |
132 ms |
6744 KB |
Output is correct |
16 |
Correct |
127 ms |
6716 KB |
Output is correct |
17 |
Correct |
116 ms |
6736 KB |
Output is correct |
18 |
Correct |
106 ms |
6488 KB |
Output is correct |
19 |
Correct |
178 ms |
7052 KB |
Output is correct |
20 |
Correct |
55 ms |
6492 KB |
Output is correct |
21 |
Correct |
139 ms |
6492 KB |
Output is correct |
22 |
Correct |
157 ms |
6712 KB |
Output is correct |
23 |
Correct |
218 ms |
7000 KB |
Output is correct |
24 |
Correct |
211 ms |
7104 KB |
Output is correct |
25 |
Correct |
178 ms |
6492 KB |
Output is correct |
26 |
Correct |
113 ms |
7168 KB |
Output is correct |
27 |
Correct |
87 ms |
6952 KB |
Output is correct |
28 |
Correct |
137 ms |
6848 KB |
Output is correct |
29 |
Correct |
466 ms |
8116 KB |
Output is correct |
30 |
Correct |
363 ms |
8028 KB |
Output is correct |
31 |
Correct |
368 ms |
7000 KB |
Output is correct |
32 |
Correct |
100 ms |
6492 KB |
Output is correct |
33 |
Correct |
582 ms |
7952 KB |
Output is correct |
34 |
Correct |
12 ms |
7256 KB |
Output is correct |
35 |
Correct |
239 ms |
18672 KB |
Output is correct |
36 |
Correct |
222 ms |
15432 KB |
Output is correct |
37 |
Correct |
240 ms |
13844 KB |
Output is correct |
38 |
Correct |
277 ms |
13648 KB |
Output is correct |
39 |
Correct |
225 ms |
13652 KB |
Output is correct |
40 |
Correct |
238 ms |
13148 KB |
Output is correct |
41 |
Correct |
234 ms |
17340 KB |
Output is correct |
42 |
Correct |
235 ms |
17588 KB |
Output is correct |
43 |
Correct |
283 ms |
17844 KB |
Output is correct |
44 |
Correct |
269 ms |
14672 KB |
Output is correct |
45 |
Correct |
362 ms |
18832 KB |
Output is correct |
46 |
Correct |
261 ms |
17852 KB |
Output is correct |
47 |
Correct |
332 ms |
13908 KB |
Output is correct |
48 |
Correct |
361 ms |
14680 KB |
Output is correct |
49 |
Correct |
66 ms |
18120 KB |
Output is correct |
50 |
Correct |
72 ms |
15044 KB |
Output is correct |
51 |
Correct |
238 ms |
17148 KB |
Output is correct |
52 |
Correct |
456 ms |
25088 KB |
Output is correct |
53 |
Correct |
289 ms |
20444 KB |
Output is correct |
54 |
Correct |
338 ms |
25196 KB |
Output is correct |
55 |
Correct |
313 ms |
19900 KB |
Output is correct |
56 |
Correct |
490 ms |
21164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6584 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
31 ms |
10920 KB |
Output is correct |
10 |
Correct |
40 ms |
11736 KB |
Output is correct |
11 |
Correct |
37 ms |
11612 KB |
Output is correct |
12 |
Correct |
40 ms |
12080 KB |
Output is correct |
13 |
Correct |
38 ms |
11860 KB |
Output is correct |
14 |
Correct |
37 ms |
10836 KB |
Output is correct |
15 |
Correct |
78 ms |
13532 KB |
Output is correct |
16 |
Correct |
78 ms |
13516 KB |
Output is correct |
17 |
Correct |
93 ms |
14008 KB |
Output is correct |
18 |
Correct |
90 ms |
13756 KB |
Output is correct |
19 |
Correct |
76 ms |
15468 KB |
Output is correct |
20 |
Correct |
76 ms |
15660 KB |
Output is correct |
21 |
Correct |
79 ms |
15620 KB |
Output is correct |
22 |
Correct |
77 ms |
15552 KB |
Output is correct |
23 |
Correct |
79 ms |
15652 KB |
Output is correct |
24 |
Correct |
77 ms |
15432 KB |
Output is correct |
25 |
Correct |
76 ms |
15672 KB |
Output is correct |
26 |
Correct |
75 ms |
15656 KB |
Output is correct |
27 |
Correct |
132 ms |
6744 KB |
Output is correct |
28 |
Correct |
127 ms |
6716 KB |
Output is correct |
29 |
Correct |
116 ms |
6736 KB |
Output is correct |
30 |
Correct |
106 ms |
6488 KB |
Output is correct |
31 |
Correct |
178 ms |
7052 KB |
Output is correct |
32 |
Correct |
55 ms |
6492 KB |
Output is correct |
33 |
Correct |
139 ms |
6492 KB |
Output is correct |
34 |
Correct |
157 ms |
6712 KB |
Output is correct |
35 |
Correct |
218 ms |
7000 KB |
Output is correct |
36 |
Correct |
12 ms |
7256 KB |
Output is correct |
37 |
Correct |
239 ms |
18672 KB |
Output is correct |
38 |
Correct |
222 ms |
15432 KB |
Output is correct |
39 |
Correct |
240 ms |
13844 KB |
Output is correct |
40 |
Correct |
277 ms |
13648 KB |
Output is correct |
41 |
Correct |
225 ms |
13652 KB |
Output is correct |
42 |
Correct |
238 ms |
13148 KB |
Output is correct |
43 |
Correct |
234 ms |
17340 KB |
Output is correct |
44 |
Correct |
235 ms |
17588 KB |
Output is correct |
45 |
Correct |
283 ms |
17844 KB |
Output is correct |
46 |
Correct |
269 ms |
14672 KB |
Output is correct |
47 |
Execution timed out |
2013 ms |
7768 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6584 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
31 ms |
10920 KB |
Output is correct |
11 |
Correct |
40 ms |
11736 KB |
Output is correct |
12 |
Correct |
37 ms |
11612 KB |
Output is correct |
13 |
Correct |
40 ms |
12080 KB |
Output is correct |
14 |
Correct |
38 ms |
11860 KB |
Output is correct |
15 |
Correct |
37 ms |
10836 KB |
Output is correct |
16 |
Correct |
78 ms |
13532 KB |
Output is correct |
17 |
Correct |
78 ms |
13516 KB |
Output is correct |
18 |
Correct |
93 ms |
14008 KB |
Output is correct |
19 |
Correct |
90 ms |
13756 KB |
Output is correct |
20 |
Correct |
76 ms |
15468 KB |
Output is correct |
21 |
Correct |
76 ms |
15660 KB |
Output is correct |
22 |
Correct |
79 ms |
15620 KB |
Output is correct |
23 |
Correct |
77 ms |
15552 KB |
Output is correct |
24 |
Correct |
79 ms |
15652 KB |
Output is correct |
25 |
Correct |
77 ms |
15432 KB |
Output is correct |
26 |
Correct |
76 ms |
15672 KB |
Output is correct |
27 |
Correct |
75 ms |
15656 KB |
Output is correct |
28 |
Correct |
132 ms |
6744 KB |
Output is correct |
29 |
Correct |
127 ms |
6716 KB |
Output is correct |
30 |
Correct |
116 ms |
6736 KB |
Output is correct |
31 |
Correct |
106 ms |
6488 KB |
Output is correct |
32 |
Correct |
178 ms |
7052 KB |
Output is correct |
33 |
Correct |
55 ms |
6492 KB |
Output is correct |
34 |
Correct |
139 ms |
6492 KB |
Output is correct |
35 |
Correct |
157 ms |
6712 KB |
Output is correct |
36 |
Correct |
218 ms |
7000 KB |
Output is correct |
37 |
Correct |
12 ms |
7256 KB |
Output is correct |
38 |
Correct |
239 ms |
18672 KB |
Output is correct |
39 |
Correct |
222 ms |
15432 KB |
Output is correct |
40 |
Correct |
240 ms |
13844 KB |
Output is correct |
41 |
Correct |
277 ms |
13648 KB |
Output is correct |
42 |
Correct |
225 ms |
13652 KB |
Output is correct |
43 |
Correct |
238 ms |
13148 KB |
Output is correct |
44 |
Correct |
234 ms |
17340 KB |
Output is correct |
45 |
Correct |
235 ms |
17588 KB |
Output is correct |
46 |
Correct |
283 ms |
17844 KB |
Output is correct |
47 |
Correct |
269 ms |
14672 KB |
Output is correct |
48 |
Execution timed out |
2013 ms |
7768 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |