Problem p03805 (C++)

Buggy submission (status: Runtime Error):

/*********************************************************************\
             بسم الله الرحمن الرحيم                                      |
    codeforces = Alhelal_ONE  ||  topcoder = Al..helal                |
    mail = alhelaliu@gmail.com                                        |
    CSE,Islamic University,Kushtia                                    |
                                                                      |
**********************************************************************/

using namespace std;
#include <bits/stdc++.h>
typedef long long LL;
// typedef int   LL;
typedef long long ll;
typedef pair<LL, LL> PII;
typedef vector<PII> VPII;
typedef vector<LL> VI;
typedef map<string, LL> MSI;
typedef map<LL, LL> MII;

/// LOOP
#define FORSTL(x, n) for (__typeof(n.begin()) x = n.begin(); x != n.end(); x++)
#define repp(x, n) for (__typeof(n) x = 1; x <= (n); x++)
#define rep(x, n) for (__typeof(n) x = 0; x < (n); x++)
#define reps(i, s) for (int i = 0; i < s.size(); i++)
/// Shortcut
#define Unique(store)                                                          \
  store.resize(unique(store.begin(), store.end()) - store.begin())
#define _CIN                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(false);
#define mem(ar, value) memset(ar, value, sizeof(ar))
#define all(x) x.begin(), x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define se second
#define fi first
#define pi acos(-1.0)
#define EPS (1e-9)
/// DEBUG
#define DDD(x, y, z)                                                           \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x                \
       << " | " #y " = " << y << " | " #z " =" << z << endl;
#define DD(x, y)                                                               \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x                \
       << " | " #y " = " << y << endl;
#define D(x)                                                                   \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x << endl;

/// NUMBER THEORY
template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> T my_pow(T n, T p) {
  if (p == 0)
    return 1;
  T x = my_pow(n, p / 2);
  x = (x * x);
  if (p & 1)
    x = (x * n);
  return x;
} /// n to the power p
template <class T> T big_mod(T n, T p, T m) {
  if (p == 0)
    return (T)1;
  T x = big_mod(n, p / 2, m);
  x = (x * x) % m;
  if (p & 1)
    x = (x * n) % m;
  return x;
}
// template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
/// string to int
template <class T> T extract(string s, T ret) {
  stringstream ss(s);
  ss >> ret;
  return ret;
} ///
string itos(LL n) {
  string s;
  while (n) {
    s += (n % 10 + 48);
    n /= 10;
  }
  reverse(all(s));
  return s;
}
LL stoi(string s) {
  LL n = 0;
  rep(i, len(s)) n = n * 10 + (s[i] - 48);
  return n;
}
/// GEOMETRY
// double DEG(double x) { return (180.0*x)/(PI);}
// double RAD(double x) { return (x*(double)PI)/(180.0);}
// template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)(
// (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );} template<typename T> T ANGLE(T x1,T
// y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
// template<typename T> LL isLeft(T a,T b,T c) { return
// (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
/// Direction Array
// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};
// struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or
// (a.x==b.x and a.y<b.y);} bool vis[N];LL prime(){vis[1]=true;for(int
// i=4;i<=N;i+=2)  vis[i]=1;LL aa=sqrt(N+.00001);for(int i=3;i<=aa;i+=2){for(int
// j=i*i;j<=N;j+=(2*i)) vis[j]=1;}}
///========================================================================================================///

const int N = 5e5 + 5, MOD = 1e9 + 7;
LL ar[N], br[N];
LL a = 0, b = 0, c = 0, r = 0, rr = 0, f = 0, ff = 0, n, m, t = 0, ks = 0;
string s, ss;
LL dp[101][101];
int main() {
  // _CIN;

  cin >> n >> m;

  rep(i, m) ar[i] = i;
  rep(i, n) {
    cin >> a >> b;
    a--, b--;

    dp[a][b] = 1;
    dp[b][a] = 1;
  }

  LL ans = 0;

  do {
    LL c = 0;
    if (ar[0] == 0) {
      for (int i = 1; i < m; i++) {
        if (dp[ar[i]][ar[i - 1]] == 1 or dp[ar[i - 1]][ar[i]]) {
          c++;
        }
      }
      if (c == m - 1)
        ans++;
      c = 0;
    }

  } while (next_permutation(ar, ar + m));

  cout << ans << endl;

  return 0;
}


Fixed submission:

