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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } using Pir=std::pair<int,int>; const int MAXN=2e5+10; int N,Q,Idx; struct Edges { int fr,to; bool operator<(Edges x){ return fr==x.fr?to<x.to:fr<x.fr; } }Ed[MAXN],Edge[MAXN]; std::map<int,int>Mp[MAXN],Id[MAXN]; bool Cut[MAXN],ans[MAXN]; std::vector<int>val[MAXN<<2]; #define ls (p<<1) #define rs (p<<1|1) void modifyCover(int p,int l,int r,int ql,int qr,int v) { if(ql<=l&&r<=qr) return val[p].push_back(v),void(); int mid=(l+r)>>1; if(ql<=mid) modifyCover(ls,l,mid,ql,qr,v); if(mid<qr) modifyCover(rs,mid+1,r,ql,qr,v); } struct Dsu { int Rt[MAXN],Siz[MAXN],Stk[MAXN],Top; inline void init(int n){ for(int i=1;i<=n;++i) Rt[i]=i,Siz[i]=1; } inline int getRt(int x) { while(x!=Rt[x]) x=Rt[x]; return x; } inline void merge(int u,int v) { int p=getRt(u),q=getRt(v); if(p==q) return ; if(Siz[p]<Siz[q]) std::swap(p,q); Siz[p]+=Siz[q],Rt[q]=p; Stk[++Top]=q; } inline void back() { int q=Stk[Top--]; Siz[Rt[q]]-=Siz[q],Rt[q]=q; } inline void back(int x){ while(Top>x) back(); } inline bool connect(int u,int v){ return (getRt(u)==getRt(v)); } }Dsu; void Divide_Conquer(int p,int l,int r,bool ok) { int Tim=Dsu.Top; if(ok) for(int x:val[p]) { auto e=Edge[x]; Dsu.merge(e.fr,e.to+N),Dsu.merge(e.to,e.fr+N); if(Dsu.connect(e.fr,e.fr+N)||Dsu.connect(e.to,e.to+N)) { ok=0; break; } } int mid=(l+r)>>1; if(l==r) return ans[l]=ok,void(); Divide_Conquer(ls,l,mid,ok),Divide_Conquer(rs,mid+1,r,ok); Dsu.back(Tim); } int main() { read(N,Q); Dsu.init(N+N+1); for(int i=1;i<=Q;++i) { read(Ed[i].fr,Ed[i].to); if(!Id[Ed[i].fr][Ed[i].to]) { Edge[++Idx]=(Edges){Ed[i].fr,Ed[i].to}; Id[Ed[i].fr][Ed[i].to]=Idx; } } for(int i=1;i<=Q;++i) { auto e=Ed[i]; int id=Id[e.fr][e.to]; if(Cut[id]) { modifyCover(1,1,Q,Mp[e.fr][e.to],i-1,id); } else Mp[e.fr][e.to]=i; Cut[id]^=1; } for(int i=1;i<=Idx;++i) if(Cut[Id[Edge[i].fr][Edge[i].to]]) { modifyCover(1,1,Q,Mp[Edge[i].fr][Edge[i].to],Q,i); } Divide_Conquer(1,1,Q,1); for(int i=1;i<=Q;++i) puts(ans[i]?"YES":"NO"); return 0; }
|