#include <bits/stdc++.h> using namespace std; #define ssize(x) int(x.size()) #define pb push_back struct DSU { struct info { int siz=1; char state='0'; }; vector <int> rep; vector <info> ver; char state(int x) { if(ver[x].siz==1 && ver[x].state=='?') ver[x].state='0'; return ver[x].state; } void init(int n) { rep.clear(), ver.clear(); rep.resize(n+1), ver.resize(n+1); for(int i=1; i<=n; ++i) rep[i]=i; } int Find(int x) { if(rep[x]!=x) rep[x]=Find(rep[x]); return rep[x]; } void Union(int a, int b) { a=Find(a), b=Find(b); if(a==b) { ver[a].state='1'; return; } if(ver[a].siz<ver[b].siz) swap(a, b); rep[b]=a; ver[a].siz+=ver[b].siz; if(state(a)=='1' || state(b)=='1') ver[a].state='1'; else ver[a].state='?'; } int Add() { int x=ssize(rep); rep.pb(x), ver.pb({1, '0'}); return x; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin>>n>>q; DSU dsu; dsu.init(n); vector <int> active(n+1); for(int i=1; i<=n; ++i) active[i]=i; for(++q; --q;) { char type; cin>>type; if(type=='+') { int a, b; cin>>a>>b; dsu.Union(active[a], active[b]); } else if(type=='-') { int c; cin>>c; --dsu.ver[dsu.Find(active[c])].siz; active[c]=dsu.Add(); } else { int d; cin>>d; cout<<dsu.state(dsu.Find(active[d])); } } cout<<"\n"; exit(0); }
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 | #include <bits/stdc++.h> using namespace std; #define ssize(x) int(x.size()) #define pb push_back struct DSU { struct info { int siz=1; char state='0'; }; vector <int> rep; vector <info> ver; char state(int x) { if(ver[x].siz==1 && ver[x].state=='?') ver[x].state='0'; return ver[x].state; } void init(int n) { rep.clear(), ver.clear(); rep.resize(n+1), ver.resize(n+1); for(int i=1; i<=n; ++i) rep[i]=i; } int Find(int x) { if(rep[x]!=x) rep[x]=Find(rep[x]); return rep[x]; } void Union(int a, int b) { a=Find(a), b=Find(b); if(a==b) { ver[a].state='1'; return; } if(ver[a].siz<ver[b].siz) swap(a, b); rep[b]=a; ver[a].siz+=ver[b].siz; if(state(a)=='1' || state(b)=='1') ver[a].state='1'; else ver[a].state='?'; } int Add() { int x=ssize(rep); rep.pb(x), ver.pb({1, '0'}); return x; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin>>n>>q; DSU dsu; dsu.init(n); vector <int> active(n+1); for(int i=1; i<=n; ++i) active[i]=i; for(++q; --q;) { char type; cin>>type; if(type=='+') { int a, b; cin>>a>>b; dsu.Union(active[a], active[b]); } else if(type=='-') { int c; cin>>c; --dsu.ver[dsu.Find(active[c])].siz; active[c]=dsu.Add(); } else { int d; cin>>d; cout<<dsu.state(dsu.Find(active[d])); } } cout<<"\n"; exit(0); } |