Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000007
#define MAXN 1000001

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
set<int> getFactorization(int x) {
  set<int> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
  int max_comp() {
    int mx = INT_MIN;
    vector<bool> check(V + 1, false);

    for (int i = 1; i <= V; i++) {
      if (!check[i]) {
        int count = 0;
        DFS_util(check, i, count);
        if (count > mx) {
          mx = count;
        }
      }
    }
    return mx;
  }
  void DFS_util(vector<bool> &check, int i, int &count) {
    check[i] = true;
    count++;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (!check[*it]) {
        DFS_util(check, *it, count);
      }
    }
    return;
  }
};

int main() {
  sieve();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr;
    int temp;
    for (int i = 0; i < n; i++) {
      cin >> temp;
      arr.push_back(temp);
    }
    map<int, int> mp;
    set<int> s = getFactorization(arr[0]);
    for (auto it = s.begin(); it != s.end(); it++) {
      mp[*it]++;
    }
    int f = 0;
    for (int i = 1; i < n; i++) {
      set<int> temp = getFactorization(arr[i]);
      for (auto it = temp.begin(); it != temp.end(); it++) {
        mp[*it]++;
        if (f == 0 && s.count(*it)) {
          f = 1;
        } else {
          s.insert(*it);
        }
      }
    }
    if (f == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      for (auto it = mp.begin(); it != mp.end(); it++) {
        if (it->second >= n) {
          cout << "not coprime" << endl;
          return 0;
        }
      }
      cout << "setwise coprime" << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000007
#define MAXN 100001

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
set<int> getFactorization(int x) {
  set<int> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
  int max_comp() {
    int mx = INT_MIN;
    vector<bool> check(V + 1, false);

    for (int i = 1; i <= V; i++) {
      if (!check[i]) {
        int count = 0;
        DFS_util(check, i, count);
        if (count > mx) {
          mx = count;
        }
      }
    }
    return mx;
  }
  void DFS_util(vector<bool> &check, int i, int &count) {
    check[i] = true;
    count++;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (!check[*it]) {
        DFS_util(check, *it, count);
      }
    }
    return;
  }
};

int main() {
  sieve();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr;
    int temp;
    for (int i = 0; i < n; i++) {
      cin >> temp;
      arr.push_back(temp);
    }
    map<int, int> mp;
    set<int> s = getFactorization(arr[0]);
    for (auto it = s.begin(); it != s.end(); it++) {
      mp[*it]++;
    }
    int f = 0;
    for (int i = 1; i < n; i++) {
      set<int> temp = getFactorization(arr[i]);
      for (auto it = temp.begin(); it != temp.end(); it++) {
        mp[*it]++;
        if (f == 0 && s.count(*it)) {
          f = 1;
        } else {
          s.insert(*it);
        }
      }
    }
    if (f == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      for (auto it = mp.begin(); it != mp.end(); it++) {
        if (it->second >= n) {
          cout << "not coprime" << endl;
          return 0;
        }
      }
      cout << "setwise coprime" << endl;
    }
  }
  return 0;
}