1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=3e4+10,M=1e5+10,Qt=4e4+10; int n,m,q,fir[N],nxt[M<<1],son[M<<1],tot,mx[N],sz[N],F[N],top[N],id[N],dep[N],Ans[N],cnt; I void Add(CI x,CI y){nxt[++tot]=fir[x],fir[x]=tot,son[tot]=y;} #define to son[i] struct Que{int op,x,y;}Q[Qt]; #define P pair<int,int> #define MP make_pair map<P,int> mp; I void Dfs(CI x,CI fa){ RI i;for(sz[x]=1,dep[x]=dep[F[x]=fa]+1,i=fir[x];i;i=nxt[i]) !mp[MP(x,to)]&&to^fa&&!sz[to]&&(Dfs(to,x),sz[x]+=sz[to],sz[mx[x]]<sz[to]&&(mx[x]=to)); } I void Dfs2(CI x,CI Top){ if(top[x]=Top,id[x]=++cnt,mx[x]) Dfs2(mx[x],Top);RI i;for(i=fir[x];i;i=nxt[i]) !mp[MP(x,to)]&&to^mx[x]&&F[to]==x&&to^F[x]&&(Dfs2(to,to),0); } class Tree{ private: class SegmentTree{ private: struct node{int S,T;}T[N<<2]; #define mid (l+r>>1) #define PT CI x=1,CI l=1,CI r=n #define LT x<<1,l,mid #define RT x<<11,mid+1,r #define PD(x) (T[x].T&&(T[x<<1].S=T[x<<11].S=0,T[x<<1].T=T[x<<11].T=1,T[x].T=0)) #define PU(x) (T[x].S=T[x<<1].S+T[x<<11].S) public: I void B(PT){ if(l==r) return void(T[x]=(node){1,0}); B(LT),B(RT),PU(x); } I void U(CI L,CI R,PT){ if(L<=l&&r<=R) return T[x].T=1,T[x].S=0,void(); PD(x),L<=mid&&(U(L,R,LT),0),R>mid&&(U(L,R,RT),0),PU(x); } I int Q(CI L,CI R,PT){ if(L<=l&&r<=R) return T[x].S; RI S=0;return PD(x),L<=mid&&(S+=Q(L,R,LT)),R>mid&&(S+=Q(L,R,RT)),S; } }S; public: I void B(){S.B();} I void U(RI x,RI y){ W(top[x]^top[y]) dep[top[x]]<dep[top[y]]&&(swap(x,y),0),S.U(id[top[x]],id[x]),x=F[top[x]]; dep[x]<dep[y]&&(swap(x,y),0),x^y&&(S.U(id[y]+1,id[x]),0); } I int Q(RI x,RI y){ RI X=0;W(top[x]^top[y]) dep[top[x]]<dep[top[y]]&&(swap(x,y),0),X+=S.Q(id[top[x]],id[x]),x=F[top[x]]; return dep[x]<dep[y]&&(swap(x,y),0),x^y&&(X+=S.Q(id[y]+1,id[x])),X; } }T; I void B(CI x){ RI i;for(i=fir[x];i;i=nxt[i]) !mp[MP(x,to)]&&(F[to]==x&&(B(to),0),to^F[x]&&dep[to]<dep[x]&&(T.U(x,to),0)); } int main(){ RI i,op,x,y;for(read(n,m),i=1;i<=m;i++) read(x,y),Add(x,y),Add(y,x); W(read(op),~op) read(x,y),Q[++q].op=op,Q[q].x=x,Q[q].y=y,!op&&(mp[MP(Q[q].y,Q[q].x)]=mp[MP(Q[q].x,Q[q].y)]=1); for(Dfs(1,0),Dfs2(1,1),T.B(),B(1),i=q;i;i--) Q[i].op?void(Ans[i]=T.Q(Q[i].x,Q[i].y)):T.U(Q[i].x,Q[i].y); for(i=1;i<=q;i++) Q[i].op&&(writeln(Ans[i]),0);return 0; }
|