#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int MAX=1e5+5;
int P[MAX];
bool isLine[MAX];
ii segs[MAX];
vector<int> S[MAX];
vector<iii> edges;
vector<ii> history[MAX];
int find(int u) {
if(P[u]==u)
return u;
return P[u]=find(P[u]);
}
ii getNewSeg(ii s1,ii s2,int u,int v) {
if(s1.first==u) {
if(s2.first==v)
return {s1.second,s2.second};
if(s2.second==v)
return {s1.second,s2.first};
} else if(s1.second==u) {
if(s2.first==v)
return {s1.first,s2.second};
if(s2.second==v)
return {s1.first,s2.first};
}
return {-1,-1};
}
void merge(int u,int v,int w) {
if(S[u].size()<S[v].size())
swap(u,v);
P[v]=u;
segs[u]=getNewSeg(segs[u],segs[v],u,v);
bool newIsLine=(segs[u].first!=-1);
if(isLine[u]!=newIsLine) {
isLine[u]=false;
for(int tmp:S[u])
history[tmp].push_back({w,u});
}
if(isLine[v]!=newIsLine) {
isLine[v]=false;
for(int tmp:S[v])
history[tmp].push_back({w,u});
}
while(S[v].size()>0) {
int tmp=S[v].back();
S[v].pop_back();
S[u].push_back(tmp);
}
}
void init(int N, int M,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
for(int i=0;i<M;i++)
edges.push_back({W[i],{U[i],V[i]}});
sort(edges.begin(),edges.end());
for(int i=0;i<N;i++) {
P[i]=i;
isLine[i]=true;
segs[i]={i,i};
S[i].push_back(i);
}
for(iii edge:edges) {
int u=find(edge.second.first),v=find(edge.second.second),w=edge.first;
if(u!=v)
merge(u,v,w);
else if(isLine[u]) {
isLine[u]=false;
segs[u]={-1,-1};
for(int tmp:S[u])
history[tmp].push_back({w,u});
}
}
//for(ii tmp:history[3])
// cerr<<tmp.first<<' '<<tmp.second<<'\n';
}
int getMinimumFuelCapacity(int X, int Y) {
vector<ii> hisX=history[X],hisY=history[Y];
int rs=-1;
while(hisX.size()>0&&hisY.size()>0&&hisX.back().second==hisY.back().second) {
rs=max(hisX.back().first,hisY.back().first);
hisX.pop_back(),hisY.pop_back();
}
return rs;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
230 ms |
22620 KB |
Output is correct |
4 |
Correct |
196 ms |
23044 KB |
Output is correct |
5 |
Correct |
202 ms |
23184 KB |
Output is correct |
6 |
Correct |
199 ms |
23000 KB |
Output is correct |
7 |
Correct |
203 ms |
23264 KB |
Output is correct |
8 |
Correct |
179 ms |
22628 KB |
Output is correct |
9 |
Correct |
227 ms |
22944 KB |
Output is correct |
10 |
Correct |
191 ms |
22644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |