#include<iostream> #include<algorithm> #include<stack> using namespace std; const int ILE = 1000000; int t[ILE + 1], rozne, komp, pewne; stack <int> s; void tablica(int l1, int l2){ if(l1 == l2){ rozne-=t[l1]; t[l1] = 2; pewne++; } else if(t[l1] == 2) { rozne-=t[l2]; pewne++; t[l2] = 2; } else if(t[l2] == 2){ rozne-=t[l1]; pewne++; t[l1] = 2; } if(t[l1] == 0){ rozne++; s.push(l1); t[l1]= 1; } if(t[l2] == 0){ rozne++; s.push(l2); t[l2] = 1; } } int main(){ ios::sync_with_stdio(0); int n, m, l1, l2; char znak; cin >> n >> m; for(int i = 0; i < m; i++){ cin >> znak; if(znak == '+'){ cin >> l1 >> l2; komp++; tablica(l1, l2); } else if(znak == '-'){ cin >> l1; if(t[l1] == 2 ) pewne--; if(t[l1] == 1) rozne--; komp--; t[l1] = 0; } else { cin >> l1; if(t[l1]== 2) cout << 1; else if(t[l1] == 1) cout << '?'; else cout << t[l1]; } if(rozne == komp-pewne) { while(!s.empty()){ t[s.top()] = 2; s.pop(); rozne--; pewne++; } } } }
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 | #include<iostream> #include<algorithm> #include<stack> using namespace std; const int ILE = 1000000; int t[ILE + 1], rozne, komp, pewne; stack <int> s; void tablica(int l1, int l2){ if(l1 == l2){ rozne-=t[l1]; t[l1] = 2; pewne++; } else if(t[l1] == 2) { rozne-=t[l2]; pewne++; t[l2] = 2; } else if(t[l2] == 2){ rozne-=t[l1]; pewne++; t[l1] = 2; } if(t[l1] == 0){ rozne++; s.push(l1); t[l1]= 1; } if(t[l2] == 0){ rozne++; s.push(l2); t[l2] = 1; } } int main(){ ios::sync_with_stdio(0); int n, m, l1, l2; char znak; cin >> n >> m; for(int i = 0; i < m; i++){ cin >> znak; if(znak == '+'){ cin >> l1 >> l2; komp++; tablica(l1, l2); } else if(znak == '-'){ cin >> l1; if(t[l1] == 2 ) pewne--; if(t[l1] == 1) rozne--; komp--; t[l1] = 0; } else { cin >> l1; if(t[l1]== 2) cout << 1; else if(t[l1] == 1) cout << '?'; else cout << t[l1]; } if(rozne == komp-pewne) { while(!s.empty()){ t[s.top()] = 2; s.pop(); rozne--; pewne++; } } } } |