alexjercan/bugnet huggingface dataset:
Problem p00594 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long long int arr[1000000 + 5];

int main() {
  int N;
  while (cin >> N && N) {
    map<int, int> mm;
    map<int, int>::iterator iter;
    long long int num;
    long long int result;
    bool flag = false;
    for (int i = 0; i < N; i++) {
      cin >> num;
      mm[num]++;
      if (mm[num] > (N / 2)) {
        flag = true;
        result = num;
      }
    }
    if (!flag)
      cout << "NO COLOR" << endl;
    else
      cout << result << endl;
  }
  // while(1);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

//??\\?????\\??´?????£??????????????\\?°¨?????°??????map?????¨??????????????°???

int main() {
  int N;
  while (cin >> N && N) {
    multiset<long long int> mm;
    multiset<long long int>::iterator iter;
    int num;
    int count1 = 0;
    bool flag = false;
    for (int i = 0; i < N; i++) {
      cin >> num;
      mm.insert(num);
      count1++;
    }
    for (iter = mm.begin(); iter != mm.end(); ++iter) {
      // cout<<*iter<<endl;
      int kk = mm.count(*iter);
      // cout<<kk<<endl;
      if (kk > (count1 / 2)) {
        cout << (*iter) << endl;
        flag = true;
        break;
      }
    }
    if (!flag)
      cout << "NO COLOR" << endl;
  }
  // while(1);
  return 0;
}
Problem p00594 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int MAX = 1000001;

int main() {
  int n, s;

  while (cin >> n, n) {
    int m[MAX] = {0};
    int ans = 0, num = 0;
    for (int i = 0; i < n; i++) {
      cin >> s;
      m[s]++;

      if (ans < m[s]) {
        ans = m[s];
        num = s;
      }
    }

    if (ans > n / 2)
      cout << num << endl;
    else
      cout << "NO COLOR" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, s;

  while (cin >> n, n) {
    vector<int> m(n);
    for (int i = 0; i < n; i++) {
      cin >> s;
      m[s]++;
    }
    int ans = 0, num = 0;
    for (int i = 0; i < m.size(); i++) {
      if (ans < m[i]) {
        ans = m[i];
        num = i;
      }
    }

    if (num > n / 2)
      cout << num << endl;
    else
      cout << "NO COLOR" << endl;
  }

  return 0;
}
Problem p00594 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <string.h>

typedef struct {
  int color;
  int number;
} Kind;

Kind U[1000000];

int main() {
  int i, j, a, c, b;

  while (~scanf("%d", &a)) {
    if (a == 0)
      break;

    memset(U, 0, sizeof(U));
    b = a;
    for (i = 0; i < a; i++) {
      scanf("%d", &c);
      for (j = 0; j < b; j++) {
        if (U[j].color == c || U[j].color == 0) {
          U[j].color = U[j].color > 0 ? U[j].color : c;
          ++U[j].number;
          break;
        }
      }
      b = j + 1;
    }

    j = -1;
    for (i = 0; i < a; i++) {
      if (2 * U[i].number > a) {
        j = i;
        break;
      }
    }

    if (j == -1) {
      printf("NO COLOR\\n");
    } else {
      printf("%d\\n", U[j].color);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

typedef struct {
  int color;
  int number;
} Kind;

Kind U[1000000];

int main() {
  int i, j, a, c;

  while (~scanf("%d", &a)) {
    if (a == 0)
      break;

    memset(U, 0, sizeof(U));

    for (i = 0; i < a; i++) {
      scanf("%d", &c);
      for (j = 0;; j++) {
        if (U[j].color == c || U[j].color == 0) {
          U[j].color = U[j].color > 0 ? U[j].color : c;
          ++U[j].number;
          break;
        }
      }
    }

    j = -1;
    for (i = 0; i < a; i++) {
      if (2 * U[i].number > a) {
        j = i;
        break;
      }
    }

    if (j == -1) {
      printf("NO COLOR\\n");
    } else {
      printf("%d\\n", U[j].color);
    }
  }
  return 0;
}
Problem p00594 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

const int mod = 109;
int cnt[mod];

int main() {
  while (int n = getInt()) {
    int ans = -1;
    const int n2 = n / 2;

    for (int i = 0; i < mod; i++) {
      cnt[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int a = getInt();
      int m = a % mod;
      if (++cnt[m] > n2) {
        ans = a;
        for (++i; i < n; i++)
          getInt();
      }
    }

    if (ans == -1)
      puts("NO COLOR");
    else
      printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

const int mod = 109;
int cnt[mod];

int main() {
  while (int n = getInt()) {
    int ans = -1;
    const int n2 = n / 2;

    for (int i = 0; i < mod; i++) {
      cnt[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int a = getInt();
      int m = a % mod;
      if (++cnt[m] > n2) {
        ans = a;
        if (i != n - 1)
          while (getchar() != '\\n')
            ;
      }
    }

    if (ans == -1)
      puts("NO COLOR");
    else
      printf("%d\\n", ans);
  }
  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

void change(int *, int *);

int main(void) {
  int x, y;

  while (scanf("%d %d", &x, &y) != EOF) {
    if (x == 0 && y == 0)
      break;
    if (x < y)
      change(&x, &y);
    while (y) {
      x %= y;
      change(&x, &y);
    }
    cout << x << endl;
  }
  return 0;
}

void change(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

void change(int *, int *);

int main(void) {
  int x, y;

  while (1) {
    scanf("%d %d", &x, &y);
    if (x == 0 && y == 0)
      break;
    if (x < y)
      change(&x, &y);
    while (y) {
      x %= y;
      change(&x, &y);
    }
    cout << x << endl;
  }
  return 0;
}

void change(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b) != EOF)
    printf("%d\\n", gcd(a, b));
}

Error: -8

Fixed submission:

#include <cstdio>
int solve(int a, int b) {
  if (a == 1)
    return a;
  return solve(b, a % b);
}
int main() {
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF)
    printf("%d\\n", solve(a, b));
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int pgcd(int a, int b) { return a == 0 ? b : pgcd(b % a, a); }

int main() {
  int a, b;
  while (cin >> a >> b) {
    cout << pgcd(a, b) << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int pgcd(int a, int b) { return b == 0 ? a : pgcd(b % a, a); }

int main() {
  int a, b;
  while (cin >> a >> b) {
    cout << pgcd(a, b) << endl;
  }
  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int a, int b) { return (b > 0) ? gcd(b, a % b) : a; }

int main() {
  int a, b;

  while (cin >> a >> b) {
    cout << gcd(a, b) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

template <typename T> T gcd(T a, T b) {
  T r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  int a, b;

  while (cin >> a >> b, a, b) {
    cout << gcd(a, b) << endl;
  }
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int gcm(int a, int b) {
  if (a > b) {
    int tmp = a;
    a = b;
    b = tmp;
  }

  for (int i = 1;; ++i) {
    if (a % i == 0 and b % (a / i) == 0) {
      return a / i;
    }
  }
}

int main(void) {
  int a, b;
  while (cin >> a >> b) {
    cout << gcm(a, b) << endl;
  }

  return 0;
}

Error: 0
a


Fixed submission:

#include <iostream>
using namespace std;

int gcm(int a, int b) {
  if (a > b) {
    int tmp = a;
    a = b;
    b = tmp;
    cerr << 'a' << endl;
  }

  for (int i = 1;; ++i) {
    if (a % i == 0 and b % (a / i) == 0) {
      return a / i;
    }
  }
}

int main(void) {
  int a, b;
  while (cin >> a >> b) {
    cout << gcm(a, b) << endl;
  }

  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int m, int n);

int main(int argc, const char *argv[]) {
  // std::cout << "Hello World\\n";
  int n, m;
  while (cin >> n >> m) {
    // std::cout << n << "," << m << "\\n";
    cout << gcd(n, m) << "\\n";
  }
  return 0;
}

int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

Error: TLE

Fixed submission:


#include <iostream>

int gcd(int m, int n);

int main(int argc, const char *argv[]) {
  // std::cout << "Hello World\\n";
  while (true) {
    int n, m;
    std::cin >> n >> m;
    // std::cout << n << "," << m << "\\n";
    std::cout << gcd(n, m) << "\\n";
  }
  return 0;
}

int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int a, b, r;
  while (cin >> a >> b) {
    if (a < b) {
      r = a;
      a = b;
      b = r;
    }
    r = a % b;
    if (r == 0) {
      cout << b << endl;
      continue;
    }
    while (1) {
      r = a % b;
      if (r == 0) {
        cout << b << endl;
        break;
      }
      a = b;
      b = r;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b, r;
  while (cin >> a >> b) {
    if (a < b) {
      r = a;
      a = b;
      b = r;
    }
    r = a % b;
    if (r == 0) {
      cout << b << endl;
    }
    while (1) {
      a = b;
      b = r;
      r = a % b;
      if (r == 0) {
        cout << b << endl;
        break;
      }
    }
  }
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int main() {

  long long int n, m, a, b, c;

  while (cin >> n >> m) {
    a = max(n, m);
    b = min(n, m);

    while (b != 0 && a % b != 0) {
      c = a % b;
      a = b;
      b = c;
    }

    cout << b << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {

  long long int n, m, a, b, c;

  while (cin >> n >> m) {
    a = max(n, m);
    b = min(n, m);
    c = a % b;
    a = b;
    b = c;
    while (a % b != 0) {
      c = a % b;
      a = b;
      b = c;
    }

    cout << b << endl;
  }
}
Problem p00595 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, m;

  while (cin >> n >> m) {
    if (n < m)
      swap(n, m);

    while (n % m != 0) {
      int d = n % m;
      n = m;
      m = d;
    }
    cout << m << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int a, b;
  while (cin >> a >> b) {
    for (int i = min(a, b) / 2; 0 <= i; i--) {
      if (a % i == 0 && b % i == 0) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int gcd(int x, int y) { return x < y ? gcd(y, x) : y ? gcd(y, x - y) : x; }

int main(void) {
  int x, y;

  while (scanf("%d%d", &x, &y) != EOF) {
    printf("%d\\n", gcd(x, y));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int gcd(int x, int y) { return x < y ? gcd(y, x) : y ? gcd(y, x - y) : x; }

int main(void) {
  int x, y;

  while (scanf("%d", &x), x != EOF) {
    scanf("%d", &y);
    printf("%d\\n", gcd(x, y));
  }

  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  long long a, b;
  while (cin >> a >> b) {
    long long g = gcd(a, b);
    cout << g << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int a, b;
  while (cin >> a >> b, a + b) {
    int g = gcd(a, b);
    cout << g << endl;
  }
  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int eucgcd(int m, int n) {
  int p = m % n;

  while (p > 0) {
    m = n;
    n = p;
    p = m % n;
  }

  return n;
}

int main() {
  int m, n;

  while (std::cin >> m >> n && m && n) {
    if (m >= n)
      std::cout << eucgcd(m, n) << std::endl;
    else
      std::cout << eucgcd(n, m) << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int eucgcd(int m, int n) {
  int p = m % n;

  while (p > 0) {
    m = n;
    n = p;
    p = m % n;
  }

  return n;
}

int main() {
  int m, n;

  while (std::cin >> m >> n, (m != EOF && n != EOF)) {
    if (m >= n)
      std::cout << eucgcd(m, n) << std::endl;
    else
      std::cout << eucgcd(n, m) << std::endl;
  }

  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;

unsigned int euclid(unsigned int a, unsigned int b) {
  if (b == 0)
    return a;
  return euclid(b, a % b);
}

int main(void) {
  unsigned int a, b;
  while (fscanf(stdin, "%u %u", &a, &b) != EOF)
    cout << euclid(a, b) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

unsigned int euclid(unsigned int a, unsigned int b) {
  if (b == 0)
    return a;
  return euclid(b, a % b);
}

int main(void) {
  unsigned int a, b;
  while (scanf("%u %u", &a, &b) != 0)
    cout << euclid(a, b) << endl;
  return 0;
}
Problem p00595 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

long int gcd(long int a, long int b) { return (b == 0 ? a : gcd(b, a % b)); }
int main() {
  long long a, b;
  while (scanf("%lld %lld", &a, &b) != EOF) {
    long long x = gcd(a, b);
    printf("%d\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int a, b, c;

  while (scanf("%d %d", &a, &b)) {
    fflush(stdin);
    if (a < b) {
      c = a;
      a = b;
      b = c;
    }

    do {
      c = a % b;
      if (c == 0) {
        continue;
      } else
        a = b;
      b = c;
      continue;
    } while (c > 10);
    printf("%d\\n", b);
  }
  getchar();
  return 0;
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    bool dp[1 << 18][7];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < 7; i++) {
      dp[0][i] = true;
    }

    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < 7; j++) {
        if (!dp[i][j])
          continue;
        for (int k = 0; k < n; k++) {
          if ((i & 1 << k) != 0)
            continue;
          if (j == a[k][0] - '0') {
            dp[i | 1 << k][a[k][1] - '0'] = true;
          }
          if (j == a[k][1] - '0') {
            dp[i | 1 << k][a[k][0] - '0'] = true;
          }
        }
      }
    }
    bool ok = false;
    for (int i = 0; i < 7; i++) {
      if (dp[(1 << n) - 1][i]) {
        ok = true;
      }
    }

    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    bool ok = false;
    do {
      for (int d = 0; d < 2; d++) {
        char next = a[0][d];
        for (int i = 1; i < n; i++) {
          if (a[i][0] == next) {
            next = a[i][1];
          } else if (a[i][1] == next) {
            next = a[i][0];
          } else {
            break;
          }
          if (i == n - 1) {
            ok = true;
          }
        }
      }
    } while (!ok && next_permutation(a.begin(), a.end()));
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p00596 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

class UnionFind {
private:
  int n;
  vector<int> a;

public:
  UnionFind(int n) : n(n), a(n, -1) {}
  int find(int x) { return a[x] < 0 ? x : (a[x] = find(a[x])); }
  bool equal(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (a[x] > a[y])
      swap(x, y);
    a[x] += a[y];
    a[y] = x;
    --n;
  }
  int size() { return n; }
};

int main() {
  int n;
  while (cin >> n) {
    int cnt[7] = {};
    UnionFind uf(7);
    rep(i, n) {
      string domino;
      cin >> domino;
      ++cnt[domino[0] - '0'];
      ++cnt[domino[1] - '0'];
      uf.unite(domino[0] - '0', domino[1] - '0');
    }
    int c = 0;
    rep(i, 7) if (cnt[i] % 2)++ c;
    if (c > 2) {
      cout << "No" << endl;
      continue;
    }
    c = 0;
    rep(i, 7) if (!cnt[i])++ c;
    if (uf.size() == c + 1) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

class UnionFind {
private:
  int n;
  vector<int> a;

public:
  UnionFind(int n) : n(n), a(n, -1) {}
  int find(int x) { return a[x] < 0 ? x : (a[x] = find(a[x])); }
  bool equal(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (a[x] > a[y])
      swap(x, y);
    a[x] += a[y];
    a[y] = x;
    --n;
  }
  int size() { return n; }
};

int main() {
  int n;
  while (cin >> n) {
    int cnt[6] = {};
    UnionFind uf(6);
    rep(i, n) {
      string domino;
      cin >> domino;
      ++cnt[domino[0] - '0'];
      ++cnt[domino[1] - '0'];
      uf.unite(domino[0] - '0', domino[1] - '0');
    }
    int c = 0;
    rep(i, 6) if (cnt[i] % 2)++ c;
    if (c > 2) {
      cout << "No" << endl;
      continue;
    }
    c = 0;
    rep(i, 6) if (!cnt[i])++ c;
    if (uf.size() == c + 1) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

// 51
#include <iostream>
#include <set>

#define T(a, b, c) (a * 1000 + b * 10 + c)

using namespace std;

int p[28];
int n;
set<int> s;

bool dfs(int pr, int u, int r) {
  if (r == 0) {
    return true;
  } else if (s.count(T(u, r, pr))) {
    return false;
  } else {
    for (int i = 0; i < n; i++) {
      if (!(u & 1 << i)) {
        for (int j = 0; j < 2; j++) {
          p[i] = p[i] / 10 + p[i] % 10 * 10;
          if (pr == p[i] / 10) {
            if (dfs(p[i] % 10, u | 1 << i, r - 1))
              return true;
          }
        }
      }
    }
    s.insert(T(u, r, pr));
    return false;
  }
}

int main() {
  while (cin >> n) {
    s.clear();
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    for (int i = 0; i < n; i++) {
      if (dfs(p[i] % 10, 1 << i, n - 1) || dfs(p[i] / 10, 1 << i, n - 1)) {
        cout << "Yes" << endl;
        goto next;
      }
    }
    cout << "No" << endl;
  next:;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 51
#include <iostream>

using namespace std;

int p[28];
int n;

bool dfs(int pr, int u, int r) {
  if (r == 0) {
    return true;
  } else {
    for (int i = 0; i < n; i++) {
      if (!(u & 1 << i)) {
        for (int j = 0; j < 2; j++) {
          p[i] = p[i] / 10 + p[i] % 10 * 10;
          if (pr == p[i] / 10) {
            if (dfs(p[i] % 10, u | 1 << i, r - 1))
              return true;
          }
        }
      }
    }
    return false;
  }
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    for (int i = 0; i < n; i++) {
      if (dfs(p[i] % 10, 1 << i, n - 1) || dfs(p[i] / 10, 1 << i, n - 1)) {
        cout << "Yes" << endl;
        goto next;
      }
    }
    cout << "No" << endl;
  next:;
  }
  return 0;
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 18
using namespace std;

bool dp[(1 << N)][N][2];

int main() {

  int n;

  while (cin >> n) {

    string num[N];

    for (int i = 0; i < n; i++)
      cin >> num[i];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < n; i++)
      for (int j = 0; j < 2; j++)
        dp[(1 << i)][i][j] = true;

    for (int i = 0; i < (1 << n); i++)

      for (int j = 0; j < n; j++)

        for (int k = 0; k < 2; k++) {

          if (!dp[i][j][k])
            continue;

          for (int l = 0; l < n; l++) {

            if (i & (1 << l))
              continue;

            for (int m = 0; m < 2; m++)

              if (num[j][k] == num[l][m])
                dp[i | (1 << l)][l][!m] = true;
          }
        }

    bool ans = false;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < 2; j++)
        ans |= dp[(1 << n) - 1][i][j];

    cout << (ans ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int dp[1 << 18][18][2];
int N;
string s[18];

int func(int state, int n, int p) {
  if (state == (1 << N) - 1) { // cout << p << endl;
    return 1;
  }
  if (dp[state][n][p] != -1)
    return dp[state][n][p];

  int ret = 0;
  for (int i = 0; i < N; i++) {
    if (state & (1 << i))
      continue;
    if (s[n][p] == s[i][0])
      ret = func(state | (1 << i), i, 1);
    if (s[n][p] == s[i][1])
      ret = func(state | (1 << i), i, 0);
  }

  return dp[state][n][p] = ret;
}

int main() {
  while (cin >> N) {
    for (int i = 0; i < N; i++)
      cin >> s[i];
    int flag = 0;
    for (int i = 0; i < N; i++) {
      fill_n(**dp, (1 << 18) * 18 * 2, -1);
      flag = func(1 << i, i, 0);
      flag = func(1 << i, i, 1);
      // cout << flag << endl;
      if (flag == 1)
        break;
    }

    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}