This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
struct UF{
private:
public:
vi ranki,p,sz;
UF(int n){
ranki.assign(n, 0);
p.assign(n, 0);
sz.assign(n,1);
for(int i = 0; i < n; i++) p[i] = i;
}
int findset(int i){ return (p[i] == i) ? i : findset(p[i]);}
bool issameset(int i, int j) {return findset(i) == findset(j);}
void unionset(int i, int j){
if(!issameset(i,j)){
int x = findset(i), y = findset(j);
if(ranki[x] > ranki[y]){
p[y] = x;
sz[x] = sz[y] = sz[x] + sz [y];
}else{
if(ranki[x] == ranki[y]) ranki[y]++;
p[x] = y;
sz[x] = sz[y] = sz[x] + sz [y];
}
}
}
};
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
int n = (int) r.size();
int m = (int) c.size();
vector<int> ans(n, 0);
bool flag = false;
for(int i = 0; i < n; i++){
if(r[i] != 0){
ans[i] = 1;
flag = true;
}
}
if(flag){
return ans;
}
UF unionfind(n);
for(int i = 0; i < m; i++){
unionfind.unionset(u[i],v[i]);
}
int mini = *min_element(unionfind.sz.begin(),unionfind.sz.end());
for(int i = 0; i < n; i++){
if(unionfind.sz[unionfind.findset(i)] == mini){
ans[i] = 1;
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |