#include "island.h"
#include <bits/stdc++.h>
using namespace std;
const int GAP = 500;
struct UnionFind {
int n;
vector<int> uf,si,c;
UnionFind(int n=0):n(n),uf(n),si(n,1){
for(int i=0;i<n;++i)
uf[i]=i;
}
int find(int x){return x==uf[x]?x:find(uf[x]);}
bool join(int x, int y, bool keep_track){
if((x=find(x))==(y=find(y))) return false;
if(si[x]<si[y]) swap(x,y);
si[x]+=si[y];uf[y]=x;
if(keep_track) c.push_back(y);
return true;
}
void rollback(){
while(c.size()){
int x=c.back(); c.pop_back();
si[uf[x]]-=si[x];uf[x]=x;;
}
}
} UF[605];
vector<pair<int,int>> edges;
vector<int> F;
int M;
void Init(int K, vector<int> F, vector<int> S, vector<int> E){
M = S.size();
while(K!=(int)F.size());
edges.resize(M);
for(int i=0;i<M;++i)
edges[i] = make_pair(S[i], E[i]);
reverse(edges.begin(), edges.end());
UnionFind cur(K);
::F = F;
for(int i=0;i<M;++i){
if(i%GAP==0) UF[i/GAP] = cur;
cur.join(F[edges[i].first], F[edges[i].second], 0);
}
}
int Separate(int A, int B){
int bucket = 0, start = 0, ans = -1;
for(int i=0;i<M;i+=GAP){
if(UF[i/GAP].find(A) == UF[i/GAP].find(B))
break;
start = i, bucket = i/GAP;
}
for(int i=start;i<start+GAP && i<M; ++i){
// cout << "Joining " << F[edges[i].first] << ' ' << F[edges[i].second] << '\n';
UF[bucket].join(F[edges[i].first], F[edges[i].second], 1);
if(UF[bucket].find(A) == UF[bucket].find(B)){
ans = M - i;
break;
}
}
assert(ans != -1);
UF[bucket].rollback();
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4306 ms |
479076 KB |
Output is correct |
2 |
Correct |
4594 ms |
479076 KB |
Output is correct |
3 |
Correct |
4389 ms |
479168 KB |
Output is correct |
4 |
Correct |
4839 ms |
479160 KB |
Output is correct |
5 |
Correct |
4061 ms |
479072 KB |
Output is correct |
6 |
Correct |
4401 ms |
479076 KB |
Output is correct |
7 |
Correct |
4073 ms |
479076 KB |
Output is correct |
8 |
Correct |
3695 ms |
479072 KB |
Output is correct |
9 |
Correct |
3879 ms |
479072 KB |
Output is correct |
10 |
Correct |
3946 ms |
479076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5099 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5099 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |