Solution
首先这个东西显然不弱于 DAG 可达性,因此正解时间复杂度不能低于 ,然而我们不能每次询问都跑一遍 DAG 可达性,这样时间复杂度爆炸。因此我们可以考虑操作序列分块,令块长为 ,每次把关键点取出来,个数显然不超过 ,对于每次询问在所有关键点内部跑一次 bfs 时间复杂度是比较可以接受的,因此我们可以一开始先跑一遍 Tarjan 缩点 + DAG 可达性,求出每两个关键点之间的可达性,对于每次询问在内部跑一次 bfs,对于 bfs 可以用 bitset 优化,这样单次 bfs 是 。
时间复杂度 , 取 左右时是比较平衡的。
Code
//when you use vector or deque,pay attention to the size of it.
//by OldDirverTree
#include<bits/stdc++.h>
//#include<atcoder/all>
#define P pair<int,int>
#define mid (l+r>>1)
using namespace std;
//using namespace atcoder;
//using mint=modint998244353;
const int N=50001,M=1e5+1,B=224;
int n,m,q,tot,cnt,dfn[N],low[N],bel[N];
bitset<B<<1> vis,f[N],g[B<<1],g2[B<<1];
int id[N],pos[B<<1],tax[B<<1][B<<1];
struct change { int o,x,y; }C[M];
bool st[N],now[M],inC[M],ans[M];
struct edge { int x,y; }e[M];
vector<int> G[N],G2[N];
stack<int> s;
struct custom_hash
{
static uint64_t splitmix64(uint64_t x) {
x+=0x9e3779b97f4a7c15;
x=(x^(x>>30) )*0xbf58476d1ce4e5b9;
x=(x^(x>>27) )*0x94d049bb133111eb;
return x^(x>>31);
}
size_t operator() (uint64_t x) const {
static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x+FIXED_RANDOM);
}
};
int read() {
int x=0; bool _=true; char c=0;
while (!isdigit(c) ) _&=(c!='-'),c=getchar();
while (isdigit(c) ) x=x*10+(c&15),c=getchar();
return _?x:-x;
}
void tarjan(int u) {
dfn[u]=low[u]=++tot,s.push(u),st[u]=true; for (int v:G[u])
if (!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]); else if (st[v]) low[u]=min(low[u],dfn[v]);
if (dfn[u]==low[u]) { cnt++; for (int o=0;o^u;o=s.top(),s.pop(),st[o]=false,bel[o]=cnt); }
}
void solve(int l,int r)
{
int len=0;
for (int i=l;i<r;i++) {
auto add=[&](int x){ if (!~id[x]) pos[len]=x,id[x]=len++; };
if (C[i].o&1) add(e[C[i].x].x),add(e[C[i].x].y),inC[C[i].x]=true;
else add(C[i].x),add(C[i].y);
}
for (int i=1;i<=m;i++) if (now[i]&&!inC[i]) G[e[i].x].push_back(e[i].y);
for (int i=1;i<=n;i++) if (!dfn[i]) tarjan(i);
for (int x=1;x<=n;x++) for (int y:G[x]) if (bel[x]^bel[y]) G2[bel[x] ].push_back(bel[y]);
for (int i=0;i<len;i++) f[bel[pos[i] ] ][i]=1;
for (int x=1;x<=cnt;x++) for (int y:G2[x]) f[x]=f[x]|f[y];
for (int x=0;x<len;x++) for (int y=0;y<len;y++) g[x][y]=f[bel[pos[x] ] ][y];
for (int i=1;i<=m;i++) if (now[i]&&inC[i])
{ int x=id[e[i].x],y=id[e[i].y]; tax[x][y]++,g2[x][y]=1; }
for (int i=l;i<r;i++)
{
if (C[i].o&1) {
now[C[i].x]^=1; int x=id[e[C[i].x].x],y=id[e[C[i].x].y];
now[C[i].x]?tax[x][y]++:tax[x][y]--; g2[x][y]=tax[x][y];
}
else
{
vis.set(),vis[id[C[i].x] ]=false;
queue<int> q; q.push(id[C[i].x]);
while (!q.empty() ) {
int u=q.front(); q.pop(); auto tmp=vis&(g[u]|g2[u]);
for (int v=tmp._Find_first();v<len;v=tmp._Find_next(v) )
vis[v]=false,q.push(v);
}
ans[i]=!vis[id[C[i].y] ];
}
}
for (int i=1;i<=m;i++) inC[i]=false;
for (int i=1;i<=cnt;i++) f[i].reset(),G2[i].clear();
for (int i=0;i<len;i++) g[i].reset(),g2[i].reset(),id[pos[i] ]=-1;
for (int i=1;i<=n;i++) G[i].clear(),dfn[i]=0;
memset(tax,0,sizeof tax),tot=cnt=0;
}
main()
{
n=read(),m=read(),q=read();
for (int i=1;i<=m;i++)
e[i]={read(),read()},now[i]=true;
for (int i=0;i<q;i++) {
C[i].o=read(),C[i].x=read();
if (C[i].o&2) C[i].y=read();
}
for (int i=1;i<=n;i++) id[i]=-1;
for (int i=0;i<q;i+=B) solve(i,min(i+B,q) );
for (int i=0;i<q;i++) if (C[i].o&2) puts(ans[i]?"YES":"NO");
return 0;
}