Why is my code giving Runtime Error?
Difference between en1 and en2, changed 147 character(s)
I was solving this problem [Problem Link](https://codeforces.com/contest/160/problem/D)↵

<spoiler summary="Here is my code">↵

~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
signed main(){↵
  ios_base::sync_with_stdio(false);↵
  cin.tie(0);↵
  /* -1 -> None↵
      0 -> at least one↵
      1 -> any↵
     -2 -> undecided↵
  */↵
  int N,M; cin >> N >> M;↵
  vector<tuple<int,int,int,int>> e(M);↵
  vector<int> answer(M, -2);↵
  vector<int> p(N), sz(N, 1);↵
  iota(p.begin(), p.end(), 0);↵

  function<int(int)> find = [&](int x){↵
    return (x == p[x] ? x : p[x] = find(x));↵
  };↵
  function<bool(int,int)> unite = [&](int x, int y){↵
    x = find(x); y = find(y);↵
    if(x != y){↵
      if(sz[x] < sz[y]) swap(x, y);↵
      p[y] = x;↵
      sz[x] += sz[y];↵
      return true;↵
    }↵
    return false;↵
  };↵
  for(int i = 0; i < M; i++){↵
    int x,y,z; cin >> x >> y >> z;↵
    x--; y--;↵
    e[i] = make_tuple(z, x, y, i);↵
  }↵
  sort(e.begin(), e.end());↵
  int i = 0;↵
  while(i < M){↵
    int j = i;↵
    while(j < M && get<0>(e[j]) == get<0>(e[i])){↵
      if(find(get<1>(e[j])) == find(get<2>(e[j])))↵
        answer[get<3>(e[j])] = -1;↵
      ++j;↵
    }↵
    bool ok = true;↵
    j = i;↵
    while(j < M && get<0>(e[j]) == get<0>(e[i])){↵
      if(answer[get<3>(e[j])] == -1){↵
        ++j; continue;↵
      }↵
      if(unite(get<1>(e[j]), get<2>(e[j])) == false) // When I comment this line↵
        ok = false;↵
      ++j;↵
    }↵
    j = i;↵
    while(j < M && get<0>(e[j]) == get<0>(e[i])){↵
      if(answer[get<3>(e[j])] == -1){↵
        ++j; continue;↵
      }↵
      if(ok) answer[get<3>(e[j])] = 1;↵
      else answer[get<3>(e[j])] = 0;↵
      ++j;↵
    }↵
    i = j;↵
  }↵
  for(int i = 0; i < M; i++){↵
    if(answer[i] == -1)↵
      cout << "none" << '\n';↵
    else if(answer[i] == 0)↵
      cout << "at least one" << '\n';↵
    else↵
      cout << "any" << '\n';↵
  }↵
  return 0;↵
}↵

~~~~~↵


</spoiler>↵

Now I don't want to know the logic of how to solve the problem or whether my logic is wrong or not. ↵

It's just that when I am compiling this code is giving RE (Specifically : Runtime error: exit code is 2147483647)↵

Now when I comment out the line in which I unite two sets (Mentioned in the code), it does not give RE.↵

But I don't know how can my unite function be wrong(It's just merging two sets [DSU]). ↵

Can anyone tell me the flaw?↵

ThanksEDIT : Found the error. Tbh it was a silly one. In my unite function when (p[x] != x) I call the function again with the same argument instead of p[x].

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English arnav2004 2022-01-21 10:35:08 147
en1 English arnav2004 2022-01-20 23:24:49 2445 Initial revision (published)