#include<bits/stdc++.h>
#include "swap.h"
#define MAXN 200007
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],parent[MAXN];
int lt[MAXN],rt[MAXN],in[MAXN],out[MAXN],num;
vector<int> query[MAXN];
vector<int> tree[MAXN],edges;
bool treeedge[MAXN];
struct union_find{
vector< pair<int,int> > dsu[MAXN];
int sz[MAXN],trip[MAXN],deg[MAXN],tim,topv[MAXN];
void reset(){
for(int i=1;i<=n;i++){
dsu[i]={{i,0}}; sz[i]=1; trip[i]=deg[i]=0;
topv[i]=i;
}
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];
if(in[topv[rooty]]<in[topv[rootx]])topv[rootx]=topv[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,bcc;
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 dfs(int x,int p){
parent[x]=p;
num++; in[x]=num;
for(int i:tree[x]){
if(i==p)continue;
dfs(i,x);
}
out[x]=num;
}
bool subtree(int x,int y){
return in[y]>=in[x] and out[y]<=out[x];
}
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;
}
}
}
bcc.reset();
for(int i=1;i<=m;i++){
if(bcc.root(e[i].from)==bcc.root(e[i].to))continue;
tree[e[i].from].push_back(e[i].to);
tree[e[i].to].push_back(e[i].from);
bcc.mergev(e[i].from,e[i].to);
treeedge[i]=true;
}
dfs(1,0);
bcc.reset();
for(int i=1;i<=m;i++){
if(treeedge[i])continue;
int x=bcc.root(e[i].from),y=bcc.root(e[i].to);
while(!subtree(bcc.topv[x],bcc.topv[y])){
bcc.mergev(x,bcc.root(parent[bcc.topv[x]]));
x=bcc.root(x);
bcc.tim--;
}
while(bcc.root(x)!=bcc.root(y)){
bcc.mergev(y,bcc.root(parent[bcc.topv[y]]));
y=bcc.root(y);
bcc.tim--;
}
bcc.tim++;
edges.push_back(e[i].cost);
}
}
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=edges.size()+1,tt;
while(l+1<r){
tt=(l+r)/2;
if(bcc.pastroot(X,tt)==bcc.pastroot(Y,tt)){
r=tt;
}else{
l=tt;
}
}
if(r!=edges.size()+1)ans=min(ans,edges[r-1]);
l=0; r=m+1;
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,5,{0,1,2,3,2},{1,2,3,0,4},{5,6,4,1,10});
cout<<getMinimumFuelCapacity(0,2)<<"\n";
}
*/
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:199:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
199 | if(r!=edges.size()+1)ans=min(ans,edges[r-1]);
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31320 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
3 ms |
31344 KB |
Output is correct |
4 |
Correct |
4 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31560 KB |
Output is correct |
6 |
Correct |
4 ms |
31324 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
4 ms |
31580 KB |
Output is correct |
9 |
Correct |
142 ms |
50152 KB |
Output is correct |
10 |
Correct |
176 ms |
54540 KB |
Output is correct |
11 |
Correct |
180 ms |
53928 KB |
Output is correct |
12 |
Correct |
203 ms |
55208 KB |
Output is correct |
13 |
Correct |
126 ms |
56584 KB |
Output is correct |
14 |
Correct |
139 ms |
49644 KB |
Output is correct |
15 |
Correct |
201 ms |
56408 KB |
Output is correct |
16 |
Correct |
222 ms |
54716 KB |
Output is correct |
17 |
Correct |
212 ms |
58160 KB |
Output is correct |
18 |
Correct |
150 ms |
57200 KB |
Output is correct |
19 |
Incorrect |
49 ms |
37204 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31320 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
205 ms |
57636 KB |
Output is correct |
4 |
Correct |
220 ms |
58416 KB |
Output is correct |
5 |
Correct |
202 ms |
57952 KB |
Output is correct |
6 |
Correct |
201 ms |
58160 KB |
Output is correct |
7 |
Correct |
197 ms |
58188 KB |
Output is correct |
8 |
Correct |
200 ms |
57596 KB |
Output is correct |
9 |
Correct |
229 ms |
58184 KB |
Output is correct |
10 |
Correct |
193 ms |
57364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31320 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
3 ms |
31344 KB |
Output is correct |
4 |
Correct |
4 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31560 KB |
Output is correct |
6 |
Correct |
4 ms |
31324 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
4 ms |
31580 KB |
Output is correct |
9 |
Correct |
3 ms |
31324 KB |
Output is correct |
10 |
Correct |
5 ms |
31568 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
5 ms |
31324 KB |
Output is correct |
13 |
Correct |
4 ms |
31324 KB |
Output is correct |
14 |
Correct |
4 ms |
31324 KB |
Output is correct |
15 |
Correct |
4 ms |
31560 KB |
Output is correct |
16 |
Correct |
4 ms |
31540 KB |
Output is correct |
17 |
Correct |
4 ms |
31400 KB |
Output is correct |
18 |
Correct |
5 ms |
31324 KB |
Output is correct |
19 |
Correct |
5 ms |
31324 KB |
Output is correct |
20 |
Correct |
4 ms |
31580 KB |
Output is correct |
21 |
Correct |
4 ms |
31324 KB |
Output is correct |
22 |
Correct |
4 ms |
31324 KB |
Output is correct |
23 |
Correct |
4 ms |
31324 KB |
Output is correct |
24 |
Correct |
5 ms |
31580 KB |
Output is correct |
25 |
Correct |
5 ms |
31832 KB |
Output is correct |
26 |
Correct |
5 ms |
31576 KB |
Output is correct |
27 |
Correct |
4 ms |
31580 KB |
Output is correct |
28 |
Correct |
5 ms |
31404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
31324 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
4 ms |
31320 KB |
Output is correct |
4 |
Correct |
3 ms |
31344 KB |
Output is correct |
5 |
Correct |
4 ms |
31324 KB |
Output is correct |
6 |
Correct |
5 ms |
31560 KB |
Output is correct |
7 |
Correct |
4 ms |
31324 KB |
Output is correct |
8 |
Correct |
5 ms |
31320 KB |
Output is correct |
9 |
Correct |
4 ms |
31580 KB |
Output is correct |
10 |
Correct |
142 ms |
50152 KB |
Output is correct |
11 |
Correct |
176 ms |
54540 KB |
Output is correct |
12 |
Correct |
180 ms |
53928 KB |
Output is correct |
13 |
Correct |
203 ms |
55208 KB |
Output is correct |
14 |
Correct |
126 ms |
56584 KB |
Output is correct |
15 |
Correct |
5 ms |
31568 KB |
Output is correct |
16 |
Correct |
6 ms |
31324 KB |
Output is correct |
17 |
Correct |
5 ms |
31324 KB |
Output is correct |
18 |
Correct |
4 ms |
31324 KB |
Output is correct |
19 |
Correct |
4 ms |
31324 KB |
Output is correct |
20 |
Correct |
4 ms |
31560 KB |
Output is correct |
21 |
Correct |
4 ms |
31540 KB |
Output is correct |
22 |
Correct |
4 ms |
31400 KB |
Output is correct |
23 |
Correct |
5 ms |
31324 KB |
Output is correct |
24 |
Correct |
5 ms |
31324 KB |
Output is correct |
25 |
Correct |
4 ms |
31580 KB |
Output is correct |
26 |
Correct |
4 ms |
31324 KB |
Output is correct |
27 |
Correct |
4 ms |
31324 KB |
Output is correct |
28 |
Correct |
4 ms |
31324 KB |
Output is correct |
29 |
Correct |
5 ms |
31580 KB |
Output is correct |
30 |
Correct |
5 ms |
31832 KB |
Output is correct |
31 |
Correct |
5 ms |
31576 KB |
Output is correct |
32 |
Correct |
4 ms |
31580 KB |
Output is correct |
33 |
Correct |
5 ms |
31404 KB |
Output is correct |
34 |
Correct |
28 ms |
34092 KB |
Output is correct |
35 |
Correct |
187 ms |
55348 KB |
Output is correct |
36 |
Correct |
179 ms |
54036 KB |
Output is correct |
37 |
Correct |
171 ms |
53480 KB |
Output is correct |
38 |
Correct |
181 ms |
53632 KB |
Output is correct |
39 |
Correct |
171 ms |
53732 KB |
Output is correct |
40 |
Correct |
164 ms |
52680 KB |
Output is correct |
41 |
Correct |
200 ms |
53936 KB |
Output is correct |
42 |
Correct |
184 ms |
54624 KB |
Output is correct |
43 |
Correct |
154 ms |
58700 KB |
Output is correct |
44 |
Correct |
218 ms |
55244 KB |
Output is correct |
45 |
Correct |
261 ms |
53716 KB |
Output is correct |
46 |
Correct |
207 ms |
55200 KB |
Output is correct |
47 |
Correct |
177 ms |
55488 KB |
Output is correct |
48 |
Correct |
215 ms |
57644 KB |
Output is correct |
49 |
Correct |
103 ms |
41164 KB |
Output is correct |
50 |
Correct |
102 ms |
40660 KB |
Output is correct |
51 |
Correct |
209 ms |
52424 KB |
Output is correct |
52 |
Correct |
297 ms |
58748 KB |
Output is correct |
53 |
Correct |
263 ms |
61004 KB |
Output is correct |
54 |
Correct |
374 ms |
61400 KB |
Output is correct |
55 |
Correct |
180 ms |
61388 KB |
Output is correct |
56 |
Correct |
290 ms |
61900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31320 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
3 ms |
31344 KB |
Output is correct |
4 |
Correct |
4 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31560 KB |
Output is correct |
6 |
Correct |
4 ms |
31324 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
4 ms |
31580 KB |
Output is correct |
9 |
Correct |
142 ms |
50152 KB |
Output is correct |
10 |
Correct |
176 ms |
54540 KB |
Output is correct |
11 |
Correct |
180 ms |
53928 KB |
Output is correct |
12 |
Correct |
203 ms |
55208 KB |
Output is correct |
13 |
Correct |
126 ms |
56584 KB |
Output is correct |
14 |
Correct |
139 ms |
49644 KB |
Output is correct |
15 |
Correct |
201 ms |
56408 KB |
Output is correct |
16 |
Correct |
222 ms |
54716 KB |
Output is correct |
17 |
Correct |
212 ms |
58160 KB |
Output is correct |
18 |
Correct |
150 ms |
57200 KB |
Output is correct |
19 |
Correct |
205 ms |
57636 KB |
Output is correct |
20 |
Correct |
220 ms |
58416 KB |
Output is correct |
21 |
Correct |
202 ms |
57952 KB |
Output is correct |
22 |
Correct |
201 ms |
58160 KB |
Output is correct |
23 |
Correct |
197 ms |
58188 KB |
Output is correct |
24 |
Correct |
200 ms |
57596 KB |
Output is correct |
25 |
Correct |
229 ms |
58184 KB |
Output is correct |
26 |
Correct |
193 ms |
57364 KB |
Output is correct |
27 |
Correct |
5 ms |
31568 KB |
Output is correct |
28 |
Correct |
6 ms |
31324 KB |
Output is correct |
29 |
Correct |
5 ms |
31324 KB |
Output is correct |
30 |
Correct |
4 ms |
31324 KB |
Output is correct |
31 |
Correct |
4 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
31560 KB |
Output is correct |
33 |
Correct |
4 ms |
31540 KB |
Output is correct |
34 |
Correct |
4 ms |
31400 KB |
Output is correct |
35 |
Correct |
5 ms |
31324 KB |
Output is correct |
36 |
Correct |
28 ms |
34092 KB |
Output is correct |
37 |
Correct |
187 ms |
55348 KB |
Output is correct |
38 |
Correct |
179 ms |
54036 KB |
Output is correct |
39 |
Correct |
171 ms |
53480 KB |
Output is correct |
40 |
Correct |
181 ms |
53632 KB |
Output is correct |
41 |
Correct |
171 ms |
53732 KB |
Output is correct |
42 |
Correct |
164 ms |
52680 KB |
Output is correct |
43 |
Correct |
200 ms |
53936 KB |
Output is correct |
44 |
Correct |
184 ms |
54624 KB |
Output is correct |
45 |
Correct |
154 ms |
58700 KB |
Output is correct |
46 |
Correct |
218 ms |
55244 KB |
Output is correct |
47 |
Correct |
32 ms |
34128 KB |
Output is correct |
48 |
Correct |
343 ms |
56696 KB |
Output is correct |
49 |
Correct |
334 ms |
56248 KB |
Output is correct |
50 |
Correct |
322 ms |
56244 KB |
Output is correct |
51 |
Correct |
330 ms |
56308 KB |
Output is correct |
52 |
Correct |
299 ms |
55568 KB |
Output is correct |
53 |
Correct |
261 ms |
50876 KB |
Output is correct |
54 |
Correct |
363 ms |
56484 KB |
Output is correct |
55 |
Correct |
354 ms |
56876 KB |
Output is correct |
56 |
Correct |
219 ms |
62000 KB |
Output is correct |
57 |
Correct |
322 ms |
58416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
31324 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Correct |
4 ms |
31320 KB |
Output is correct |
4 |
Correct |
3 ms |
31344 KB |
Output is correct |
5 |
Correct |
4 ms |
31324 KB |
Output is correct |
6 |
Correct |
5 ms |
31560 KB |
Output is correct |
7 |
Correct |
4 ms |
31324 KB |
Output is correct |
8 |
Correct |
5 ms |
31320 KB |
Output is correct |
9 |
Correct |
4 ms |
31580 KB |
Output is correct |
10 |
Correct |
142 ms |
50152 KB |
Output is correct |
11 |
Correct |
176 ms |
54540 KB |
Output is correct |
12 |
Correct |
180 ms |
53928 KB |
Output is correct |
13 |
Correct |
203 ms |
55208 KB |
Output is correct |
14 |
Correct |
126 ms |
56584 KB |
Output is correct |
15 |
Correct |
139 ms |
49644 KB |
Output is correct |
16 |
Correct |
201 ms |
56408 KB |
Output is correct |
17 |
Correct |
222 ms |
54716 KB |
Output is correct |
18 |
Correct |
212 ms |
58160 KB |
Output is correct |
19 |
Correct |
150 ms |
57200 KB |
Output is correct |
20 |
Correct |
205 ms |
57636 KB |
Output is correct |
21 |
Correct |
220 ms |
58416 KB |
Output is correct |
22 |
Correct |
202 ms |
57952 KB |
Output is correct |
23 |
Correct |
201 ms |
58160 KB |
Output is correct |
24 |
Correct |
197 ms |
58188 KB |
Output is correct |
25 |
Correct |
200 ms |
57596 KB |
Output is correct |
26 |
Correct |
229 ms |
58184 KB |
Output is correct |
27 |
Correct |
193 ms |
57364 KB |
Output is correct |
28 |
Correct |
5 ms |
31568 KB |
Output is correct |
29 |
Correct |
6 ms |
31324 KB |
Output is correct |
30 |
Correct |
5 ms |
31324 KB |
Output is correct |
31 |
Correct |
4 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
31324 KB |
Output is correct |
33 |
Correct |
4 ms |
31560 KB |
Output is correct |
34 |
Correct |
4 ms |
31540 KB |
Output is correct |
35 |
Correct |
4 ms |
31400 KB |
Output is correct |
36 |
Correct |
5 ms |
31324 KB |
Output is correct |
37 |
Correct |
28 ms |
34092 KB |
Output is correct |
38 |
Correct |
187 ms |
55348 KB |
Output is correct |
39 |
Correct |
179 ms |
54036 KB |
Output is correct |
40 |
Correct |
171 ms |
53480 KB |
Output is correct |
41 |
Correct |
181 ms |
53632 KB |
Output is correct |
42 |
Correct |
171 ms |
53732 KB |
Output is correct |
43 |
Correct |
164 ms |
52680 KB |
Output is correct |
44 |
Correct |
200 ms |
53936 KB |
Output is correct |
45 |
Correct |
184 ms |
54624 KB |
Output is correct |
46 |
Correct |
154 ms |
58700 KB |
Output is correct |
47 |
Correct |
218 ms |
55244 KB |
Output is correct |
48 |
Correct |
32 ms |
34128 KB |
Output is correct |
49 |
Correct |
343 ms |
56696 KB |
Output is correct |
50 |
Correct |
334 ms |
56248 KB |
Output is correct |
51 |
Correct |
322 ms |
56244 KB |
Output is correct |
52 |
Correct |
330 ms |
56308 KB |
Output is correct |
53 |
Correct |
299 ms |
55568 KB |
Output is correct |
54 |
Correct |
261 ms |
50876 KB |
Output is correct |
55 |
Correct |
363 ms |
56484 KB |
Output is correct |
56 |
Correct |
354 ms |
56876 KB |
Output is correct |
57 |
Correct |
219 ms |
62000 KB |
Output is correct |
58 |
Correct |
322 ms |
58416 KB |
Output is correct |
59 |
Incorrect |
49 ms |
37204 KB |
Output isn't correct |
60 |
Halted |
0 ms |
0 KB |
- |