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>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("O3")
using namespace std;
using namespace __gnu_pbds;
// typedef int int;
typedef pair<int,int>pairll;
#define N 150007
#define pb push_back
#define fr first
#define sc second
// #pragma GCC optimize("O3")
// #pragma GCC optimze("unroll-loops")
// #pragma GCC target("avx2")
int ans[2*N];
int p[2*N],f[2*N],n,m,d[2*N],sz[2*N];
bool fr[2*N];
set<pair<int,int>> v[2*N];
vector<int>v2[2*N];
vector<pairll> v1[2*N];
set<int> s[2*N],del[2*N],a[2*N];
int P(int x){
return p[x] == x ? x : p[x] = P(p[x]);
}
void A(int x,int y){
x=P(x);
y=P(y);
if(x == y) return;
p[y] = x;
if(v2[x].size()<v2[y].size()) swap(v2[x],v2[y]);
for(auto& val : v2[y]) v2[x].push_back(val);
if(v1[x].size()<v1[y].size()){
swap(v1[x],v1[y]);
}
for(auto& val : v1[y]) v1[x].push_back(val);
if(v[x].size()<v[y].size()){
swap(s[x],s[y]);
swap(v[x],v[y]);
swap(a[x],a[y]);
}
for(auto&[col,to] : v[y]){
if(s[x].count(col)){
v2[x].push_back(to);
// auto it = v[y].lower_bound(make_pair(col,-1));
// while(it != v[y].end()){
// if(it->first != col) break;
// v2[x].push_back(it->second);
// it++;
// }
// for(auto& val : v[y][it]) v2[x].push_back(val);
}else if(s[y].count(col)){
auto it = v[x].lower_bound(make_pair(col,-1));
while(it != v[x].end()){
if(it->first != col) break;
v2[x].push_back(it->second);
it++;
}
// for(auto& val : v[x][it]) v2[x].push_back(val);
s[x].insert(col);
}else{
v[x].insert(make_pair(col,to));
// if(v[y][it].size()>v[x][it].size()) swap(v[x][it],v[y][it]);
// for(auto& val : v[y][it]) v[x][it].push_back(val);
}
// a[x].insert(it);
}
sz[x]+=sz[y];
return ;
}
int S(int x,int y){
x=P(x);
if(f[x]==2)return x;
if(f[x]==1)return 0;
f[x]=2;
while(v2[x].size()>0){
int m1=v2[x].back();
v2[x].pop_back();
if(P(m1)!=P(x)){
int g=S(m1,x);
if(g!=0){
if(x==g){
x=P(x);
}else{
A(x,y);
return g;
}
}
}
}
f[x]=1;
return 0;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> w, vector<int> c) {
n=r.size();
for(int i=1;i<=n;i++){
d[i]=r[i-1];
p[i]=i;
s[i].insert(d[i]);
sz[i]=1;
}
m=u.size();
for(int i=0;i<m;i++){
u[i]++;
w[i]++;
v[u[i]].insert({c[i],w[i]});
v[w[i]].insert({c[i],u[i]});
v1[u[i]].emplace_back(c[i],w[i]);
v1[w[i]].emplace_back(c[i],u[i]);
if(c[i]==d[u[i]]){
v2[u[i]].pb(w[i]);
}
if(c[i]==d[w[i]]){
v2[w[i]].pb(u[i]);
}
a[u[i]].insert(c[i]);
a[w[i]].insert(c[i]);
}
for(int i=1;i<=n;i++){
if(f[i]==0) S(i,i);
}
vector<int> values[2*N];
for(int i=1;i<=n;i++){
s[i].clear();
}
for(int i = 1; i <= n; i++){
values[P(i)].push_back(d[i]);
}
for(int i = 1; i <= n; i++){
sort(values[i].begin(),values[i].end());
values[i].erase(unique(values[i].begin(),values[i].end()),values[i].end());
}
int mn=n;
for(int i=1;i<=n;i++){
if(p[i]!=i) continue;
for(auto& it:v1[i]){
auto itt = lower_bound(values[i].begin(),values[i].end(),it.first);
bool have = (itt != values[i].end() && *itt == it.first);
if(have && P(it.sc)!=i){
fr[i]=1;
break;
}
}
if(fr[i]==0) mn=min(mn,sz[i]);
//cout<<i<<" "<<P(i)<<endl;
}
for(int i=1;i<=n;i++){
if(fr[P(i)]==0 && sz[P(i)]==mn) ans[i-1]=1;
}
return vector<int>(ans,ans+n);
}
# | 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... |