1 / 4
Jul 2021

I’m trying to solve CAM5 since 4 days, but it’s showing runtime error - SIGILL everytime, I’m unable to figure out. I using C++14 (gcc) lang.

Here is my code:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <chrono>
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace chrono;
// using namespace boost::multiprecision;

// template stats

#define F first
#define S second
#define N 300045
#define db double
#define M 1000000007
// #define lll cpp_int
#define ll long long int
#define ull unsigned ll
#define umap unordered_map
#define uset unordered_set
#define PI 3.141592653589793238
#define all(v) v.begin(), v.end()
#define each(seq) for(auto e: seq)
#define inf numeric_limits<ll>::max()
#define F0(n, i) for(ll i = 0; i < n; i++)
#define F1(n, i) for(ll i = 1; i <= n; i++)
#define dbgpr(pr) cout << ' ' << #pr << "{" << pr.F << ',' << pr.S << "}"
#define dbgarr(seq) cout << #seq << " <"; each(seq) { cout << e << ','; } cout << ">\n"
#define dbgprs(seq) cout << #seq << " <"; each(seq) { dbgpr(e); cout << ','; } cout << ">\n"
#define dbgmat(mat) cout << #mat << " {\n"; each(mat) { cout << ' '; dbgarr(e); } cout << "}\n"
#define dbgmap(hash) cout << #hash << " { "; each(hash) { cout << e.first << ": " << e.second << ", "; } cout << "}\n"
#define dbgtree(tree) cout << #tree << " {\n"; each(tree) { cout << e.first << ": "; dbgarr(e.second); } cout << "}\n"

// template ends. main code starts here

vector<vector<ll>> tree(1e5+1);
bool seen[int(1e5)+1];

void recur(ll n = 1, ll p = -1) {
 seen[n] = 1;
 for(int i: tree[n]) {
  if(i != p && !seen[i]) {
   recur(i, n);
  }
 }
}

void solve() {
 ll n, m;
 cin >> n >> m;

 F0(n, i) {
  tree[i].clear();
  seen[i] = 0;
 }

 F0(m, _) {
  ll u, v;
  cin >> u >> v;
  tree[u].push_back(v);
  tree[v].push_back(u);
 }

 ll k = 0;
 F0(n, i) {
  //cout << i << ": seen=" << seen[i] << ' ';
  //dbgarr(tree[i]);

  if(!seen[i]) {
   recur(i);
   k++;
  }
 }

 cout << k;
}

int main() {
 auto start = high_resolution_clock::now();
 ios_base::sync_with_stdio(false); // comment this line, while debugging
 cin.tie(NULL);

 ll t;
 cin >> t;
 while(t--) {
  solve();
  cout << '\n';
 }

 auto stop = high_resolution_clock::now();
 auto duration = duration_cast<microseconds>(stop - start);
 // cerr << "> Time elasped: " << duration.count() << " µs\n";
 return 0;
}

Perhaps you’re recursing too deep? Try a non-recursive version.

Well, I can’t figure out how to dfs without recursion :woman_shrugging:

8 days later

I figured out that pragma directive is creating runtime error, but can anyone explain why so?