#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,pg[100001],r[100001],lp[100001],LCA;
vector<pii> adj[100001],Adj[100001];
vector<int> add[100001], del[100001];
bool comp(pii a, pii b) {return a.second<b.second;}
struct Edge{
int u,v,w;
bool operator < (const Edge &o) {return w<o.w;}
};
vector<Edge> edge,bridge;
int find(int x) {
while (pg[x]!=x) x=pg[x];
return x;
}
void unite(int X, int Y, int w) {
int x=find(X), y=find(Y);
if (x!=y) {
adj[X].push_back(pii(Y,w));
adj[Y].push_back(pii(X,w));
if (r[x]<r[y]) swap(x,y);
pg[y]=x;
if (r[x]==r[y]) ++r[x];
} else {
bridge.push_back({X,Y,w});
}
}
int p[100001][18],level[100001],mmax[100001][18],mmin[100001];
void dfs1(int x, int par) {
level[x]=level[par]+1;
p[x][0]=par;
for (int i=1; i<18; ++i) {
p[x][i]=p[p[x][i-1]][i-1];
mmax[x][i]=max(mmax[x][i-1],mmax[p[x][i-1]][i-1]);
}
for (auto s : adj[x]) if (s.first!=par) {
mmax[s.first][0]=s.second;
dfs1(s.first,x);
}
}
int lca(int X, int Y, int mode) {
int ma=-INT_MAX;
if (level[X]<level[Y]) swap(X,Y);
int x=X, y=Y;
int dif=level[x]-level[y];
for (int i=0; i<18; ++i) {
if (dif&(1<<i)) {
ma=max(ma,mmax[x][i]);
x=p[x][i];
}
}
if (x!=y) {
for (int i=17; i>=0; --i) {
if (p[x][i]!=p[y][i]) {
ma=max({ma,mmax[x][i],mmax[y][i]});
x=p[x][i];
y=p[y][i];
}
}
ma=max({ma,mmax[x][0],mmax[y][0]});
LCA=p[x][0];
} else {
LCA=Y;
}
int mi=min({lp[X],lp[Y],mmin[X],mmin[Y]});
if (mode==0) return ma;
else return max(ma,mi);
}
multiset<int> temp[100001];
void dfs2(int x, int par) {
for (auto s : add[x]) temp[x].insert(s);
for (auto s : adj[x]) {
if (s.first==par) continue;
dfs2(s.first,x);
if (temp[s.first].size()>temp[x].size()) swap(temp[s.first],temp[x]);
for (auto h : temp[s.first]) temp[x].insert(h);
}
if (temp[x].size()) lp[x]=*(temp[x].begin());
else lp[x]=INT_MAX;
for (auto s : del[x]) temp[x].erase(temp[x].find(s)), temp[x].erase(temp[x].find(s));
}
void dfs3(int x, int par) {
if (Adj[x].size()<3) mmin[x]=INT_MAX;
else mmin[x]=Adj[x][2].second;
for (auto s : adj[x]) {
if (s.first==par) continue;
dfs3(s.first,x);
mmin[x]=min(mmin[x],max(s.second,mmin[s.first]));
}
}
void dfs4(int x, int par) {
for (auto s : adj[x]) {
if (s.first==par) continue;
mmin[s.first]=min(mmin[s.first],max(s.second,mmin[x]));
dfs4(s.first,x);
}
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n=N;
for (int i=0; i<M; ++i) {
edge.push_back({U[i],V[i],W[i]});
Adj[U[i]].push_back(pii(V[i],W[i]));
Adj[V[i]].push_back(pii(U[i],W[i]));
}
sort(edge.begin(),edge.end());
for (int i=0; i<n; ++i) pg[i]=i;
for (auto s : edge) unite(s.u,s.v,s.w);
for (int i=0; i<n; ++i) sort(Adj[i].begin(),Adj[i].end(),comp);
//find mmax
mmax[0][0]=INT_MAX;
dfs1(0,0);
//find loop
for (auto s : bridge) {
int ma=max(lca(s.u,s.v,0),s.w);
add[s.u].push_back(ma);
add[s.v].push_back(ma);
del[LCA].push_back(ma);
}
dfs2(0,0);
//find mmin
dfs3(0,0);
dfs4(0,0);
}
int getMinimumFuelCapacity(int X, int Y) {
int mx=lca(X,Y,1);
if (mx==INT_MAX) return -1;
return mx;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14560 KB |
Output is correct |
5 |
Correct |
9 ms |
14676 KB |
Output is correct |
6 |
Correct |
10 ms |
14712 KB |
Output is correct |
7 |
Correct |
9 ms |
14676 KB |
Output is correct |
8 |
Correct |
10 ms |
14804 KB |
Output is correct |
9 |
Correct |
153 ms |
43064 KB |
Output is correct |
10 |
Correct |
188 ms |
51876 KB |
Output is correct |
11 |
Correct |
201 ms |
50300 KB |
Output is correct |
12 |
Correct |
208 ms |
52948 KB |
Output is correct |
13 |
Correct |
185 ms |
55712 KB |
Output is correct |
14 |
Correct |
216 ms |
42116 KB |
Output is correct |
15 |
Correct |
350 ms |
55652 KB |
Output is correct |
16 |
Correct |
421 ms |
51920 KB |
Output is correct |
17 |
Correct |
398 ms |
60152 KB |
Output is correct |
18 |
Correct |
388 ms |
57400 KB |
Output is correct |
19 |
Correct |
97 ms |
26132 KB |
Output is correct |
20 |
Correct |
351 ms |
55488 KB |
Output is correct |
21 |
Correct |
366 ms |
53016 KB |
Output is correct |
22 |
Correct |
430 ms |
57460 KB |
Output is correct |
23 |
Correct |
353 ms |
57984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
202 ms |
46740 KB |
Output is correct |
4 |
Correct |
194 ms |
48044 KB |
Output is correct |
5 |
Correct |
197 ms |
47472 KB |
Output is correct |
6 |
Correct |
208 ms |
47844 KB |
Output is correct |
7 |
Correct |
196 ms |
47840 KB |
Output is correct |
8 |
Correct |
207 ms |
46640 KB |
Output is correct |
9 |
Correct |
198 ms |
47444 KB |
Output is correct |
10 |
Correct |
200 ms |
46408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14560 KB |
Output is correct |
5 |
Correct |
9 ms |
14676 KB |
Output is correct |
6 |
Correct |
10 ms |
14712 KB |
Output is correct |
7 |
Correct |
9 ms |
14676 KB |
Output is correct |
8 |
Correct |
10 ms |
14804 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14672 KB |
Output is correct |
11 |
Correct |
10 ms |
14776 KB |
Output is correct |
12 |
Correct |
9 ms |
14676 KB |
Output is correct |
13 |
Correct |
11 ms |
14548 KB |
Output is correct |
14 |
Correct |
9 ms |
14676 KB |
Output is correct |
15 |
Correct |
9 ms |
14680 KB |
Output is correct |
16 |
Correct |
9 ms |
14676 KB |
Output is correct |
17 |
Correct |
9 ms |
14804 KB |
Output is correct |
18 |
Correct |
9 ms |
14676 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
20 |
Correct |
8 ms |
14672 KB |
Output is correct |
21 |
Correct |
8 ms |
14676 KB |
Output is correct |
22 |
Correct |
10 ms |
14932 KB |
Output is correct |
23 |
Correct |
9 ms |
14548 KB |
Output is correct |
24 |
Correct |
10 ms |
14932 KB |
Output is correct |
25 |
Correct |
10 ms |
15060 KB |
Output is correct |
26 |
Correct |
10 ms |
15060 KB |
Output is correct |
27 |
Correct |
8 ms |
14804 KB |
Output is correct |
28 |
Correct |
10 ms |
15072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
9 ms |
14560 KB |
Output is correct |
6 |
Correct |
9 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14712 KB |
Output is correct |
8 |
Correct |
9 ms |
14676 KB |
Output is correct |
9 |
Correct |
10 ms |
14804 KB |
Output is correct |
10 |
Correct |
153 ms |
43064 KB |
Output is correct |
11 |
Correct |
188 ms |
51876 KB |
Output is correct |
12 |
Correct |
201 ms |
50300 KB |
Output is correct |
13 |
Correct |
208 ms |
52948 KB |
Output is correct |
14 |
Correct |
185 ms |
55712 KB |
Output is correct |
15 |
Correct |
9 ms |
14672 KB |
Output is correct |
16 |
Correct |
10 ms |
14776 KB |
Output is correct |
17 |
Correct |
9 ms |
14676 KB |
Output is correct |
18 |
Correct |
11 ms |
14548 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
20 |
Correct |
9 ms |
14680 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14804 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14672 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
10 ms |
14932 KB |
Output is correct |
28 |
Correct |
9 ms |
14548 KB |
Output is correct |
29 |
Correct |
10 ms |
14932 KB |
Output is correct |
30 |
Correct |
10 ms |
15060 KB |
Output is correct |
31 |
Correct |
10 ms |
15060 KB |
Output is correct |
32 |
Correct |
8 ms |
14804 KB |
Output is correct |
33 |
Correct |
10 ms |
15072 KB |
Output is correct |
34 |
Correct |
22 ms |
18380 KB |
Output is correct |
35 |
Correct |
195 ms |
51804 KB |
Output is correct |
36 |
Correct |
200 ms |
47152 KB |
Output is correct |
37 |
Correct |
178 ms |
43748 KB |
Output is correct |
38 |
Correct |
217 ms |
42624 KB |
Output is correct |
39 |
Correct |
173 ms |
41888 KB |
Output is correct |
40 |
Correct |
151 ms |
39808 KB |
Output is correct |
41 |
Correct |
215 ms |
48840 KB |
Output is correct |
42 |
Correct |
218 ms |
50704 KB |
Output is correct |
43 |
Correct |
176 ms |
52968 KB |
Output is correct |
44 |
Correct |
168 ms |
43392 KB |
Output is correct |
45 |
Correct |
367 ms |
74180 KB |
Output is correct |
46 |
Correct |
222 ms |
45444 KB |
Output is correct |
47 |
Correct |
195 ms |
42380 KB |
Output is correct |
48 |
Correct |
201 ms |
47572 KB |
Output is correct |
49 |
Correct |
342 ms |
72736 KB |
Output is correct |
50 |
Correct |
287 ms |
68352 KB |
Output is correct |
51 |
Correct |
319 ms |
64968 KB |
Output is correct |
52 |
Correct |
438 ms |
69704 KB |
Output is correct |
53 |
Correct |
423 ms |
77140 KB |
Output is correct |
54 |
Correct |
569 ms |
78732 KB |
Output is correct |
55 |
Correct |
178 ms |
55164 KB |
Output is correct |
56 |
Correct |
423 ms |
79296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14560 KB |
Output is correct |
5 |
Correct |
9 ms |
14676 KB |
Output is correct |
6 |
Correct |
10 ms |
14712 KB |
Output is correct |
7 |
Correct |
9 ms |
14676 KB |
Output is correct |
8 |
Correct |
10 ms |
14804 KB |
Output is correct |
9 |
Correct |
153 ms |
43064 KB |
Output is correct |
10 |
Correct |
188 ms |
51876 KB |
Output is correct |
11 |
Correct |
201 ms |
50300 KB |
Output is correct |
12 |
Correct |
208 ms |
52948 KB |
Output is correct |
13 |
Correct |
185 ms |
55712 KB |
Output is correct |
14 |
Correct |
216 ms |
42116 KB |
Output is correct |
15 |
Correct |
350 ms |
55652 KB |
Output is correct |
16 |
Correct |
421 ms |
51920 KB |
Output is correct |
17 |
Correct |
398 ms |
60152 KB |
Output is correct |
18 |
Correct |
388 ms |
57400 KB |
Output is correct |
19 |
Correct |
202 ms |
46740 KB |
Output is correct |
20 |
Correct |
194 ms |
48044 KB |
Output is correct |
21 |
Correct |
197 ms |
47472 KB |
Output is correct |
22 |
Correct |
208 ms |
47844 KB |
Output is correct |
23 |
Correct |
196 ms |
47840 KB |
Output is correct |
24 |
Correct |
207 ms |
46640 KB |
Output is correct |
25 |
Correct |
198 ms |
47444 KB |
Output is correct |
26 |
Correct |
200 ms |
46408 KB |
Output is correct |
27 |
Correct |
9 ms |
14672 KB |
Output is correct |
28 |
Correct |
10 ms |
14776 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
11 ms |
14548 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
9 ms |
14676 KB |
Output is correct |
34 |
Correct |
9 ms |
14804 KB |
Output is correct |
35 |
Correct |
9 ms |
14676 KB |
Output is correct |
36 |
Correct |
22 ms |
18380 KB |
Output is correct |
37 |
Correct |
195 ms |
51804 KB |
Output is correct |
38 |
Correct |
200 ms |
47152 KB |
Output is correct |
39 |
Correct |
178 ms |
43748 KB |
Output is correct |
40 |
Correct |
217 ms |
42624 KB |
Output is correct |
41 |
Correct |
173 ms |
41888 KB |
Output is correct |
42 |
Correct |
151 ms |
39808 KB |
Output is correct |
43 |
Correct |
215 ms |
48840 KB |
Output is correct |
44 |
Correct |
218 ms |
50704 KB |
Output is correct |
45 |
Correct |
176 ms |
52968 KB |
Output is correct |
46 |
Correct |
168 ms |
43392 KB |
Output is correct |
47 |
Correct |
28 ms |
18508 KB |
Output is correct |
48 |
Correct |
419 ms |
53460 KB |
Output is correct |
49 |
Correct |
404 ms |
50544 KB |
Output is correct |
50 |
Correct |
437 ms |
48728 KB |
Output is correct |
51 |
Correct |
344 ms |
47692 KB |
Output is correct |
52 |
Correct |
333 ms |
45548 KB |
Output is correct |
53 |
Correct |
207 ms |
39540 KB |
Output is correct |
54 |
Correct |
503 ms |
52796 KB |
Output is correct |
55 |
Correct |
377 ms |
54296 KB |
Output is correct |
56 |
Correct |
324 ms |
59000 KB |
Output is correct |
57 |
Correct |
343 ms |
48828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
9 ms |
14560 KB |
Output is correct |
6 |
Correct |
9 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14712 KB |
Output is correct |
8 |
Correct |
9 ms |
14676 KB |
Output is correct |
9 |
Correct |
10 ms |
14804 KB |
Output is correct |
10 |
Correct |
153 ms |
43064 KB |
Output is correct |
11 |
Correct |
188 ms |
51876 KB |
Output is correct |
12 |
Correct |
201 ms |
50300 KB |
Output is correct |
13 |
Correct |
208 ms |
52948 KB |
Output is correct |
14 |
Correct |
185 ms |
55712 KB |
Output is correct |
15 |
Correct |
216 ms |
42116 KB |
Output is correct |
16 |
Correct |
350 ms |
55652 KB |
Output is correct |
17 |
Correct |
421 ms |
51920 KB |
Output is correct |
18 |
Correct |
398 ms |
60152 KB |
Output is correct |
19 |
Correct |
388 ms |
57400 KB |
Output is correct |
20 |
Correct |
202 ms |
46740 KB |
Output is correct |
21 |
Correct |
194 ms |
48044 KB |
Output is correct |
22 |
Correct |
197 ms |
47472 KB |
Output is correct |
23 |
Correct |
208 ms |
47844 KB |
Output is correct |
24 |
Correct |
196 ms |
47840 KB |
Output is correct |
25 |
Correct |
207 ms |
46640 KB |
Output is correct |
26 |
Correct |
198 ms |
47444 KB |
Output is correct |
27 |
Correct |
200 ms |
46408 KB |
Output is correct |
28 |
Correct |
9 ms |
14672 KB |
Output is correct |
29 |
Correct |
10 ms |
14776 KB |
Output is correct |
30 |
Correct |
9 ms |
14676 KB |
Output is correct |
31 |
Correct |
11 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14676 KB |
Output is correct |
33 |
Correct |
9 ms |
14680 KB |
Output is correct |
34 |
Correct |
9 ms |
14676 KB |
Output is correct |
35 |
Correct |
9 ms |
14804 KB |
Output is correct |
36 |
Correct |
9 ms |
14676 KB |
Output is correct |
37 |
Correct |
22 ms |
18380 KB |
Output is correct |
38 |
Correct |
195 ms |
51804 KB |
Output is correct |
39 |
Correct |
200 ms |
47152 KB |
Output is correct |
40 |
Correct |
178 ms |
43748 KB |
Output is correct |
41 |
Correct |
217 ms |
42624 KB |
Output is correct |
42 |
Correct |
173 ms |
41888 KB |
Output is correct |
43 |
Correct |
151 ms |
39808 KB |
Output is correct |
44 |
Correct |
215 ms |
48840 KB |
Output is correct |
45 |
Correct |
218 ms |
50704 KB |
Output is correct |
46 |
Correct |
176 ms |
52968 KB |
Output is correct |
47 |
Correct |
168 ms |
43392 KB |
Output is correct |
48 |
Correct |
28 ms |
18508 KB |
Output is correct |
49 |
Correct |
419 ms |
53460 KB |
Output is correct |
50 |
Correct |
404 ms |
50544 KB |
Output is correct |
51 |
Correct |
437 ms |
48728 KB |
Output is correct |
52 |
Correct |
344 ms |
47692 KB |
Output is correct |
53 |
Correct |
333 ms |
45548 KB |
Output is correct |
54 |
Correct |
207 ms |
39540 KB |
Output is correct |
55 |
Correct |
503 ms |
52796 KB |
Output is correct |
56 |
Correct |
377 ms |
54296 KB |
Output is correct |
57 |
Correct |
324 ms |
59000 KB |
Output is correct |
58 |
Correct |
343 ms |
48828 KB |
Output is correct |
59 |
Correct |
97 ms |
26132 KB |
Output is correct |
60 |
Correct |
351 ms |
55488 KB |
Output is correct |
61 |
Correct |
366 ms |
53016 KB |
Output is correct |
62 |
Correct |
430 ms |
57460 KB |
Output is correct |
63 |
Correct |
353 ms |
57984 KB |
Output is correct |
64 |
Correct |
9 ms |
14676 KB |
Output is correct |
65 |
Correct |
8 ms |
14672 KB |
Output is correct |
66 |
Correct |
8 ms |
14676 KB |
Output is correct |
67 |
Correct |
10 ms |
14932 KB |
Output is correct |
68 |
Correct |
9 ms |
14548 KB |
Output is correct |
69 |
Correct |
10 ms |
14932 KB |
Output is correct |
70 |
Correct |
10 ms |
15060 KB |
Output is correct |
71 |
Correct |
10 ms |
15060 KB |
Output is correct |
72 |
Correct |
8 ms |
14804 KB |
Output is correct |
73 |
Correct |
10 ms |
15072 KB |
Output is correct |
74 |
Correct |
367 ms |
74180 KB |
Output is correct |
75 |
Correct |
222 ms |
45444 KB |
Output is correct |
76 |
Correct |
195 ms |
42380 KB |
Output is correct |
77 |
Correct |
201 ms |
47572 KB |
Output is correct |
78 |
Correct |
342 ms |
72736 KB |
Output is correct |
79 |
Correct |
287 ms |
68352 KB |
Output is correct |
80 |
Correct |
319 ms |
64968 KB |
Output is correct |
81 |
Correct |
438 ms |
69704 KB |
Output is correct |
82 |
Correct |
423 ms |
77140 KB |
Output is correct |
83 |
Correct |
569 ms |
78732 KB |
Output is correct |
84 |
Correct |
178 ms |
55164 KB |
Output is correct |
85 |
Correct |
423 ms |
79296 KB |
Output is correct |
86 |
Correct |
122 ms |
34420 KB |
Output is correct |
87 |
Correct |
394 ms |
49676 KB |
Output is correct |
88 |
Correct |
394 ms |
50036 KB |
Output is correct |
89 |
Correct |
292 ms |
50936 KB |
Output is correct |
90 |
Correct |
428 ms |
80852 KB |
Output is correct |
91 |
Correct |
436 ms |
87248 KB |
Output is correct |
92 |
Correct |
432 ms |
66904 KB |
Output is correct |
93 |
Correct |
598 ms |
72004 KB |
Output is correct |
94 |
Correct |
562 ms |
81112 KB |
Output is correct |
95 |
Correct |
769 ms |
82756 KB |
Output is correct |
96 |
Correct |
377 ms |
55196 KB |
Output is correct |
97 |
Correct |
425 ms |
67756 KB |
Output is correct |