/*********************************************************************\
             بسم الله الرحمن الرحيم                                      |
    codeforces = Alhelal_ONE  ||  topcoder = Al..helal                |
    mail = alhelaliu@gmail.com                                        |
    CSE,Islamic University,Kushtia                                    |
                                                                      |
**********************************************************************/

using namespace std;
#include <bits/stdc++.h>
typedef long long LL;
// typedef int   LL;
typedef long long ll;
typedef pair<LL, LL> PII;
typedef vector<PII> VPII;
typedef vector<LL> VI;
typedef map<string, LL> MSI;
typedef map<LL, LL> MII;

/// LOOP
#define FORSTL(x, n) for (__typeof(n.begin()) x = n.begin(); x != n.end(); x++)
#define repp(x, n) for (__typeof(n) x = 1; x <= (n); x++)
#define rep(x, n) for (__typeof(n) x = 0; x < (n); x++)
#define reps(i, s) for (int i = 0; i < s.size(); i++)
/// Shortcut
#define Unique(store)                                                          \
  store.resize(unique(store.begin(), store.end()) - store.begin())
#define _CIN                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(false);
#define mem(ar, value) memset(ar, value, sizeof(ar))
#define all(x) x.begin(), x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define se second
#define fi first
#define pi acos(-1.0)
#define EPS (1e-9)
/// DEBUG
#define DDD(x, y, z)                                                           \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x                \
       << " | " #y " = " << y << " | " #z " =" << z << endl;
#define DD(x, y)                                                               \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x                \
       << " | " #y " = " << y << endl;
#define D(x)                                                                   \
  cerr << __FUNCTION__ << ":" << __LINE__ << ": " #x " = " << x << endl;

/// NUMBER THEORY
template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> T my_pow(T n, T p) {
  if (p == 0)
    return 1;
  T x = my_pow(n, p / 2);
  x = (x * x);
  if (p & 1)
    x = (x * n);
  return x;
} /// n to the power p
template <class T> T big_mod(T n, T p, T m) {
  if (p == 0)
    return (T)1;
  T x = big_mod(n, p / 2, m);
  x = (x * x) % m;
  if (p & 1)
    x = (x * n) % m;
  return x;
}
// template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
/// string to int
template <class T> T extract(string s, T ret) {
  stringstream ss(s);
  ss >> ret;
  return ret;
} ///
string itos(LL n) {
  string s;
  while (n) {
    s += (n % 10 + 48);
    n /= 10;
  }
  reverse(all(s));
  return s;
}
LL stoi(string s) {
  LL n = 0;
  rep(i, len(s)) n = n * 10 + (s[i] - 48);
  return n;
}
/// GEOMETRY
// double DEG(double x) { return (180.0*x)/(PI);}
// double RAD(double x) { return (x*(double)PI)/(180.0);}
// template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)(
// (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );} template<typename T> T ANGLE(T x1,T
// y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
// template<typename T> LL isLeft(T a,T b,T c) { return
// (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
/// Direction Array
// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};
// struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or
// (a.x==b.x and a.y<b.y);} bool vis[N];LL prime(){vis[1]=true;for(int
// i=4;i<=N;i+=2)  vis[i]=1;LL aa=sqrt(N+.00001);for(int i=3;i<=aa;i+=2){for(int
// j=i*i;j<=N;j+=(2*i)) vis[j]=1;}}
///========================================================================================================///

const int N = 5e5 + 5, MOD = 1e9 + 7;
LL ar[N], br[N];
LL a = 0, b = 0, c = 0, r = 0, rr = 0, f = 0, ff = 0, n, m, t = 0, ks = 0;
string s, ss;
LL dp[101][101];
int main() {
  // _CIN;

  // cin>>n>>m;
  cin >> m >> n;

  rep(i, m) ar[i] = i;
  rep(i, n) {
    cin >> a >> b;
    a--, b--;

    dp[a][b] = 1;
    dp[b][a] = 1;
  }

  LL ans = 0;

  do {
    LL c = 0;
    if (ar[0] == 0) {
      for (int i = 1; i < m; i++) {
        if (dp[ar[i]][ar[i - 1]] == 1 or dp[ar[i - 1]][ar[i]]) {
          c++;
        }
      }
      if (c == m - 1)
        ans++;
      c = 0;
    }

  } while (next_permutation(ar, ar + m));

  cout << ans << endl;

  return 0;
}