이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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, vector<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_back(i.second.front());
i.second.pop_back();
}
}
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();
for(auto j: remainedge[x][i]){
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_back(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... |