fork download
  1. #include <bits/stdc++.h>
  2. #define mp make_pair
  3. #define pb push_back
  4. #define st first
  5. #define nd second
  6. using namespace std;
  7. typedef pair<int,int> ii;
  8. vector <int> dsu(1005,-1);
  9. int Find(int a){return (dsu[a]<0)?a:dsu[a]=Find(dsu[a]);}
  10. void Join(int a,int b){
  11. a=Find(a);b=Find(b);
  12. if(a==b)return;
  13. else{
  14. if(a>b)swap(a,b);
  15. dsu[b]+=dsu[a];dsu[a]=b;
  16. }
  17. }
  18. int main(){
  19. ios_base::sync_with_stdio(false);
  20. freopen("no112b.inp","r",stdin);
  21. freopen("no112b.out","w",stdout);
  22. int n,m;scanf("%d%d",&n,&m);
  23. for(int i=0;i<m;i++){
  24. int u,v;scanf("%d%d",&u,&v);
  25. Join(u,v);
  26. }
  27. int q;scanf("%d",&q);
  28. while(q--){
  29. int t,u,v;
  30. scanf("%d%d%d",&t,&u,&v);
  31. if(t==1){
  32. if(Find(u)==Find(v))printf("1");
  33. else printf("0");
  34. }
  35. else{
  36. Join(u,v);
  37. }
  38. }
  39. }
  40.  
Runtime error #stdin #stdout 0s 3224KB
stdin
Standard input is empty
stdout
Standard output is empty