#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 2 * 1000 * 1000 + 7;
int newval[N], cnt;
bool sursp[N];
int fau[N], ilc[N], ill[N], il[N];
inline void C(int v)
{
newval[v] = v;
fau[v] = v; il[v] = 1;
}
int Find(int v)
{
if(fau[v] == v) return v;
Find(fau[v]);
sursp[v] |= sursp[fau[v]];
return (fau[v] = fau[fau[v]]);
}
inline void US(int a, int b)
{
int f = Find(a);
++ilc[f];
if(il[f] == ilc[f]) sursp[f] = true;
}
inline void U(int a, int b)
{
a = Find(a); b = Find(b);
//if(sursp[a]) swap(a, b);
ilc[a] += ilc[b] + 1;
ill[a] += ill[b];
il[a] += il[b];
sursp[a] |= sursp[b];
fau[b] = a;
}
inline void Union(int a, int b)
{
a = newval[a]; b = newval[b];
if(Find(a) == Find(b))
US(a, b);
else
U(a, b);
}
void Delete(int v)
{
int original = v, f;
v = newval[v];
f = Find(v);
++cnt; newval[original] = cnt;
++ill[f];
}
char Check(int v)
{
v = newval[v];
int f = Find(v);
if(sursp[v]) return '1';
if(ilc[f] == ill[f]) return '0';
return '?';
}
void Solve()
{
int n, q, a, b; char z;
string answer;
cin >> n >> q;
for(int i = 1; i <= n + q + 10; ++i)
C(i);
cnt = n;
for(int i = 1; i <= q; ++i)
{
cin >> z;
switch(z)
{
case '+':
cin >> a >> b;
Union(a, b);
break;
case '-':
cin >> a;
Delete(a);
break;
case '?':
cin >> a;
char cur = Check(a);
answer.pb(cur);
break;
}
}
cout << answer << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//int t; cin >> t;
//while(t--)
Solve();
return 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 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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 2 * 1000 * 1000 + 7; int newval[N], cnt; bool sursp[N]; int fau[N], ilc[N], ill[N], il[N]; inline void C(int v) { newval[v] = v; fau[v] = v; il[v] = 1; } int Find(int v) { if(fau[v] == v) return v; Find(fau[v]); sursp[v] |= sursp[fau[v]]; return (fau[v] = fau[fau[v]]); } inline void US(int a, int b) { int f = Find(a); ++ilc[f]; if(il[f] == ilc[f]) sursp[f] = true; } inline void U(int a, int b) { a = Find(a); b = Find(b); //if(sursp[a]) swap(a, b); ilc[a] += ilc[b] + 1; ill[a] += ill[b]; il[a] += il[b]; sursp[a] |= sursp[b]; fau[b] = a; } inline void Union(int a, int b) { a = newval[a]; b = newval[b]; if(Find(a) == Find(b)) US(a, b); else U(a, b); } void Delete(int v) { int original = v, f; v = newval[v]; f = Find(v); ++cnt; newval[original] = cnt; ++ill[f]; } char Check(int v) { v = newval[v]; int f = Find(v); if(sursp[v]) return '1'; if(ilc[f] == ill[f]) return '0'; return '?'; } void Solve() { int n, q, a, b; char z; string answer; cin >> n >> q; for(int i = 1; i <= n + q + 10; ++i) C(i); cnt = n; for(int i = 1; i <= q; ++i) { cin >> z; switch(z) { case '+': cin >> a >> b; Union(a, b); break; case '-': cin >> a; Delete(a); break; case '?': cin >> a; char cur = Check(a); answer.pb(cur); break; } } cout << answer << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); return 0; } |
English