#include <bits/stdc++.h> using namespace std; //#define int long long constexpr int maxN = 3e5+7; int n,q,lnk[maxN],sz[maxN],stan[maxN]; // 0 - nie ma kompa, 1 - nie wiadomo, 2 - ma kompa bool vis[maxN]; vector<int>v[maxN]; void pre(){ for(int i=1;i<=n;i++){ sz[i] = 1; lnk[i] = i; } } int Find(int x){ if(lnk[x] == x) return x; lnk[x] = Find(lnk[x]); return lnk[x]; } void unite(int a, int b){ a = Find(a), b = Find(b); if(a == b) return; v[a].push_back(b); v[b].push_back(a); if(sz[a] < sz[b]) swap(a,b); lnk[b] = a; sz[a] += sz[b]; } bool same(int a, int b){ return Find(a) == Find(b); } void dfs_comp(int x){ vis[x] = true; if(stan[x] != 0) stan[x] = 2; for(int a:v[x]) if(!vis[a]) dfs_comp(a); } int licznik_1; void cnt_1(int x){ vis[x] = true; if(stan[x] == 1) licznik_1++; for(int a:v[x]) if(!vis[a]) cnt_1(a); } void clean(){ for(int i=1;i<=n;i++) vis[i] = false; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>q; pre(); for(int i=1;i<=q;i++){ char type; cin>>type; if(type == '+'){ int a,b; cin>>a>>b; if(a == b){ stan[a] = 2; dfs_comp(a); clean(); continue; } if(stan[a] > stan[b]) swap(a,b); if(stan[a] == 0 && stan[b] <= 1){ unite(a,b); stan[a] = 1, stan[b] = 1; } else if(stan[a] == 1 && stan[b] == 1){ if(same(a,b)){ dfs_comp(a); clean(); } else unite(a,b); } else if(stan[b] == 2){ stan[a] = 2; unite(a,b); dfs_comp(a); clean(); } } else if(type == '-'){ int x; cin>>x; stan[x] = 0; licznik_1 = 0; cnt_1(x); clean(); } else{ int x; cin>>x; if(stan[x] == 0) cout<<0; else if(stan[x] == 2) cout<<1; else cout<<'?'; } /* cout<<" to ejst stan\n"; for(int j=1;j<=n;j++) cout<<stan[j]<<' '; cout<<"\n"; */ } cout<<"\n"; } /* 5 11 ? 1 + 1 2 + 2 3 ? 2 + 3 1 - 2 ? 1 ? 2 ? 3 + 2 2 ? 2 */
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 | #include <bits/stdc++.h> using namespace std; //#define int long long constexpr int maxN = 3e5+7; int n,q,lnk[maxN],sz[maxN],stan[maxN]; // 0 - nie ma kompa, 1 - nie wiadomo, 2 - ma kompa bool vis[maxN]; vector<int>v[maxN]; void pre(){ for(int i=1;i<=n;i++){ sz[i] = 1; lnk[i] = i; } } int Find(int x){ if(lnk[x] == x) return x; lnk[x] = Find(lnk[x]); return lnk[x]; } void unite(int a, int b){ a = Find(a), b = Find(b); if(a == b) return; v[a].push_back(b); v[b].push_back(a); if(sz[a] < sz[b]) swap(a,b); lnk[b] = a; sz[a] += sz[b]; } bool same(int a, int b){ return Find(a) == Find(b); } void dfs_comp(int x){ vis[x] = true; if(stan[x] != 0) stan[x] = 2; for(int a:v[x]) if(!vis[a]) dfs_comp(a); } int licznik_1; void cnt_1(int x){ vis[x] = true; if(stan[x] == 1) licznik_1++; for(int a:v[x]) if(!vis[a]) cnt_1(a); } void clean(){ for(int i=1;i<=n;i++) vis[i] = false; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>q; pre(); for(int i=1;i<=q;i++){ char type; cin>>type; if(type == '+'){ int a,b; cin>>a>>b; if(a == b){ stan[a] = 2; dfs_comp(a); clean(); continue; } if(stan[a] > stan[b]) swap(a,b); if(stan[a] == 0 && stan[b] <= 1){ unite(a,b); stan[a] = 1, stan[b] = 1; } else if(stan[a] == 1 && stan[b] == 1){ if(same(a,b)){ dfs_comp(a); clean(); } else unite(a,b); } else if(stan[b] == 2){ stan[a] = 2; unite(a,b); dfs_comp(a); clean(); } } else if(type == '-'){ int x; cin>>x; stan[x] = 0; licznik_1 = 0; cnt_1(x); clean(); } else{ int x; cin>>x; if(stan[x] == 0) cout<<0; else if(stan[x] == 2) cout<<1; else cout<<'?'; } /* cout<<" to ejst stan\n"; for(int j=1;j<=n;j++) cout<<stan[j]<<' '; cout<<"\n"; */ } cout<<"\n"; } /* 5 11 ? 1 + 1 2 + 2 3 ? 2 + 3 1 - 2 ? 1 ? 2 ? 3 + 2 2 ? 2 */ |