#include <bits/stdc++.h> using namespace std; int t[300005]; vector<int> G[300004]; //set< pair<int,int> > pary; char wypisz(int a){ if (a == 0) return '0'; if (a == 1) return '1'; return '?'; } void dfs(int v){ t[v] = 1; for(unsigned int i = 0; i < G[v].size();i++){ int w = G[v][i]; if(t[w]==-1) dfs(w); } } void bfs(int z){ int ile = 0, ktory = 0; for(unsigned int i = 0; i < G[z].size();i++){ int w = G[z][i]; if(t[w]==-1) { ile++; ktory = w; } } if (ile == 1) t[ktory] = 0; } int main() { ios_base::sync_with_stdio(0); cin.tie();cout.tie(); int n, i, q, a, b; char znak; cin >> n >> q; for(i = 0; i < q; i++){ cin >> znak >> a; if (znak == '?') cout << wypisz(t[a]); else if (znak == '-') { if(t[a] == -1) bfs(a); t[a] = 0; } else { cin >> b; if (a == b) dfs(a); else if (t[a] == 1) { dfs(b); } else if (t[b] == 1){ dfs(a); } else { G[a].push_back(b); G[b].push_back(a); if (t[a] == -1 && t[b] == -1) dfs(a); else { t[a] = -1; t[b] = -1; } } } } 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 | #include <bits/stdc++.h> using namespace std; int t[300005]; vector<int> G[300004]; //set< pair<int,int> > pary; char wypisz(int a){ if (a == 0) return '0'; if (a == 1) return '1'; return '?'; } void dfs(int v){ t[v] = 1; for(unsigned int i = 0; i < G[v].size();i++){ int w = G[v][i]; if(t[w]==-1) dfs(w); } } void bfs(int z){ int ile = 0, ktory = 0; for(unsigned int i = 0; i < G[z].size();i++){ int w = G[z][i]; if(t[w]==-1) { ile++; ktory = w; } } if (ile == 1) t[ktory] = 0; } int main() { ios_base::sync_with_stdio(0); cin.tie();cout.tie(); int n, i, q, a, b; char znak; cin >> n >> q; for(i = 0; i < q; i++){ cin >> znak >> a; if (znak == '?') cout << wypisz(t[a]); else if (znak == '-') { if(t[a] == -1) bfs(a); t[a] = 0; } else { cin >> b; if (a == b) dfs(a); else if (t[a] == 1) { dfs(b); } else if (t[b] == 1){ dfs(a); } else { G[a].push_back(b); G[b].push_back(a); if (t[a] == -1 && t[b] == -1) dfs(a); else { t[a] = -1; t[b] = -1; } } } } return 0; } |