#include <iostream>
#include <vector>
#include <set>
using namespace std;
int N, Q;
int ANS[300001];
set<int> visited;
vector<int> tab[300001];
int q1 = 0, q2 = 0, x;
string action;
void change(int s, int value, int increaser) {
int member;
for (int i = 0; i < tab[s].size(); ++i) {
member = tab[s][0];
tab[s].erase(tab[s].begin());
ANS[member] = value;
change(member, (value + increaser) % 2, increaser);
if(i < tab[s].size()) break;
}
return;
}
bool route(int s, int end){
visited.insert(s);
if(tab[s].size())
for (int i = 0; i < tab[s].size(); ++i) {
if(tab[s][i] == end){
return true;
}
else if(visited.count(tab[s][i]) == false)
return route(tab[s][i], end);
}
else return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
int a;
for (a=0; a < Q; ++a) {
cin >> action;
if (action == "+") {
cin >> q1 >> q2;
if (q1 == q2) {
if (ANS[q1] == 2) {
change(q1, 1, 1);
}
ANS[q1] = 1;
}
else {
if(ANS[q1] == 1){
ANS[q2] = 1;
change(q2, 1, 1);
}
else if(ANS[q2] == 1){
ANS[q1] = 1;
change(q1, 1, 1);
}
else{
if(ANS[q1] == 2 && ANS[q2] == 2){
visited.clear();
if(route(q2, q1)){
ANS[q1] = 1;
change(q1, 1, 0);
}
}
else{
ANS[q1] = 2;
tab[q1].push_back(q2);
ANS[q2] = 2;
tab[q2].push_back(q1);
}
}
}
}
else if (action == "-") {
cin >> q1;
if (ANS[q1] == 1) {
ANS[q1] = 0;
} else {
ANS[q1] = 0;
change(q1, 0, 1);
}
}
else if (action == "?") {
cin >> q1;
if (ANS[q1] == 2) {
cout << "?";
} else
cout << ANS[q1];
}
}
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 | #include <iostream> #include <vector> #include <set> using namespace std; int N, Q; int ANS[300001]; set<int> visited; vector<int> tab[300001]; int q1 = 0, q2 = 0, x; string action; void change(int s, int value, int increaser) { int member; for (int i = 0; i < tab[s].size(); ++i) { member = tab[s][0]; tab[s].erase(tab[s].begin()); ANS[member] = value; change(member, (value + increaser) % 2, increaser); if(i < tab[s].size()) break; } return; } bool route(int s, int end){ visited.insert(s); if(tab[s].size()) for (int i = 0; i < tab[s].size(); ++i) { if(tab[s][i] == end){ return true; } else if(visited.count(tab[s][i]) == false) return route(tab[s][i], end); } else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N >> Q; int a; for (a=0; a < Q; ++a) { cin >> action; if (action == "+") { cin >> q1 >> q2; if (q1 == q2) { if (ANS[q1] == 2) { change(q1, 1, 1); } ANS[q1] = 1; } else { if(ANS[q1] == 1){ ANS[q2] = 1; change(q2, 1, 1); } else if(ANS[q2] == 1){ ANS[q1] = 1; change(q1, 1, 1); } else{ if(ANS[q1] == 2 && ANS[q2] == 2){ visited.clear(); if(route(q2, q1)){ ANS[q1] = 1; change(q1, 1, 0); } } else{ ANS[q1] = 2; tab[q1].push_back(q2); ANS[q2] = 2; tab[q2].push_back(q1); } } } } else if (action == "-") { cin >> q1; if (ANS[q1] == 1) { ANS[q1] = 0; } else { ANS[q1] = 0; change(q1, 0, 1); } } else if (action == "?") { cin >> q1; if (ANS[q1] == 2) { cout << "?"; } else cout << ANS[q1]; } } return 0; } |
English