//Jakub Nowak XIV LO Wroclaw
#include<bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#define vi vector<int>
#define pb push_back
#define st first
#define nd second
/*void wypisz(auto &X) {
for(auto &x : X) cout << x << " ";
cout << "\n";
}*/
struct union_find{
vi PAR = vi(1, 0);
vi ILE_LUDA = vi(1, 0);
vi ILE_KOMPA = vi(1, 0);
void add() {
PAR.pb(PAR.size());
ILE_LUDA.pb(1);
ILE_KOMPA.pb(0);
}
int find(int a) {
if(a == PAR[a]) return a;
PAR[a] = find(PAR[a]);
return PAR[a];
}
void uni(int a, int b) {
a = find(a);
b = find(b);
if(a == b) {
ILE_KOMPA[a]++;
return;
}
if(ILE_LUDA[b] > ILE_LUDA[a]) swap(a, b);
PAR[b] = a;
ILE_LUDA[a] = ILE_LUDA[a] + ILE_LUDA[b];
ILE_KOMPA[a] = ILE_KOMPA[a] + ILE_KOMPA[b] + 1;
}
void remove(int a) {
a = find(a);
ILE_LUDA[a]--;
ILE_KOMPA[a]--;
}
};
void solve() {
int n, q;
vi KTORY_MOJ(1, 0);
union_find U;
cin >> n >> q;
for(int i=1; i<=n; i++) {
U.add();
KTORY_MOJ.pb(U.PAR.size()-1);
}
for(int i=1; i<=q; i++) {
char c;
cin >> c;
switch(c) {
case '+' : {
int a, b;
cin >> a >> b;
U.uni(KTORY_MOJ[a], KTORY_MOJ[b]);
break;
}
case '-' : {
int a;
cin >> a;
U.remove(KTORY_MOJ[a]);
U.add();
KTORY_MOJ[a] = U.PAR.size()-1;
break;
}
case '?': {
int a;
cin >> a;
a = U.find(KTORY_MOJ[a]);
if(U.ILE_KOMPA[a] == 0) {//jesli komkpa jest 0, to na pewno nie
cout << 0;
break;
}
if(U.ILE_LUDA[a] == U.ILE_KOMPA[a]) {//jesli luda jest tyle co kompa to na pewno tak
cout << 1;
break;
}
cout << "?"; //jesli 0 < kompa < luda to nwm
break;
}
}
//cout << 4;
/*if(c == '+') {
int a, b;
cin >> a >> b;
//cout << 1;
}
if(c == '-') {
int a;
cin >> a;
//cout << 2;
}
if(c == '?') {
int a;
cin >> a;
//cout << 3;
}*/
}
}
int32_t main() {
boost
solve();
}
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 | //Jakub Nowak XIV LO Wroclaw #include<bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); #define int long long #define vi vector<int> #define pb push_back #define st first #define nd second /*void wypisz(auto &X) { for(auto &x : X) cout << x << " "; cout << "\n"; }*/ struct union_find{ vi PAR = vi(1, 0); vi ILE_LUDA = vi(1, 0); vi ILE_KOMPA = vi(1, 0); void add() { PAR.pb(PAR.size()); ILE_LUDA.pb(1); ILE_KOMPA.pb(0); } int find(int a) { if(a == PAR[a]) return a; PAR[a] = find(PAR[a]); return PAR[a]; } void uni(int a, int b) { a = find(a); b = find(b); if(a == b) { ILE_KOMPA[a]++; return; } if(ILE_LUDA[b] > ILE_LUDA[a]) swap(a, b); PAR[b] = a; ILE_LUDA[a] = ILE_LUDA[a] + ILE_LUDA[b]; ILE_KOMPA[a] = ILE_KOMPA[a] + ILE_KOMPA[b] + 1; } void remove(int a) { a = find(a); ILE_LUDA[a]--; ILE_KOMPA[a]--; } }; void solve() { int n, q; vi KTORY_MOJ(1, 0); union_find U; cin >> n >> q; for(int i=1; i<=n; i++) { U.add(); KTORY_MOJ.pb(U.PAR.size()-1); } for(int i=1; i<=q; i++) { char c; cin >> c; switch(c) { case '+' : { int a, b; cin >> a >> b; U.uni(KTORY_MOJ[a], KTORY_MOJ[b]); break; } case '-' : { int a; cin >> a; U.remove(KTORY_MOJ[a]); U.add(); KTORY_MOJ[a] = U.PAR.size()-1; break; } case '?': { int a; cin >> a; a = U.find(KTORY_MOJ[a]); if(U.ILE_KOMPA[a] == 0) {//jesli komkpa jest 0, to na pewno nie cout << 0; break; } if(U.ILE_LUDA[a] == U.ILE_KOMPA[a]) {//jesli luda jest tyle co kompa to na pewno tak cout << 1; break; } cout << "?"; //jesli 0 < kompa < luda to nwm break; } } //cout << 4; /*if(c == '+') { int a, b; cin >> a >> b; //cout << 1; } if(c == '-') { int a; cin >> a; //cout << 2; } if(c == '?') { int a; cin >> a; //cout << 3; }*/ } } int32_t main() { boost solve(); } |
English