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 all(v) (v).begin(), (v).end()
using namespace std;
using pi = pair<int,int>;
struct CC{
int grp[300001];
int fi(int x){
if(grp[x] == x) return x;
else return grp[x] = fi(grp[x]);
}
void un(int x, int y){
x = fi(x);
y = fi(y);
assert(x!=y);
grp[x] = y;
}
} cc;
vector<int> R,U,V,C;
struct CYCLES{
int grp[300001];
int siz[300001];
int toward[300001];
set<int> components[300001];
set<int> colors[300001];
map<int, queue<int> > remainedge[300001];
queue<int> remaincolor[300001];
int fi(int x){
if(grp[x] == x) return x;
else return grp[x] = fi(grp[x]);
}
void un(int x, int y){
x = fi(x);
y = fi(y);
assert(x!=y);
if(siz[x] > siz[y]) swap(x,y);
siz[y] += siz[x];
for(auto &i :components[x]){
components[y].insert(i);
}
for(auto &i :colors[x]){
if(colors[y].find(i) == colors[y].end()) {
colors[y].insert(i);
}
}
for(auto &i : colors[y]) remaincolor[y].push(i);
for(auto &i : remainedge[x]){
if(i.second.size() && colors[y].find(i.first) != colors[y].end()) remaincolor[y].push(i.first);
while(i.second.size()){
remainedge[y][i.first].push(i.second.front());
i.second.pop();
}
}
grp[x] = y;
toward[y] = -1;
}
void findedge(int x){
x=fi(x);
toward[x] = -1;
while(remaincolor[x].size()){
int i = remaincolor[x].front();
while(remainedge[x][i].size()){
int j = remainedge[x][i].front();
remainedge[x][i].pop();
if(components[x].find(j) == components[x].end()) toward[x] = j;
if(toward[x] != -1) break;
}
if(toward[x] != -1) break;
remaincolor[x].pop();
}
}
} cycles;
int N,M;
vector<pi> adj[300001];
int chk[300001];
vector<pi> candidate;
int ans = 1e9;
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
N = r.size();
M = u.size();
R=r;U=u;V=v;C=c;
for(int i = 0; i<M; i++){
adj[U[i]].push_back({V[i],C[i]});
adj[V[i]].push_back({U[i],C[i]});
}
//init
for(int i = 0; i<N; i++){
cc.grp[i] = i;
cycles.grp[i] = i;
}
for(int i = 0; i<N; i++){
cycles.toward[i] = -1;
cycles.components[i].insert(i);
cycles.colors[i].insert(R[i]);
cycles.remaincolor[i].push(R[i]);
for(auto &j : adj[i]){
cycles.remainedge[i][j.second].push(j.first);
cycles.siz[i]++;
}
cycles.findedge(i);
while(cycles.toward[cycles.fi(i)] != -1 && cc.fi(cycles.toward[cycles.fi(i)]) == cc.fi(i)){
cycles.un(i,cycles.toward[cycles.fi(i)]);
cycles.findedge(i);
}
if(cycles.toward[cycles.fi(i)] == -1){
int tmp = cycles.components[cycles.fi(i)].size();
for(int j : cycles.components[cycles.fi(i)]) candidate.push_back({j,tmp});
ans = min(ans,tmp);
}
else{
cc.un(cycles.toward[cycles.fi(i)],i);
}
}
std::vector<int> ret(N, 0);
for(auto &[i,j] : candidate){
if(j==ans) ret[i] = 1;
}
return ret;
}
# | 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... |