#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
vector<int> par,sz,swappable,deg,has;
int total_groups;
void init(int n){
par.resize(n); sz.resize(n); swappable.resize(n); deg.resize(n);has.resize(n);
for(int i=0;i<n;i++){
par[i]=i;
sz[i]=1;
swappable[i]=0;
deg[i]=0;
has[i]=0;
}
}
int find(int x){
if(par[x]==x) return x;
else return par[x]=find(par[x]);
}
void sw(int x,int y){
swappable[x]=(swappable[x]&swappable[y]);
swappable[y]=(swappable[x]&swappable[y]);
has[x]|=has[y];
if(has[x]){
swappable[y]=1;
swappable[x]=1;
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
sw(x,y);
par[y] = x;
sz[x] += sz[y];
total_groups--;
}
bool same(int x,int y){
x = find(x);
y = find(y);
if(x==y) return true;
else return false;
}
//remember to init(n+5) and total_groups=n
struct edge{
int u,v,d;
};
bool operator <(edge a,edge b){
return a.d<b.d;
}
#include "swap.h"
int n,m,ok=1,mx=-1;
int sub1=1,sub2=1,sub4=1,sub5=1;
vector<int> tw;
vector<vector<pair<int,int>>> adj;
vector<edge> ed;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n=N,m=M;tw.resize(n+2);
adj.resize(n + 2);
for (int i = 0; i < m; i++) {
adj[U[i]].push_back(make_pair(V[i],W[i]));
adj[V[i]].push_back(make_pair(U[i], W[i]));
mx = max(mx, W[i]);
}
for (int i = 0; i < n; i++) {
sub1&=(adj[i].size() <= 2);
ok&=(adj[i].size()==2);
}
for(int i=0;i<m;i++){
if(U[i]!=0) sub2=0;
tw[V[i]]=W[i];
}
for(int i=0;i<m;i++){
ed.pb({U[i],V[i],W[i]});
}
sort(ed.begin(),ed.end());
}
int getMinimumFuelCapacity(int X, int Y) {
if(sub1){
if(ok) return mx;
else return -1;
}
if(sub2){
if(m<=2) return -1;
if(X>Y) swap(X,Y);
if(X!=0){//if both at side
int ret=max(tw[Y],tw[X]);
int cnt=0;
while(ed[cnt].v==X || ed[cnt].v==Y) cnt++;
ret=max(ret,ed[cnt].d);
return ret;
}
else{
int ret=tw[Y],tmp,cnt=0;
while(ed[cnt].v==Y) cnt++;
ret=max(ret,ed[cnt].d);
tmp=ed[cnt].v;
cnt++;
while(ed[cnt].v==Y || ed[cnt].v==tmp) cnt++;
ret=max(ret,ed[cnt].d);
return ret;
}
}
init(n+2);
int i=0;
while(i<=m-1 && (!same(X,Y) || !swappable[find(X)])){
deg[ed[i].u]++; deg[ed[i].v]++;
if(same(ed[i].u,ed[i].v)) has[find(ed[i].u)]=1;
if(deg[ed[i].u]>=3) has[find(ed[i].u)]=1;
if(deg[ed[i].v]>=3) has[find(ed[i].v)]=1;
unite(ed[i].u,ed[i].v);
if(same(X,Y) && has[find(X)]==1) return ed[i].d;
i++;
}
return -1;
}
/*int main(){
int tt; cin>>tt;
while(tt--){
int n,m; cin>>n>>m;
vector<int>u(m),v(m),w(m);
for(int i=0;i<m;i++) cin>>u[i]>>v[i]>>w[i];
init(n,m,u,v,w);
int q; cin>>q;
for(int i=0;i<q;i++){
int x,y; cin>>x>>y;
cout<<getMinimumFuelCapacity(x,y)<<'\n';
}
}
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
46 ms |
9416 KB |
Output is correct |
10 |
Correct |
59 ms |
10892 KB |
Output is correct |
11 |
Correct |
54 ms |
10680 KB |
Output is correct |
12 |
Correct |
60 ms |
11140 KB |
Output is correct |
13 |
Correct |
58 ms |
11208 KB |
Output is correct |
14 |
Correct |
50 ms |
9552 KB |
Output is correct |
15 |
Correct |
109 ms |
12440 KB |
Output is correct |
16 |
Correct |
110 ms |
12268 KB |
Output is correct |
17 |
Correct |
122 ms |
12704 KB |
Output is correct |
18 |
Correct |
117 ms |
12784 KB |
Output is correct |
19 |
Correct |
56 ms |
6332 KB |
Output is correct |
20 |
Correct |
105 ms |
13328 KB |
Output is correct |
21 |
Correct |
104 ms |
13464 KB |
Output is correct |
22 |
Correct |
112 ms |
13996 KB |
Output is correct |
23 |
Correct |
108 ms |
14004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
98 ms |
13996 KB |
Output is correct |
4 |
Correct |
104 ms |
17100 KB |
Output is correct |
5 |
Correct |
104 ms |
17220 KB |
Output is correct |
6 |
Correct |
97 ms |
17048 KB |
Output is correct |
7 |
Correct |
104 ms |
17184 KB |
Output is correct |
8 |
Correct |
95 ms |
16784 KB |
Output is correct |
9 |
Correct |
113 ms |
16992 KB |
Output is correct |
10 |
Correct |
98 ms |
16652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
456 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
46 ms |
9416 KB |
Output is correct |
11 |
Correct |
59 ms |
10892 KB |
Output is correct |
12 |
Correct |
54 ms |
10680 KB |
Output is correct |
13 |
Correct |
60 ms |
11140 KB |
Output is correct |
14 |
Correct |
58 ms |
11208 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
456 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
8 ms |
2004 KB |
Output is correct |
35 |
Correct |
93 ms |
11500 KB |
Output is correct |
36 |
Correct |
95 ms |
11596 KB |
Output is correct |
37 |
Correct |
98 ms |
11384 KB |
Output is correct |
38 |
Correct |
88 ms |
11364 KB |
Output is correct |
39 |
Correct |
90 ms |
11320 KB |
Output is correct |
40 |
Correct |
83 ms |
10628 KB |
Output is correct |
41 |
Correct |
94 ms |
11488 KB |
Output is correct |
42 |
Correct |
92 ms |
11468 KB |
Output is correct |
43 |
Correct |
78 ms |
11508 KB |
Output is correct |
44 |
Correct |
102 ms |
12004 KB |
Output is correct |
45 |
Correct |
113 ms |
14920 KB |
Output is correct |
46 |
Correct |
97 ms |
11468 KB |
Output is correct |
47 |
Correct |
94 ms |
11432 KB |
Output is correct |
48 |
Correct |
99 ms |
12004 KB |
Output is correct |
49 |
Correct |
69 ms |
13120 KB |
Output is correct |
50 |
Correct |
54 ms |
9356 KB |
Output is correct |
51 |
Correct |
92 ms |
11796 KB |
Output is correct |
52 |
Correct |
128 ms |
16200 KB |
Output is correct |
53 |
Correct |
130 ms |
17008 KB |
Output is correct |
54 |
Correct |
168 ms |
18188 KB |
Output is correct |
55 |
Correct |
84 ms |
11432 KB |
Output is correct |
56 |
Correct |
145 ms |
16736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
46 ms |
9416 KB |
Output is correct |
10 |
Correct |
59 ms |
10892 KB |
Output is correct |
11 |
Correct |
54 ms |
10680 KB |
Output is correct |
12 |
Correct |
60 ms |
11140 KB |
Output is correct |
13 |
Correct |
58 ms |
11208 KB |
Output is correct |
14 |
Correct |
50 ms |
9552 KB |
Output is correct |
15 |
Correct |
109 ms |
12440 KB |
Output is correct |
16 |
Correct |
110 ms |
12268 KB |
Output is correct |
17 |
Correct |
122 ms |
12704 KB |
Output is correct |
18 |
Correct |
117 ms |
12784 KB |
Output is correct |
19 |
Correct |
98 ms |
13996 KB |
Output is correct |
20 |
Correct |
104 ms |
17100 KB |
Output is correct |
21 |
Correct |
104 ms |
17220 KB |
Output is correct |
22 |
Correct |
97 ms |
17048 KB |
Output is correct |
23 |
Correct |
104 ms |
17184 KB |
Output is correct |
24 |
Correct |
95 ms |
16784 KB |
Output is correct |
25 |
Correct |
113 ms |
16992 KB |
Output is correct |
26 |
Correct |
98 ms |
16652 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
308 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
308 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
2004 KB |
Output is correct |
37 |
Correct |
93 ms |
11500 KB |
Output is correct |
38 |
Correct |
95 ms |
11596 KB |
Output is correct |
39 |
Correct |
98 ms |
11384 KB |
Output is correct |
40 |
Correct |
88 ms |
11364 KB |
Output is correct |
41 |
Correct |
90 ms |
11320 KB |
Output is correct |
42 |
Correct |
83 ms |
10628 KB |
Output is correct |
43 |
Correct |
94 ms |
11488 KB |
Output is correct |
44 |
Correct |
92 ms |
11468 KB |
Output is correct |
45 |
Correct |
78 ms |
11508 KB |
Output is correct |
46 |
Correct |
102 ms |
12004 KB |
Output is correct |
47 |
Execution timed out |
2061 ms |
2288 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
46 ms |
9416 KB |
Output is correct |
11 |
Correct |
59 ms |
10892 KB |
Output is correct |
12 |
Correct |
54 ms |
10680 KB |
Output is correct |
13 |
Correct |
60 ms |
11140 KB |
Output is correct |
14 |
Correct |
58 ms |
11208 KB |
Output is correct |
15 |
Correct |
50 ms |
9552 KB |
Output is correct |
16 |
Correct |
109 ms |
12440 KB |
Output is correct |
17 |
Correct |
110 ms |
12268 KB |
Output is correct |
18 |
Correct |
122 ms |
12704 KB |
Output is correct |
19 |
Correct |
117 ms |
12784 KB |
Output is correct |
20 |
Correct |
98 ms |
13996 KB |
Output is correct |
21 |
Correct |
104 ms |
17100 KB |
Output is correct |
22 |
Correct |
104 ms |
17220 KB |
Output is correct |
23 |
Correct |
97 ms |
17048 KB |
Output is correct |
24 |
Correct |
104 ms |
17184 KB |
Output is correct |
25 |
Correct |
95 ms |
16784 KB |
Output is correct |
26 |
Correct |
113 ms |
16992 KB |
Output is correct |
27 |
Correct |
98 ms |
16652 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
8 ms |
2004 KB |
Output is correct |
38 |
Correct |
93 ms |
11500 KB |
Output is correct |
39 |
Correct |
95 ms |
11596 KB |
Output is correct |
40 |
Correct |
98 ms |
11384 KB |
Output is correct |
41 |
Correct |
88 ms |
11364 KB |
Output is correct |
42 |
Correct |
90 ms |
11320 KB |
Output is correct |
43 |
Correct |
83 ms |
10628 KB |
Output is correct |
44 |
Correct |
94 ms |
11488 KB |
Output is correct |
45 |
Correct |
92 ms |
11468 KB |
Output is correct |
46 |
Correct |
78 ms |
11508 KB |
Output is correct |
47 |
Correct |
102 ms |
12004 KB |
Output is correct |
48 |
Execution timed out |
2061 ms |
2288 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |