#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
void reakcja_lancuchowa(int indeks, vector<int> T[], vector<bool>& odwiedzone) {
queue<int> kolejka;
kolejka.push(indeks);
while (!kolejka.empty()) {
int aktualny = kolejka.front();
kolejka.pop();
if (!odwiedzone[aktualny]) {
odwiedzone[aktualny] = true;
vector<int> pomVector = T[aktualny];
T[aktualny].clear();
T[aktualny].push_back(aktualny);
for (const auto &element : pomVector) {
if (!odwiedzone[element]) {
kolejka.push(element);
}
}
}
}
}
void traci_laptopa(int indeks, vector<int> T[], vector<bool>& odwiedzone){
reakcja_lancuchowa(indeks,T,odwiedzone);
T[indeks].clear();
odwiedzone[indeks] = false;
}
string maKomputer(int indeks, vector<int> vec){
if(vec.empty())
return "0";
if(vec[0] == indeks)
return "1";
return "?";
}
int main() {
int n, p ;
cin>>n>>p;
vector<int> T[n];
vector<bool> odwiedzone(n, false);
for (int i = 0; i<p; i++) {
char znak;
cin >>znak;
switch (znak) {
case '+':
int a, b;
cin>>a>>b;
if(a == b)
reakcja_lancuchowa(a,T,odwiedzone);
if(T[a].end() != find(T[a].begin(), T[a].end(), b)){
reakcja_lancuchowa(a,T,odwiedzone);
}
if(T[b].end() != find(T[b].begin(), T[b].end(), a)){
reakcja_lancuchowa(b,T,odwiedzone);
}
T[a].push_back(b);
T[b].push_back(a);
break;
case '-':
int c;
cin>>c;
traci_laptopa(c,T,odwiedzone);
break;
case '?':
int d;
cin >>d;
cout << maKomputer(d,T[d]);
}
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; void reakcja_lancuchowa(int indeks, vector<int> T[], vector<bool>& odwiedzone) { queue<int> kolejka; kolejka.push(indeks); while (!kolejka.empty()) { int aktualny = kolejka.front(); kolejka.pop(); if (!odwiedzone[aktualny]) { odwiedzone[aktualny] = true; vector<int> pomVector = T[aktualny]; T[aktualny].clear(); T[aktualny].push_back(aktualny); for (const auto &element : pomVector) { if (!odwiedzone[element]) { kolejka.push(element); } } } } } void traci_laptopa(int indeks, vector<int> T[], vector<bool>& odwiedzone){ reakcja_lancuchowa(indeks,T,odwiedzone); T[indeks].clear(); odwiedzone[indeks] = false; } string maKomputer(int indeks, vector<int> vec){ if(vec.empty()) return "0"; if(vec[0] == indeks) return "1"; return "?"; } int main() { int n, p ; cin>>n>>p; vector<int> T[n]; vector<bool> odwiedzone(n, false); for (int i = 0; i<p; i++) { char znak; cin >>znak; switch (znak) { case '+': int a, b; cin>>a>>b; if(a == b) reakcja_lancuchowa(a,T,odwiedzone); if(T[a].end() != find(T[a].begin(), T[a].end(), b)){ reakcja_lancuchowa(a,T,odwiedzone); } if(T[b].end() != find(T[b].begin(), T[b].end(), a)){ reakcja_lancuchowa(b,T,odwiedzone); } T[a].push_back(b); T[b].push_back(a); break; case '-': int c; cin>>c; traci_laptopa(c,T,odwiedzone); break; case '?': int d; cin >>d; cout << maKomputer(d,T[d]); } } return 0; } |
English