# 题解 loj #3524. 「IOI2021」钥匙

``````#include <vector>
#include "keys.h"
using namespace std;
const int N=1000005;
vector<pair<int,int> > g[N];
vector<int> e[N];
int p[N],q[N];
bool vs[N],usd[N];
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
std::vector<int> ans(r.size(), 1);
const int n=r.size();int i,j,minn=1e9,top1,top2;
for(i=0;i<u.size();++i) g[u[i]].push_back(make_pair(v[i],c[i])),g[v[i]].push_back(make_pair(u[i],c[i]));
for(i=0;i<n;++i){
for(j=0;j<n;++j) vs[j]=0,e[r[j]].clear(),usd[r[j]]=0;
top1=top2=0,q[++top2]=i,vs[i]=1,usd[r[i]]=1;
while(top1<top2){
const int top=q[++top1];
for(const auto&p : g[top]) (!usd[p.second])?e[p.second].push_back(p.first),0:(!vs[p.first]?vs[p.first]=1,q[++top2]=p.first:0);
if(!usd[r[top]]){
usd[r[top]]=1;
for(const int&p : e[r[top]]) !vs[p]?vs[p]=1,q[++top2]=p:0;
e[r[top]].clear();
}
}
for(j=0;j<n;++j) p[i]+=vs[j];
if(p[i]<minn) minn=p[i];
}
for(i=0;i<n;++i) ans[i]=(p[i]==minn);
return ans;
}
``````

(0)
(0)

© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com