QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#296912 | #4000. Dynamic Reachability | rageOfThunder | TL | 0ms | 11764kb | C++14 | 4.5kb | 2024-01-03 19:40:27 | 2024-01-03 19:40:27 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
void cmax(int &x,int v){x=max(x,v);}
void cmin(int &x,int v){x=min(x,v);}
#define ull unsigned long long
const int N=1e5+5;
pair<int,int>E[N];
int n,m,q;
bool inG[N],vG[N];
namespace Tarjan{
vector<int>G[N];
int low[N],dfn[N],dfc,stk[N],top,scc[N],sc;
bool ins[N];
void dfs(int u){
// cout<<"dfs "<<u<<endl;
stk[++top]=u,dfn[u]=low[u]=++dfc;
for(int v:G[u]){
if(!dfn[v])dfs(v),cmin(low[u],low[v]);
else if(ins[v])cmin(low[u],dfn[v]);
}
if(dfn[u]!=low[u])return ;
scc[u]=++sc,ins[u]=0;
while(stk[top]!=u)scc[stk[top]]=sc,ins[stk[top]]=0,top--;top--;
}
vector<int>G2[N];
vector<ull>getF(vector<int>nodes){
// puts("Tarjan");
int V=nodes.size();assert(V<=63);
vector<ull>ans(n+1),f(V);
for(int i=1;i<=m;i++)if(inG[i]&&(!vG[i])){
auto [u,v]=E[i];
// cout<<"addedge : "<<u<<" -> "<<v<<endl;
G[u].emplace_back(v);
}
for(int i=1;i<=n;i++)if(!dfn[i])dfs(i);
// cout<<"scc = ";for(int i=1;i<=n;i++)cout<<scc[i]<<" ";puts("");
vector<int>oud(sc+1);
queue<int>Q;
for(int i=1;i<=m;i++)if(inG[i]&&(!vG[i])){
auto [u,v]=E[i];
u=scc[u],v=scc[v];
if(u!=v)G2[v].emplace_back(u),oud[u]++;
}
for(int i=0;i<V;i++){
int u=nodes[i];
ans[scc[u]]|=(1ull<<i);
}
for(int i=1;i<=sc;i++)if(oud[i]==0)Q.push(i);
while(Q.size()){
int x=Q.front();Q.pop();
// cout<<"x = "<<x<<" ans = "<<ans[x]<<endl;
for(int y:G2[x]){
ans[y]|=ans[x];
if(--oud[y]==0)Q.push(y);
}
}
for(int i=0;i<V;i++)f[i]=ans[scc[nodes[i]]];
// cout<<"f = ";for(int i=0;i<V;i++)cout<<f[i]<<" ";puts("");
for(int i=1;i<=n;i++)G[i].clear(),dfn[i]=low[i]=0,G2[i].clear();
dfc=top=sc=0;
return f;
}
}
struct Q{int op,x,y;}qr[N];
int id[N];
bool Res[N];
void solve(int l,int r){
// cout<<"solve "<<l<<" "<<r<<endl;
vector<int>nodes;
vector<int>edges;
int ncnt=0;
for(int i=l;i<=r;i++){
if(qr[i].op==1){
int p=qr[i].x;
auto [u,v]=E[p];
if(id[u]==-1)id[u]=ncnt++,nodes.emplace_back(u);
if(id[v]==-1)id[v]=ncnt++,nodes.emplace_back(v);
if(!vG[p])vG[p]=1,edges.emplace_back(p);
}
if(qr[i].op==2){
auto [GENSHINIMPACT,u,v]=qr[i];
if(id[u]==-1)id[u]=ncnt++,nodes.emplace_back(u);
if(id[v]==-1)id[v]=ncnt++,nodes.emplace_back(v);
}
}
// cout<<"nodes = ";for(int x:nodes)cout<<x<<" ";puts("");
// cout<<"edges = ";for(int p:edges)cout<<"("<<E[p].fi<<" -> "<<E[p].se<<") ";puts("");
auto f=Tarjan::getF(nodes);
auto tmpf=f;
ull vis=0,U=(1ull<<ncnt)-1;
// cout<<"U = "<<U<<endl;
function<void(int)>dfs=[&](int u){
// cout<<"dfs "<<u<<endl;
vis|=(1ull<<u);
// cout<<"vis = "<<vis<<endl;
ull to=((U^vis)&f[u]);
// cout<<"to = "<<to<<endl;
while(to!=0){
dfs(__builtin_ctz(to));
to=((U^vis)&f[u]);
// cout<<" u = "<<u<<" to = "<<to<<endl;
}
};
for(int i=l;i<=r;i++){
if(qr[i].op==1){
int p=qr[i].x;
inG[p]^=1;
}
else{
auto [GENSHINIMPACT,st,ed]=qr[i];
for(auto e:edges){
if(!inG[e])continue;
auto [u,v]=E[e];
// cout<<"addedge "<<u<<" -> "<<v<<endl;
// cout<<"id = "<<id[u]<<" "<<id[v]<<endl;
assert(id[u]!=-1&&id[v]!=-1);
u=id[u],v=id[v];
f[u]|=(1ull<<v);
}
st=id[st],ed=id[ed];
vis=0,dfs(st);
if(vis&(1ull<<ed))Res[i]=true;
else Res[i]=false;
f=tmpf;
}
}
for(auto p:edges)vG[p]=0;
for(auto u:nodes)id[u]=-1;
}
signed main(void){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
memset(id,-1,sizeof(id));
n=read(),m=read(),q=read();
for(int i=1;i<=m;i++)E[i].fi=read(),E[i].se=read(),inG[i]=1;
for(int i=1;i<=q;i++){
qr[i].op=read();
if(qr[i].op==1)qr[i].x=read();
else qr[i].x=read(),qr[i].y=read();
}
vector<int>vis(n+1,0),nodes;
int lst=1,ncnt=0;
for(int i=1;i<=q;i++){
auto [op,x,y]=qr[i];
if(op==1){
auto [u,v]=E[x];
if(!vis[u])vis[u]=1,nodes.emplace_back(u),ncnt++;
if(!vis[v])vis[v]=1,nodes.emplace_back(v),ncnt++;
}
else{
if(!vis[x])vis[x]=1,nodes.emplace_back(x),ncnt++;
if(!vis[y])vis[y]=1,nodes.emplace_back(y),ncnt++;
}
if(ncnt>=62){
solve(lst,i);
lst=i+1,ncnt=0;
for(int x:nodes)vis[x]=0;nodes.clear();
}
}
solve(lst,q);
for(int i=1;i<=q;i++)if(qr[i].op==2)cout<<(Res[i]?"YES":"NO")<<'\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11764kb
input:
5 6 7 1 2 1 3 2 4 3 4 3 5 4 5 2 1 5 2 2 3 1 3 1 4 2 1 4 1 3 2 1 5
output:
YES NO NO YES
result:
ok 4 lines
Test #2:
score: -100
Time Limit Exceeded
input:
50000 100000 100000 36671 44121 25592 44321 13226 46463 13060 25694 14021 20087 22881 38333 34655 47774 22868 26462 31154 48710 27491 32365 5874 47497 17622 28600 1886 14193 22315 23656 14973 22704 1335 25384 22612 34915 2852 48213 23334 25519 24342 28784 6238 36125 14598 39494 33069 34250 2123 3059...