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 "keys.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1e18+10;
int inf=1e9+10;
ll M=1e9+7;
int n,m;
vii a,d;
vi b,p,ke,com;
vector<set<int>> arr,keys,arr2;
void dfs(int x){
for(auto u:a[x])if(p[u]==-1){
p[u]=x;
dfs(u);
}
b.PB(x);
}
void dfs2(int x,int k){
for(auto u:arr2[x]){
arr[k].insert(u);
keys[k].insert(ke[u]);
com[u]=k;
}
for(auto u:d[x])if(p[u]==-1){
p[u]=x;
dfs2(u,k);
}
}
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();ke=r;
a.resize(n);d.resize(n);
REP(i,0,m){
if(r[u[i]]==c[i]){
a[u[i]].PB(v[i]);
d[v[i]].PB(u[i]);
}
if(r[v[i]]==c[i]){
a[v[i]].PB(u[i]);
d[u[i]].PB(v[i]);
}
}
vi ans(n,0);bool flag=0;
REP(i,0,n)if(a[i].size()==0){
ans[i]=1;
flag=1;
}
if(flag)return ans;
int k=n;int z=n;bool flag2=1;
arr2.resize(n);
REP(i,0,n)arr2[i].insert(i);
while(flag2){
p.clear();p.resize(z,-1);
b.clear();
REP(i,0,z)if(p[i]==-1){
p[i]=-2;
dfs(i);
}
reverse(b.begin(),b.end());
p.clear();p.resize(n,-1);
arr.clear();arr.resize(z);
keys.clear();keys.resize(z);
com.clear();com.resize(n);
k=0;
REP(i,0,z)if(p[b[i]]==-1){
p[b[i]]=-2;
dfs2(b[i],k++);
}
d.clear();d.resize(z);
a.clear();a.resize(z);
REP(i,0,m){
int x=com[u[i]];
int y=com[v[i]];
if(x!=y){
if(keys[x].count(c[i])==1){
a[x].PB(y);d[y].PB(x);
}
if(keys[y].count(c[i])==1){
a[y].PB(x);d[x].PB(y);
}
}
}
arr2=arr;
if(k==z)flag2=0;
z=k;
}
if(k==1){
REP(i,0,n)ans[i]=1;
return ans;
}
vi t(k,0);
REP(i,0,m){
int x=com[u[i]];
int y=com[v[i]];
if(x!=y){
if(keys[x].count(c[i])==1)t[x]=1;
if(keys[y].count(c[i])==1)t[y]=1;
}
}
int mn=inf;
vi brr;
REP(i,0,k){
if(t[i])continue;
int as=arr[i].size();
if(mn>as){
mn=as;
brr.clear();
brr.PB(i);
}
else if(mn==as)brr.PB(i);
}
ans.clear();
ans.resize(n,0);
for(auto y:brr)for(auto x:arr[y])ans[x]=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... |