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

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

int main() {
  int total, step;
  vector<int> person;
  vector<int>::iterator it;
  int i, m;

  while (1) {
    // get input
    cin >> total >> step;

    // finish condition
    if (total == 0 && step == 0)
      break;

    // init
    for (i = 1; i <= total; i++) {
      person.push_back(i);
    }
    it = person.begin();

    while (1) {
      // answer
      if (person.size() == 1) {
        cout << person[0] << endl;
        person.clear();
        break;
      }

      // step forward
      for (m = 0; m < (step - 1); m++) {
        it++;
        if (it == person.end()) {
          it = person.begin();
        }
      }

      // delete
      person.erase(it);
      if (it == person.end()) {
        it = person.begin();
      }

      //            for(int k = 0; k < person.size(); k++){
      //                cout << *it <<"is erased " << person[k] << endl;
      //            }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int total, step;
  vector<int> person;
  vector<int>::iterator it;
  int i, m;

  while (1) {
    // get input
    cin >> total >> step;

    // finish condition
    if (total == 0 && step == 0)
      break;

    // init
    for (i = 1; i <= total; i++) {
      person.push_back(i);
    }
    it = person.begin();

    while (1) {
      // answer
      if (person.size() == 1) {
        cout << person[0] << endl;
        person.clear();
        break;
      }

      // step forward
      for (m = 0; m < (step - 1); m++) {
        it++;
        if (it == person.end()) {
          it = person.begin();
        }
      }

      // delete
      person.erase(it);
      if (it == person.end()) {
        it = person.begin();
      }

      for (int k = 0; k < person.size(); k++) {
        cout << *it << "is erased " << person[k] << endl;
      }
    }
  }

  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  queue<int> qu;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 1; i <= n; i++)
      qu.push(i);
    int cnt = 0;
    while (qu.front() != qu.back()) {
      int buf = qu.front();
      qu.pop();
      if (++cnt % m != 0)
        qu.push(buf);
    }
    cout << qu.back() << endl;
    qu.pop();
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  queue<int> qu;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 1; i <= n; i++)
      qu.push(i);
    int cnt = 0;
    while (qu.front() != qu.back()) {
      int buf = qu.front();
      qu.pop();
      qu.push(buf);
      cnt++;
      if (cnt % (m - 1) == 0) {
        qu.pop();
      }
    }
    cout << qu.back() << endl;
    qu.pop();
  }
  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    bool is_die[1000] = {};
    for (int i = 0, cntM = 1, cntH = N;; i = ++i % N) {
      if (is_die[i] == true)
        continue;
      if (cntH == 1) {
        printf("%d\\n", i + 1);
        break;
      }
      if (cntM == M) {
        is_die[i] = true;
        cntH--;
        cntM = 0;
      }
      cntM++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  bool is_die[1000] = {};
  int N, M;
  while (cin >> N >> M, N || M) {
    for (int i = 0, cntM = 1, cntH = N;; i = ++i % N) {
      if (is_die[i] == true)
        continue;
      if (cntH == 1) {
        printf("%d\\n", i + 1);
        break;
      }
      if (cntM == M) {
        is_die[i] = true;
        cntH--;
        cntM = 0;
      }
      cntM++;
    }
  }
  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Runtime Error

// solve by circulational list
#include <cstdlib>
#include <iostream>
using namespace std;

struct node {
  int value;
  struct node *next;
};

int main() {
  int n, m;
  struct node *now;
  struct node *list;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    // make list
    list = (struct node *)malloc(sizeof(struct node));
    now = list;
    for (int i = 1; i < n; i++) {
      now->next = (struct node *)malloc(sizeof(struct node));
      now->value = i;
      now = now->next;
    }
    now->next = list;
    now->value = n;
    // delete
    while (now != now->next) {
      for (int i = 0; i < m - 1; i++) {
        now = now->next;
      }
      // cout << "del: " << now->next->value << endl;
      now->next = now->next->next;
    }
    // output
    cout << now->value << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

bool circle[1000];
int n, m, c;

void remove(int a, int turn) {
  while (c != 1) {
    int next = (a + 1) % n;
    if (circle[a]) {
      // cout << a << ',' << turn << endl;
      if (turn == m) {
        // cout << "removing: " << a+1 << endl;
        circle[a] = false;
        c--;
        remove(next, 1);
      } else {
        remove(next, turn + 1);
      }
    } else {
      remove(next, turn);
    }
  }
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    // initialize
    for (int i = 0; i < n; i++) {
      circle[i] = true;
    }
    c = n;
    // main
    // cout << n << endl;
    remove(n - 1, 0);
    // output
    for (int i = 0; i < n; i++) {
      if (circle[i]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int pt(int n, int m) {
  int i;
  for (i = m - 1; i < (m - 1) * n; i = (m * i) / (m - 1) + 1)
    ;
  return (m * n - i);
}

int main() {
  int n, m;

  while (cin >> n >> m, n || m)
    cout << pt(n, m) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 1000

int main() {
  int n, m, player[MAX + 1];

  for (;;) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      player[i] = 1;

    int cnt = 1, endcnt = 0;
    for (int i = 1; i <= n; i++) {
      if (endcnt == n - 1)
        break;
      if (player[i] == 1)
        cnt++;
      if (i == n)
        i = 1;
      if (cnt == m) {
        player[i] = 0;
        cnt = 0;
        endcnt++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (player[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

int main() {
  int n, m, i, c, cc;
  bool f[1010] = {};

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    rep(j, 0, 1000) f[j] = 1;
    c = 0;
    cc = n;
    i = n;
    while (1) {
      i++;
      if (i == n + 1)
        i = 1;
      if (f[i] == 1 && cc == 1)
        break;
      if (f[i] == 1)
        c++;
      if (c == m) {
        f[i] = 0;
        cc--;
        c = 0;
      }
    }
    o(i);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

int main() {
  int n, m, i, c, cc;
  bool f[1000] = {};

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    c = 0;
    cc = 0;
    i = n;
    while (1) {
      i++;
      if (i == n + 1)
        i = 1;
      if (f[i] == 0 && cc == n - 1)
        break;
      if (f[i] == 0)
        c++;
      if (c == m) {
        f[i] = 1;
        cc++;
        c = 0;
      }
      if (cc == n)
        break;
    }
    o(i);
  }
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;

int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    if (n == m && n == 0)
      break;
    bool *joseph = new bool[n + 1];
    fill((bool *)joseph, (bool *)joseph + n + 1, true);

    joseph[0] = false;

    int idx = 1;
    int c = 0;
    int total = 0;
    while (1) {
      if (n - total == 1)
        break;
      if (joseph[idx % (n + 1)])
        c++;
      if (c == m) {
        joseph[idx % (n + 1)] = false;
        total++;
        c = 0;
      }
      idx++;
    }
    cout << find(joseph, joseph + n + 1, true) - joseph << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;

int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    if (n == m && n == 0)
      break;
    bool *joseph = new bool[n + 1];
    fill((bool *)joseph, (bool *)joseph + n + 1, true);

    joseph[0] = false;

    int idx = 1;
    int c = 0;
    while (1) {
      if (count(joseph, joseph + n + 1, true) == 1)
        break;

      if (joseph[idx % (n + 1)])
        c++;
      if (c == m) {
        joseph[idx % (n + 1)] = false;
        c = 0;
      }
      idx++;
    }
    cout << find(joseph, joseph + n + 1, true) - joseph << endl;
  }
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {

  int hito[1000];
  int m, n;
  int a;
  int counter = 0;
  int counter2 = 0;
  int k = 0;

  while (1) {
    cin >> n >> m;

    if (n == 0 && m == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      hito[i] = 1;
    }

    while (1) {
      if (hito[k] == 1) {
        counter++;
      }

      if (counter == m) {
        hito[k] = 0;
        counter = 0;
        counter2++;
      }

      if (counter2 == n) {
        cout << k + 1 << endl;
        counter = 0;
        counter2 = 0;
        k = 0;
        break;
      }

      k++;

      if (k >= n) {
        k = 0;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {

  int hito[1000];
  int m, n;
  int a;
  int counter = 0;
  int counter2 = 0;
  int k = 0;

  while (1) {
    cin >> n >> m;

    if (n == 0 && m == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      hito[i] = 1;
    }

    while (1) {
      if (hito[k] == 1) {
        counter++;
      }

      if (counter == m) {
        hito[k] = 0;
        counter = 0;
        counter2++;
      }

      if (counter2 == n) {
        cout << k << endl;
      }

      k++;

      if (k >= n) {
        k = 0;
      }
    }
  }

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

#include <iostream>
using namespace std;
int main() {
  int n, m, pc, count;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    int p[n + 1];
    p[0] = 0;
    pc = n;
    count = 0;
    for (int i = 1; i <= n; i++)
      p[i] = 1;
    while (pc != 1) {
      for (int i = 1; i <= n; i++) {
        if (p[i] == 1)
          count++;
        if (count == m) {
          p[i] = 0;
          pc--;
          if (pc == 1)
            break;
          count = 0;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (p[i] == 1)
        cout << i << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m, pc, count;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    int p[n + 1];
    p[0] = 0;
    pc = n;
    count = 0;
    for (int i = 1; i <= n; i++)
      p[i] = 1;
    while (pc != 1) {
      for (int i = 1; i <= n; i++) {
        if (p[i] == 1)
          count++;
        if (count == m) {
          p[i] = 0;
          pc--;
          if (pc == -1)
            break;
          count = 0;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (p[i] == 1)
        cout << i << endl;
    }
  }
  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define EPS 1e-10
using namespace std;
typedef long long llong;
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> v(n);
    // for (int i = 1; i <= n; i++)printf("%3d", i); cout << endl;
    for (int i = 0; i < n; i++)
      v[i] = 0;
    int cnt = 1;
    int tom = 0;
    int index;
    for (int i = 0; cnt != n + 1; i++) {
      if (v[i % n] == 0) {
        tom++;
      }

      if (tom == m) {
        v[i % n] = cnt++;
        index = i % n + 1;
        tom = 0;
      }
      // for (int j = 0; j < n; j++)printf("%3d", v[j]);
      // cout << "index is " << index << endl;
    }
    cout << index << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int hantei(int b, int n) {
  if (b % n == 0)
    b = n; // b??????????????????????????£???????????????????????¨????????\\??§??£??\\??????
  else
    b = b % n;
  return b;
}
int main(void) {
  int n, m, i, j, h, g, b;
  int m1;

  int a[1000] = {};

  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;

    m1 = m;
    a[0] = m1; //??¨??????????????????????????????????????£??\\??????
    for (j = 1; j < n; j++) {

      a[j] = (a[j - 1] + m) % n; // a[j]???a[j-1]??????????¶?????????°
      for (i = 1; i < m1; i++) {
        b = a[j - 1] + i;
        for (g = 0; g <= m1; g++) {
          if (hantei(b, n) == a[g]) {
            m1++;
            a[j] = (a[j] + 1) % n;
            a[j] = hantei(a[j], n);
          }
          if (g == n - 1)
            break;
        }
      }
    }
    // for(g=0;g<n;g++)printf("%d\\n",a[g]);
    printf("%d\\n", a[n - 1]);
  }
  return 0;
}
Problem p00085 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>

using namespace std;

int main() {
  const int MAX_DATAS = 1000;

  int N[MAX_DATAS], M[MAX_DATAS], CHECK[1099], ANS[MAX_DATAS], C1 = 0, C = 1,
                                                               D = 1, E = 0;

  while (true) {
    memset(CHECK, -1, sizeof(CHECK)); // -1

    C = 1;
    D = 1;
    E = 0;

    cin >> N[C1] >> M[C1];

    if (M[C1] == 1) {
      ANS[C1] = N[C1];
      goto Exit2;
    }

    if (N[C1] == 0 && M[C1] == 0) {
      goto Exit3;
    }

    while (true) {
      if (CHECK[C] % M[C1] != 0) {
        CHECK[C] = D;

        if (D % M[C1] == 0) {
          E++;
        }

        D++;
      }

      C++;

      if (C > N[C1]) {
        C -= N[C1];
      }

      if (E == N[C1]) {
        goto Exit1;
      }
    }

  Exit1:;

    for (int i = 1; i <= N[C1]; i++) {
      CHECK[i] /= M[C1];
    }

    for (int i = 1; i <= N[C1]; i++) {
      if (CHECK[i] == N[C1]) {
        ANS[C1] = i;
      }
    }

  Exit2:;

    C1++;
  }

Exit3:;

  for (int i = 0; i < C1; i++) {
    cout << ANS[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>

using namespace std;

int main() {
  const int MAX_DATAS = 1000;

  int N[MAX_DATAS], M[MAX_DATAS], CHECK[1099], ANS[MAX_DATAS], C1 = 0, C = 1,
                                                               D = 1, E = 0;

  while (true) {
    memset(CHECK, -1, sizeof(CHECK)); // -1

    C = 1;
    D = 1;
    E = 0;

    cin >> N[C1] >> M[C1];

    if (N[C1] == 0 && M[C1] == 0) {
      goto Exit2;
    }

    while (true) {
      if (CHECK[C] % M[C1] != 0) {
        CHECK[C] = D;

        if (D % M[C1] == 0) {
          E++;
        }

        D++;
      }

      C++;

      if (C > N[C1]) {
        C -= N[C1];
      }

      if (E == N[C1]) {
        goto Exit1;
      }
    }

  Exit1:;

    for (int i = 1; i <= N[C1]; i++) {
      CHECK[i] /= M[C1];
    }

    for (int i = 1; i <= N[C1]; i++) {
      if (CHECK[i] == N[C1]) {
        ANS[C1] = i;
      }
    }

    C1++;
  }

Exit2:;

  for (int i = 0; i < C1; i++) {
    cout << ANS[i] << endl;
  }

  return 0;
}
Problem p00086 (C++) — buggy submission, verdict: Runtime Error

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

// ð·_Ì¼O(®)Æ»Ìð·_©çLÑé¹Ì{ðÂ\\¢Ìðé¾B
typedef struct {
  int name;
  int miti;
} Kousa;

int main() {
  int a, b, i;
  Kousa c[102];

  // ÅÌüÍ
  while (~scanf("%d%d", &a, &b)) {
    if (a == 0 && b == 0)
      break;

    // \\¢ÌzñÌú»
    memset(c, 0, sizeof(c));

    // ±±©ç\\¢ÌzñÉãüµÄ¢­B
    c[0].miti++;
    c[0].name = a;
    for (i = 0;; i++) {
      if (c[i].name == b || c[i].name == 0) {
        c[i].name = c[i].name != 0 ? c[i].name : b;
        ++c[i].miti;
        break;
      }
    }

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

      // ó¢Ä¢é©¯¶¼O¾Á½çãüµÄA¹ðPÂ«·
      for (i = 0;; i++) {
        if (c[i].name == a || c[i].name == 0) {
          c[i].name = c[i].name != 0 ? c[i].name : a;
          ++c[i].miti;
          break;
        }
      }
      for (i = 0;; i++) {
        if (c[i].name == b || c[i].name == 0) {
          c[i].name = c[i].name != 0 ? c[i].name : b;
          ++c[i].miti;
          break;
        }
      }
    }

    a = 0; // tOú»

    // X^[gÆS[ÓOÉ¹ªï{ÌÆ±ëª êÎtOð§ÄéB
    for (i = 0; i < 102; i++) {
      //			if(c[i].name!=0)printf("%d %d
      //%d\\n",i,c[i].name,c[i].miti);
      if (c[i].name != 1 && c[i].name != 2 && c[i].miti % 2 == 1) {
        a = 1;
      }
      if (c[i].name == 1 && c[i].miti % 2 == 0) {
        a = 1;
      }
      if (c[i].name == 2 && c[i].miti % 2 == 0)
        a = 1;
    }

    if (a == 1) {
      printf("NG\\n");
    } else {
      printf("OK\\n");
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int kousa[65], a, b, i, j;

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

    memset(kousa, 0, sizeof(kousa));
    ++kousa[a];
    ++kousa[b];
    while (scanf("%d%d", &a, &b)) {
      if (a == 0 && b == 0)
        break;
      ++kousa[a];
      ++kousa[b];
    }
    a = 0;
    for (j = 3; j <= 102; j++) {
      if (kousa[j] % 2 == 1) {
        a = 1;
      }
    }
    if (a == 1) {
      printf("NG\\n");
    } else {
      printf("OK\\n");
    }
  }
  return 0;
}
Problem p00086 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int a, b;
int t[1000];
void init() {
  for (int i = 0; i < 1000; i++)
    t[i] = 0;
}
bool check() {
  if (t[1] % 2 == 0)
    return false;
  if (t[2] % 2 == 0)
    return false;
  for (int i = 3; i < 1000; i++) {
    if (t[i] % 2 == 1)
      return false;
  }
  return true;
}
int main() {
  while (cin >> a >> b) {
    init();
    t[a]++;
    t[b]++;
    while (cin >> a >> b) {
      if (a == 0 && b == 0)
        break;
      t[a]++;
      t[b]++;
    }
    if (check())
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define INF (1 << 24)
using namespace std;
int a, b, cnt;
bool flg;
int t[101][101];
vector<int> u[101];
void init();

void dfs(int x, int c) {
  int w;
  // cout<<x<<" "<<c<<endl;
  if (c == cnt && x == 2) {
    flg = 1;
  }
  if (flg == 1)
    return;

  for (int i = 0; i < (int)u[x].size(); i++) {
    w = u[x][i];
    if (t[x][w] >= 1) {
      t[x][w]--;
      t[w][x]--;
      dfs(w, c + 1);
      t[x][w]++;
      t[w][x]++;
    }
  }
}

int main() {
  init();
  while (cin >> a >> b) {
    cnt = 1;
    t[b][a]++;
    t[a][b]++;
    u[a].push_back(b);
    u[b].push_back(a);

    while (cin >> a >> b) {
      if (a == 0 && b == 0)
        break;
      u[a].push_back(b);
      u[b].push_back(a);
      t[a][b]++;
      t[b][a]++;
      cnt++;
    }

    flg = 0;

    dfs(1, 0);

    if (flg)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
    init();
  }
  return 0;
}

void init() {
  for (int i = 0; i < 101; i++) {
    u[i].clear();
    for (int j = 0; j < 101; j++) {
      t[i][j] = 0;
    }
  }
}
Problem p00086 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

vector<vector<int>> vec(101);
int n;

int main() {
  int a, b;
  while (cin >> a >> b) {
    rep(i, 101) vec[i].clear();
    vec[a - 1].push_back(b - 1);
    vec[b - 1].push_back(a - 1);
    while (cin >> a >> b && a || b) {
      vec[a - 1].push_back(b - 1);
      vec[b - 1].push_back(a - 1);
    }
    int sum = 0;
    bool check, tmp = false;
    if (vec[0].size() % 2 != 0 && vec[1].size() % 2 != 0)
      check = true;
    else if (vec[0].size() % 2 == 0 && vec[1].size() % 2 == 0)
      check = false;
    else
      tmp = true;
    rep(i, 101) if (vec[i].size() % 2 == 1) sum++;
    if (tmp)
      cout << "NG" << endl;
    else if (check) {
      if (sum == 2)
        cout << "OK" << endl;
      else
        cout << "NG" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int way[101][101];
int n;

bool dfs(int now) {
  if (now == 1) {
    bool check = true;
    rep(i, n) rep(j, n) if (way[i][j] != 0) check = false;
    if (check)
      return true;
    else
      return false;
  }
  bool hoge;
  rep(i, 50) {
    if (way[now][i] > 0) {
      way[now][i]--;
      way[i][now]--;
      hoge = dfs(i);
      way[now][i]++;
      way[i][now]++;
      if (hoge)
        return true;
    }
  }
  return false;
}

int main() {
  int a, b;
  while (cin >> a >> b) {
    rep(i, 101) {
      n = max(a, b);
      rep(j, 101) way[i][j] = 0;
    }
    way[a - 1][b - 1]++;
    way[b - 1][a - 1]++;
    while (cin >> a >> b, a || b) {
      way[a - 1][b - 1]++;
      way[b - 1][a - 1]++;
      n = max(n, max(a, b));
    }
    if (dfs(0))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}