Submission #1519395


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]++;
    }
  }
}

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);
    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 866 Byte
Status WA
Exec Time 49 ms
Memory 1152 KB

Compile Error

./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);
                                  ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 1
AC × 15
WA × 4
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 0 ms 128 KB
subtask_01_01.txt AC 29 ms 512 KB
subtask_01_02.txt AC 1 ms 512 KB
subtask_01_03.txt AC 41 ms 896 KB
subtask_01_04.txt AC 49 ms 1152 KB
subtask_01_05.txt AC 4 ms 256 KB
subtask_01_06.txt AC 4 ms 1024 KB
subtask_01_07.txt WA 45 ms 768 KB
subtask_01_08.txt AC 49 ms 1152 KB
subtask_01_09.txt WA 0 ms 128 KB
subtask_01_10.txt AC 1 ms 512 KB
subtask_01_11.txt WA 42 ms 768 KB
subtask_01_12.txt AC 49 ms 1152 KB
subtask_01_13.txt AC 37 ms 640 KB
subtask_01_14.txt AC 1 ms 512 KB
subtask_01_15.txt WA 43 ms 768 KB
subtask_01_16.txt AC 49 ms 1152 KB
subtask_01_17.txt AC 48 ms 896 KB
subtask_01_18.txt AC 48 ms 896 KB