#include<bits/stdc++.h>
#include "swap.h"
#define MAXN 100007
using namespace std;
int n,m;
struct edge{
int from,to,cost;
inline friend bool operator < (edge fr,edge sc){
return fr.cost<sc.cost;
}
}e[MAXN];
int closest[MAXN],special[MAXN];
int lt[MAXN],rt[MAXN];
vector<int> query[MAXN];
struct union_find{
vector< pair<int,int> > dsu[MAXN];
int sz[MAXN],trip[MAXN],deg[MAXN],tim;
void reset(){
for(int i=1;i<=n;i++){
dsu[i]={{i,0}}; sz[i]=1; trip[i]=deg[i]=0;
}
tim=0;
}
int root(int x){
if(dsu[x].back().first==x)return dsu[x].back().first;
return root(dsu[x].back().first);
}
void mergev(int x,int y){
tim++;
int rootx=root(x);
int rooty=root(y);
deg[x]++; deg[y]++;
if(deg[x]>=3)trip[rootx]=x;
if(deg[y]>=3)trip[rootx]=y;
if(rootx==rooty)return;
if(sz[rootx]<sz[rooty])swap(rootx,rooty);
dsu[rooty].push_back({rootx,tim});
sz[rootx]+=sz[rooty];
if(trip[rooty]!=0)trip[rootx]=trip[rooty];
}
int comp(int x,int when){
int l=0,r=dsu[x].size(),tt;
while(l+1<r){
tt=(l+r)/2;
if(dsu[x][tt].second<=when){
l=tt;
}else{
r=tt;
}
}
return dsu[x][l].first;
}
int pastroot(int x,int when){
int curr=comp(x,when);
if(curr==x)return x;
return pastroot(curr,when);
}
}graph;
void build_mst(){
graph.reset();
for(int i=1;i<=m;i++){
graph.mergev(e[i].from,e[i].to);
for(int f:query[i]){
closest[f]=graph.trip[graph.root(f)];
}
}
}
void init(int N, int M,vector<int> U, vector<int> V, vector<int> W) {
n=N; m=M;
for(int i=1;i<=m;i++){
e[i]={U[i-1]+1,V[i-1]+1,W[i-1]};
}
sort(e+1,e+m+1);
for(int i=1;i<=n;i++){
lt[i]=0; rt[i]=m+1;
}
for(int i=1;i<=18;i++){
for(int f=1;f<=m;f++)query[f].clear();
for(int f=1;f<=n;f++){
query[(lt[f]+rt[f])/2].push_back(f);
}
build_mst();
for(int f=1;f<=n;f++){
if(closest[f]!=0){
rt[f]=(lt[f]+rt[f])/2;
special[f]=closest[f];
}else{
lt[f]=(lt[f]+rt[f])/2;
}
}
}
}
int getMinimumFuelCapacity(int X, int Y){
X++; Y++;
if(rt[X]==m+1)return -1;
int ans=e[max(rt[X],rt[Y])].cost;
int l=0,r=m+1,tt;
while(l+1<r){
tt=(l+r)/2;
if(graph.pastroot(X,tt)==graph.pastroot(Y,tt)){
r=tt;
}else{
l=tt;
}
}
ans=max(ans,e[r].cost);
return ans;
}
/*
int main(){
init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
cout<<getMinimumFuelCapacity(1, 2)<<"\n";
cout<<getMinimumFuelCapacity(2,4)<<"\n";
cout<<getMinimumFuelCapacity(0,1)<<"\n";
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
111 ms |
18732 KB |
Output is correct |
10 |
Correct |
135 ms |
21004 KB |
Output is correct |
11 |
Correct |
132 ms |
20868 KB |
Output is correct |
12 |
Correct |
141 ms |
21348 KB |
Output is correct |
13 |
Correct |
102 ms |
21592 KB |
Output is correct |
14 |
Correct |
112 ms |
18824 KB |
Output is correct |
15 |
Correct |
163 ms |
22612 KB |
Output is correct |
16 |
Correct |
163 ms |
22328 KB |
Output is correct |
17 |
Correct |
174 ms |
23144 KB |
Output is correct |
18 |
Correct |
118 ms |
22916 KB |
Output is correct |
19 |
Incorrect |
44 ms |
12824 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
156 ms |
26776 KB |
Output is correct |
4 |
Correct |
177 ms |
27188 KB |
Output is correct |
5 |
Correct |
163 ms |
27236 KB |
Output is correct |
6 |
Correct |
159 ms |
27148 KB |
Output is correct |
7 |
Correct |
164 ms |
27208 KB |
Output is correct |
8 |
Correct |
196 ms |
27080 KB |
Output is correct |
9 |
Correct |
158 ms |
27220 KB |
Output is correct |
10 |
Correct |
155 ms |
26756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
111 ms |
18732 KB |
Output is correct |
10 |
Correct |
135 ms |
21004 KB |
Output is correct |
11 |
Correct |
132 ms |
20868 KB |
Output is correct |
12 |
Correct |
141 ms |
21348 KB |
Output is correct |
13 |
Correct |
102 ms |
21592 KB |
Output is correct |
14 |
Correct |
112 ms |
18824 KB |
Output is correct |
15 |
Correct |
163 ms |
22612 KB |
Output is correct |
16 |
Correct |
163 ms |
22328 KB |
Output is correct |
17 |
Correct |
174 ms |
23144 KB |
Output is correct |
18 |
Correct |
118 ms |
22916 KB |
Output is correct |
19 |
Correct |
156 ms |
26776 KB |
Output is correct |
20 |
Correct |
177 ms |
27188 KB |
Output is correct |
21 |
Correct |
163 ms |
27236 KB |
Output is correct |
22 |
Correct |
159 ms |
27148 KB |
Output is correct |
23 |
Correct |
164 ms |
27208 KB |
Output is correct |
24 |
Correct |
196 ms |
27080 KB |
Output is correct |
25 |
Correct |
158 ms |
27220 KB |
Output is correct |
26 |
Correct |
155 ms |
26756 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
9048 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8840 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
18 ms |
10912 KB |
Output is correct |
37 |
Correct |
140 ms |
23364 KB |
Output is correct |
38 |
Correct |
136 ms |
23568 KB |
Output is correct |
39 |
Correct |
132 ms |
24072 KB |
Output is correct |
40 |
Correct |
135 ms |
24424 KB |
Output is correct |
41 |
Correct |
128 ms |
24924 KB |
Output is correct |
42 |
Correct |
125 ms |
24008 KB |
Output is correct |
43 |
Correct |
142 ms |
23308 KB |
Output is correct |
44 |
Correct |
139 ms |
23172 KB |
Output is correct |
45 |
Correct |
115 ms |
26316 KB |
Output is correct |
46 |
Correct |
144 ms |
26572 KB |
Output is correct |
47 |
Correct |
26 ms |
11344 KB |
Output is correct |
48 |
Correct |
290 ms |
28024 KB |
Output is correct |
49 |
Correct |
282 ms |
28352 KB |
Output is correct |
50 |
Correct |
281 ms |
29128 KB |
Output is correct |
51 |
Correct |
270 ms |
29824 KB |
Output is correct |
52 |
Correct |
256 ms |
29300 KB |
Output is correct |
53 |
Correct |
231 ms |
25792 KB |
Output is correct |
54 |
Correct |
292 ms |
28904 KB |
Output is correct |
55 |
Correct |
299 ms |
27856 KB |
Output is correct |
56 |
Correct |
185 ms |
31096 KB |
Output is correct |
57 |
Correct |
274 ms |
32308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |