#include <bits/stdc++.h> using namespace std; bool odw[300001]; int stan[300001],rep[300001],ile[300001]; vector<int>pol[300001]; int find(int a){ if(rep[a]!=a)rep[a]=find(rep[a]); return rep[a]; } void onion(int a,int b){ a=find(a);b=find(b); if(ile[b]>ile[a])swap(a,b); ile[a]+=ile[b]; rep[b]=a; } void dfs(int w){ odw[w]=1; ile[w]=1; rep[w]=w; for(int i=(int)pol[w].size()-1;i>=0;--i){ if(!odw[pol[w][i]]){ stan[pol[w][i]]=1; dfs(pol[w][i]); } pol[w].pop_back(); } odw[w]=0; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,q,a,b; char c; cin>>n>>q; for(int i=1;i<=n;++i){rep[i]=i;ile[i]=1;} for(int i=0;i<q;++i){ cin>>c; if(c=='-'){ cin>>a; stan[a]=0; dfs(a); } else if(c=='+'){ cin>>a>>b; if(find(a)==find(b)){ stan[a]=1; dfs(a); } else{ if(stan[a]==1)stan[b]=1; if(stan[b]==1)stan[a]=1; if(stan[a]!=1){stan[a]=2;stan[b]=2;} onion(a,b); pol[a].emplace_back(b); pol[b].emplace_back(a); } } else{ cin>>a; if(stan[a]==2)cout<<'?'; else cout<<stan[a]; } } }
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 | #include <bits/stdc++.h> using namespace std; bool odw[300001]; int stan[300001],rep[300001],ile[300001]; vector<int>pol[300001]; int find(int a){ if(rep[a]!=a)rep[a]=find(rep[a]); return rep[a]; } void onion(int a,int b){ a=find(a);b=find(b); if(ile[b]>ile[a])swap(a,b); ile[a]+=ile[b]; rep[b]=a; } void dfs(int w){ odw[w]=1; ile[w]=1; rep[w]=w; for(int i=(int)pol[w].size()-1;i>=0;--i){ if(!odw[pol[w][i]]){ stan[pol[w][i]]=1; dfs(pol[w][i]); } pol[w].pop_back(); } odw[w]=0; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,q,a,b; char c; cin>>n>>q; for(int i=1;i<=n;++i){rep[i]=i;ile[i]=1;} for(int i=0;i<q;++i){ cin>>c; if(c=='-'){ cin>>a; stan[a]=0; dfs(a); } else if(c=='+'){ cin>>a>>b; if(find(a)==find(b)){ stan[a]=1; dfs(a); } else{ if(stan[a]==1)stan[b]=1; if(stan[b]==1)stan[a]=1; if(stan[a]!=1){stan[a]=2;stan[b]=2;} onion(a,b); pol[a].emplace_back(b); pol[b].emplace_back(a); } } else{ cin>>a; if(stan[a]==2)cout<<'?'; else cout<<stan[a]; } } } |