#include "swap.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1.5e9;
struct segtree{
int k;
vector<pair<int, int>> mn;
segtree(int n){
k = 1;
while(k <= n) k*=2; k*=2;
mn.assign(k, {inf,0});
for(int i = 0; i < k/2; i++) mn[i+k/2].sc = i;
for(int i = k/2; i > 0; i--) mn[i].sc = mn[2*i].sc;
}
void upd(int in, pair<int, int> x){
in+=k/2;
mn[in] = x, in/=2;
while(in > 0){
mn[in] = min(mn[2*in], mn[2*in+1]);
in/=2;
}
}
pair<int, int> smn(int l, int r, int nd, int a, int b){
if(b < l || a > r) return {inf, inf};
if(a >= l && b <= r) return mn[nd];
int c = (a+b)/2;
return min(smn(l, r, 2*nd, a, c), smn(l, r, 2*nd+1, c+1, b));
}
pair<int, int> smn(int l, int r){
return smn(l, r, 1, 0, k/2-1);
}
}seg(10);
struct reachability_tree{
int n;
vector<int> p;
vector<int> adj;
vector<bool> ok;
vector<vector<int>> v;
vector<int> w;
reachability_tree(int n){
reachability_tree::n = n;
v.resize(n);
p.resize(n);
ok.resize(n, 0);
adj.resize(n, 0);
w.resize(n);
for(int i = 0; i < n; i++) p[i] = i;
}
int get(int x){
if(p[x] != x) return p[x] = get(p[x]);
return p[x];
}
void unite(int a, int b, int vl){
int aa = a, bb = b;
a = get(a), b = get(b);
adj[aa]++;
adj[bb]++;
p.pb(n);
ok.pb(0);
v.pb({});
p[a] = n;
p[b] = n;
v[n].pb(a);
if(a != b) v[n].pb(b);
ok[n] = ok[a]|ok[b];
if(max(adj[aa], adj[bb]) >= 3 || a == b) ok[n] = 1;
n++;
w.pb(vl);
}
}rt(0);
int n, m;
vector<int> depth, ett, pos, anc;
void dfs(int nd, int d, int ls){
depth[nd] = d;
ett.pb(nd);
if(rt.ok[nd]) ls = nd;
anc[nd] = ls;
for(int x: rt.v[nd]){
dfs(x, d+1, ls);
ett.pb(nd);
}
}
void init(int n, int m, vector<int> u, vector<int>v, vector<int>w){
::n = n, ::m = m;
rt = reachability_tree(n);
vector<tuple<int, int, int>> edges;
for(int i = 0; i < m; i++){
edges.pb({w[i], u[i], v[i]});
}
sort(edges.begin(), edges.end());
for(auto [wi, ui, vi]: edges) rt.unite(ui, vi, wi);
depth.resize(rt.n);
pos.resize(rt.n);
anc.resize(rt.n);
dfs(rt.get(0), 1, -1);
seg = segtree(ett.size());
for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
for(int i = 0; i < rt.n; i++) pos[ett[i]] = i;
}
int getMinimumFuelCapacity(int x, int y){
x = pos[x], y = pos[y];
if(x > y) swap(x, y);
int lca = seg.smn(x, y).sc;
int ans = anc[lca];
if(ans != -1) ans = rt.w[ans];
return ans;
}
Compilation message
swap.cpp: In constructor 'segtree::segtree(int)':
swap.cpp:17:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
17 | while(k <= n) k*=2; k*=2;
| ^~~~~
swap.cpp:17:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
17 | while(k <= n) k*=2; k*=2;
| ^
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
101 ms |
24488 KB |
Output is correct |
10 |
Correct |
130 ms |
27516 KB |
Output is correct |
11 |
Correct |
124 ms |
27408 KB |
Output is correct |
12 |
Correct |
126 ms |
28236 KB |
Output is correct |
13 |
Correct |
116 ms |
31304 KB |
Output is correct |
14 |
Correct |
112 ms |
24592 KB |
Output is correct |
15 |
Correct |
257 ms |
29204 KB |
Output is correct |
16 |
Correct |
255 ms |
28792 KB |
Output is correct |
17 |
Correct |
254 ms |
29768 KB |
Output is correct |
18 |
Correct |
245 ms |
32948 KB |
Output is correct |
19 |
Correct |
106 ms |
7952 KB |
Output is correct |
20 |
Correct |
248 ms |
29388 KB |
Output is correct |
21 |
Correct |
247 ms |
28632 KB |
Output is correct |
22 |
Correct |
262 ms |
29824 KB |
Output is correct |
23 |
Correct |
253 ms |
33072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
221 ms |
34756 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
101 ms |
24488 KB |
Output is correct |
11 |
Correct |
130 ms |
27516 KB |
Output is correct |
12 |
Correct |
124 ms |
27408 KB |
Output is correct |
13 |
Correct |
126 ms |
28236 KB |
Output is correct |
14 |
Correct |
116 ms |
31304 KB |
Output is correct |
15 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
101 ms |
24488 KB |
Output is correct |
10 |
Correct |
130 ms |
27516 KB |
Output is correct |
11 |
Correct |
124 ms |
27408 KB |
Output is correct |
12 |
Correct |
126 ms |
28236 KB |
Output is correct |
13 |
Correct |
116 ms |
31304 KB |
Output is correct |
14 |
Correct |
112 ms |
24592 KB |
Output is correct |
15 |
Correct |
257 ms |
29204 KB |
Output is correct |
16 |
Correct |
255 ms |
28792 KB |
Output is correct |
17 |
Correct |
254 ms |
29768 KB |
Output is correct |
18 |
Correct |
245 ms |
32948 KB |
Output is correct |
19 |
Incorrect |
221 ms |
34756 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
101 ms |
24488 KB |
Output is correct |
11 |
Correct |
130 ms |
27516 KB |
Output is correct |
12 |
Correct |
124 ms |
27408 KB |
Output is correct |
13 |
Correct |
126 ms |
28236 KB |
Output is correct |
14 |
Correct |
116 ms |
31304 KB |
Output is correct |
15 |
Correct |
112 ms |
24592 KB |
Output is correct |
16 |
Correct |
257 ms |
29204 KB |
Output is correct |
17 |
Correct |
255 ms |
28792 KB |
Output is correct |
18 |
Correct |
254 ms |
29768 KB |
Output is correct |
19 |
Correct |
245 ms |
32948 KB |
Output is correct |
20 |
Incorrect |
221 ms |
34756 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |