Problem p02343 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  int n, q, label, com, x, y, to_change1, to_change2;

  scanf("%d %d", &n, &q);
  int table[n];

  for (int i = 0; i < n; i++)
    table[i] = i;
  label = n;

  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &com, &x, &y);

    if (com == 1) {
      if (table[x] == table[y])
        printf("1\\n");
      else {
        printf("0\\n");
      }
    } else {
      if (table[x] == table[y])
        continue;

      to_change1 = table[x];
      to_change2 = table[y];

      for (int i = 0; i < n; i++) {
        if (table[i] == to_change1 || table[i] == to_change2) {
          table[i] = label;
        }
      }
      label++;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

struct Tree {
  Tree() {
    parent = 0;
    id = 0;
    num_of_children = 0;
  }

  bool isSame(int node_index) {
    if (parent != 0) {
      if (parent->id == node_index)
        return true;
      for (int i = 0; i < parent->num_of_children; i++) {
        if (parent->children[i]->id == node_index)
          return true;
      }
      return false;
    } else { //?????????????????´???
      for (int i = 0; i < num_of_children; i++) {
        if (children[i]->id == node_index)
          return true;
      }
      return false;
    }
  }

  Tree *parent;
  vector<Tree *> children;
  int num_of_children, id;
};

void mergeTree(Tree *new_parent, Tree *new_children) {
  if (new_children->isSame(new_parent->id))
    return;

  if (new_parent->parent == 0) {
    if (new_children->parent == 0) {
      new_children->parent = new_parent;
      new_parent->children.push_back(new_children);
      new_parent->num_of_children++;

      for (int i = 0; i < new_children->num_of_children; i++) {
        new_children->children[i]->parent = new_parent;
        new_parent->children.push_back(new_children->children[i]);
        new_parent->num_of_children++;
      }
      new_children->num_of_children = 0;
    } else {
      new_children->parent->parent = new_parent;
      new_parent->children.push_back(new_children->parent);
      new_parent->num_of_children++;

      Tree *tmp = new_children->parent;
      for (int i = 0; i < tmp->num_of_children; i++) {
        tmp->children[i]->parent = new_parent;
        new_parent->children.push_back(tmp->children[i]);
        new_parent->num_of_children++;
      }
      tmp->num_of_children = 0;
    }
  } else {
    if (new_children->parent == 0) {
      new_children->parent = new_parent->parent;
      new_parent->parent->children.push_back(new_children);
      new_parent->parent->num_of_children++;

      for (int i = 0; i < new_children->num_of_children; i++) {
        new_children->children[i]->parent = new_parent->parent;
        new_parent->parent->children.push_back(new_children->children[i]);
        new_parent->parent->num_of_children++;
      }
      new_children->num_of_children = 0;
    } else {
      new_children->parent->parent = new_parent->parent;
      new_parent->parent->children.push_back(new_children->parent);
      new_parent->parent->num_of_children++;

      Tree *tmp = new_children->parent;
      for (int i = 0; i < tmp->num_of_children; i++) {
        tmp->children[i]->parent = new_parent->parent;
        new_parent->parent->children.push_back(tmp->children[i]);
        new_parent->parent->num_of_children++;
      }
      tmp->num_of_children = 0;
    }
  }
}

int main() {
  int n, q, com, x, y;

  scanf("%d %d", &n, &q);

  Tree *tree = new Tree[n];

  for (int i = 0; i < n; i++) {
    tree[i].id = i;
  }

  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &com, &x, &y);

    if (com == 0) {
      if (x > y) {
        mergeTree(&tree[y], &tree[x]);
      } else {
        mergeTree(&tree[x], &tree[y]);
      }
    } else {
      if (x > y) {
        if (tree[x].isSame(y)) {
          printf("1\\n");
        } else {
          printf("0\\n");
        }
      } else {
        if (tree[y].isSame(x)) {
          printf("1\\n");
        } else {
          printf("0\\n");
        }
      }
    }
  }

  return 0;
}