QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#131572 | #4000. Dynamic Reachability | kkio | RE | 0ms | 10072kb | C++14 | 2.9kb | 2023-07-27 17:21:18 | 2023-07-27 17:21:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn=5005+10,maxm=2e5+10,K=45;
int n,m,q,qid,pid;
struct edg{int u,v,c;}e[maxm];
struct qryy{int t,op,u,v;}opt[maxm];
struct edge{int to,next;}E[maxm];
int head[maxn],tot;
inline void add(int u,int v)
{
E[++tot]={v,head[u]};
head[u]=tot;
}
int dfn[maxn],times,low[maxn],stk[maxn],top;
bool ins[maxn];
int scc[maxn],nid;
void tarjan(int u)
{
dfn[u]=low[u]=++times;
ins[u]=1;stk[++top]=u;
for(int i=head[u];i;i=E[i].next)
{
int v=E[i].to;
if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}
else if(ins[v]){low[u]=min(low[u],dfn[v]);}
}
if(low[u]==dfn[u])
{
++nid;int x=0;
while(x!=u)
{
x=stk[top];top--;
ins[x]=0;scc[x]=nid;
}
}
}
bool tag[maxm];
int deg[maxn];
bitset<50005> f[maxn];
vector<int> G[maxn],F[maxn],N[maxn];
void work()
{
times=0;for(int i=1;i<=n;i++)dfn[i]=low[i]=scc[i]=0;nid=0;
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=nid;i++)f[i].reset(),f[i].set(i),deg[i]=0,G[i].clear();
for(int i=1;i<=m;i++)
{
if(tag[i]||!e[i].c)continue;
int u=e[i].u,v=e[i].v;
if(scc[u]==scc[v])continue;
G[scc[v]].push_back(scc[u]);deg[scc[u]]++;
}
queue<int> q;
for(int i=1;i<=nid;i++)if(!deg[i])q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for(int v:G[u])
{
f[v]|=f[u];
deg[v]--;
if(!deg[v])q.push(v);
}
}
}
vector<int> Ip,Ie;
bool vis[maxn];
bool check(int s,int t)
{
queue<int> q;
q.push(s);
for(int v:Ip)vis[v]=0;
while(!q.empty())
{
int u=q.front();q.pop();
if(u==t)return 1;
if(vis[u])continue;
vis[u]=1;
for(int v:F[u])
q.push(v);
for(int v:N[u])
q.push(v);
}
return 0;
}
void solve(int L,int R)
{
Ip.clear();Ie.clear();
for(int i=L;i<=R;i++)
{
if(opt[i].op==2)
{
Ip.push_back(opt[i].u);
Ip.push_back(opt[i].v);
}
int eg=opt[i].u;
tag[eg]=1;
Ip.push_back(e[eg].u);
Ip.push_back(e[eg].v);
Ie.push_back(eg);
}
sort(Ip.begin(),Ip.end());Ip.resize(unique(Ip.begin(),Ip.end())-Ip.begin());
sort(Ie.begin(),Ie.end());Ie.resize(unique(Ie.begin(),Ie.end())-Ie.begin());
for(int i=1;i<=n;i++)head[i]=0;tot=0;
for(int i=1;i<=m;i++)if(!tag[i]&&e[i].c)add(e[i].u,e[i].v);
work();
for(int v:Ip)F[v].clear();
for(int v:Ip)for(int r:Ip)
if(v!=r&&(scc[v]==scc[r]||f[scc[v]].test(scc[r])))
F[v].push_back(r);
for(int i=L;i<=R;i++)
{
if(opt[i].op==1)
{
int eg=opt[i].u;
tag[eg]=0;
e[eg].c^=1;
}
else
{
for(int v:Ip)N[v].clear();
for(int ep:Ie)
if(e[ep].c)
N[e[ep].u].push_back(e[ep].v);
if(check(opt[i].u,opt[i].v))puts("YES");
else puts("NO");
}
}
}
int main()
{
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=m;i++)
cin>>e[i].u>>e[i].v,e[i].c=1;
for(int i=1;i<=q;i++)
{
cin>>opt[i].op>>opt[i].u;
if(opt[i].op==2)cin>>opt[i].v;
}
for(int i=1;i<=q;i+=K)solve(i,min(q,i+K-1));
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10072kb
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
Runtime Error
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...