// Witold Milewski // PA 2024 #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=a; i<=b; ++i) #define ll long long #define pi pair<int, int> #define f first #define s second #define pb push_back #define eb emplace_back #define sz(A) (int)(A.size()) const int maxn=3e5+3, maxq=1e6+3; int n, q; int vertex[maxn], rep[maxn+maxq], cnt[maxn+maxq], vertex_cnt[maxn+maxq]; int nr_new_vert=maxn; string odp=""; int ff(int x) { if(rep[x]==x) return x; return rep[x]=ff(rep[x]); } void uu(int a, int b) { a = ff(a); b = ff(b); rep[a]=b; vertex_cnt[b]+=vertex_cnt[a]; vertex_cnt[a]=0; cnt[b]+=cnt[a]; cnt[a]=0; } int main() { cin.tie(0) -> ios_base::sync_with_stdio(0); FOR(i, 0, maxn+maxq-1) rep[i]=i; cin >> n >> q; FOR(i, 1, n) vertex[i]=i, vertex_cnt[i]=1; char ty; int a, b; FOR(i, 1, q) { cin >> ty >> a; if(ty=='+') { cin >> b; if(ff(vertex[a]) != ff(vertex[b])) uu(vertex[a], vertex[b]); ++cnt[ff(vertex[a])]; } if(ty=='-') { --vertex_cnt[ff(vertex[a])]; --cnt[ff(vertex[a])]; vertex[a]=++nr_new_vert; ++vertex_cnt[ff(vertex[a])]; } if(ty=='?') { int res=2; int spojna = ff(vertex[a]); if(cnt[spojna]>=vertex_cnt[spojna]) res=1; if(vertex_cnt[spojna]==1 && cnt[spojna]==0) res=0; if(res==0) odp+="0"; if(res==1) odp+="1"; if(res==2) odp+="?"; } } cout << odp << '\n'; }
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 | // Witold Milewski // PA 2024 #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=a; i<=b; ++i) #define ll long long #define pi pair<int, int> #define f first #define s second #define pb push_back #define eb emplace_back #define sz(A) (int)(A.size()) const int maxn=3e5+3, maxq=1e6+3; int n, q; int vertex[maxn], rep[maxn+maxq], cnt[maxn+maxq], vertex_cnt[maxn+maxq]; int nr_new_vert=maxn; string odp=""; int ff(int x) { if(rep[x]==x) return x; return rep[x]=ff(rep[x]); } void uu(int a, int b) { a = ff(a); b = ff(b); rep[a]=b; vertex_cnt[b]+=vertex_cnt[a]; vertex_cnt[a]=0; cnt[b]+=cnt[a]; cnt[a]=0; } int main() { cin.tie(0) -> ios_base::sync_with_stdio(0); FOR(i, 0, maxn+maxq-1) rep[i]=i; cin >> n >> q; FOR(i, 1, n) vertex[i]=i, vertex_cnt[i]=1; char ty; int a, b; FOR(i, 1, q) { cin >> ty >> a; if(ty=='+') { cin >> b; if(ff(vertex[a]) != ff(vertex[b])) uu(vertex[a], vertex[b]); ++cnt[ff(vertex[a])]; } if(ty=='-') { --vertex_cnt[ff(vertex[a])]; --cnt[ff(vertex[a])]; vertex[a]=++nr_new_vert; ++vertex_cnt[ff(vertex[a])]; } if(ty=='?') { int res=2; int spojna = ff(vertex[a]); if(cnt[spojna]>=vertex_cnt[spojna]) res=1; if(vertex_cnt[spojna]==1 && cnt[spojna]==0) res=0; if(res==0) odp+="0"; if(res==1) odp+="1"; if(res==2) odp+="?"; } } cout << odp << '\n'; } |