Submission #1519628


Source Code Expand

#include <cstdio>

#define MAX_N 100000

int par[MAX_N], rank[MAX_N];
int n, q;

void init(){
  for(int i = 0; i < n; i++)
    par[i] = i;
}

int find(int x){
  if(par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);

  if(x == y)
    return;

  if(rank[x] > rank[y]){
    par[y] = x;
  }else{
    par[x] = y;
    if(rank[x] == rank[y]){
      rank[y]++;
    }
  }i
}

bool same(int x, int y){
  return find(x) == find(y);
}

int main(){
  int P, X, Y;
  scanf("%d %d", &n, &q);

  init();

  for(int i = 0; i < q; i++){
    scanf("%d %d %d", &P, &X, &Y);
    if(!P){
      unite(X, Y);
    }else{
      if(same(X, Y)){
        printf("Yes\n");
      }else{
        printf("No\n");
      }
    }
  }
  return 0;
}

Submission Info

Submission Time
Task B - Union Find
User nena_
Language C++14 (GCC 5.4.1)
Score 0
Code Size 848 Byte
Status CE

Compile Error

./Main.cpp: In function ‘void unite(int, int)’:
./Main.cpp:34:4: error: ‘i’ was not declared in this scope
   }i
    ^
./Main.cpp: In function ‘int main()’:
./Main.cpp:43:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &q);
                         ^
./Main.cpp:48:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &P, &X, &Y);
                                  ^