prithivMLmods/Coder-Stat huggingface dataset:
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> //sort(all(変数),greater<型名>())で降順に、*max_element(all(変数))で最大値を獲得
#include <bits/stdc++.h>
#include <cmath> //切り上げceil(値)
#include <deque>
#include <iomanip> //setprecision(数字)
#include <iostream>
#include <list>
#include <map>
#include <numeric>
// xとyのgcdは__gcd(x,y),xとyのlcmは(x*y)/__gcd、accumulate(all(変数),0)で要素の全加算
#include <queue> //priority_queue<型名> 変数
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; 0 <= i; i--)
#define vll vector<ll>
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF 1e18
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  vi check(n - 1);
  rep(i, n - 1) check[i] = a[i + 1] - a[i];
  vector<bool> increase;
  rep(i, n - 1) {
    if (check[i] != 0) {
      increase.push_back(0 < check[i]);
    }
  }
  int ans = 1;
  rep(i, increase.size() - 1) {
    if (increase[i] != increase[i + 1]) {
      ans++;
      i++;
    }
  }
  cout << ans;
}


Fixed submission:
#include <algorithm> //sort(all(変数),greater<型名>())で降順に、*max_element(all(変数))で最大値を獲得
#include <bits/stdc++.h>
#include <cmath> //切り上げceil(値)
#include <deque>
#include <iomanip> //setprecision(数字)
#include <iostream>
#include <list>
#include <map>
#include <numeric>
// xとyのgcdは__gcd(x,y),xとyのlcmは(x*y)/__gcd、accumulate(all(変数),0)で要素の全加算
#include <queue> //priority_queue<型名> 変数
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; 0 <= i; i--)
#define vll vector<ll>
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF 1e18
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  vi check(n - 1);
  rep(i, n - 1) check[i] = a[i + 1] - a[i];
  vector<bool> increase;
  rep(i, n - 1) {
    if (check[i] != 0) {
      increase.push_back(0 < check[i]);
    }
  }
  int ans = 1;
  for (int i = 1; i < increase.size(); i++) {
    if (increase[i - 1] != increase[i]) {
      ans++;
      i++;
    }
  }
  cout << ans;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

long long MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> s;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  int cnt = 1;
  int tmp = -1;
  int flag = 0;
  for (int i = 0; i < N; i++) {
    if (flag == 1 && s[i] < tmp) {
      cnt++;
      flag = 0;
    } else if (flag == -1 && s[i] > tmp) {
      cnt++;
      flag = 0;
    } else if (flag == 0 && tmp >= 0) {
      if (s[i] > tmp) {
        flag = 1;
      } else if (s[i] < tmp) {
        flag = -1;
      }
    }
    tmp = s[i];
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

long long MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> s(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  int cnt = 1;
  int tmp = -1;
  int flag = 0;
  for (int i = 0; i < N; i++) {
    if (flag == 1 && s[i] < tmp) {
      cnt++;
      flag = 0;
    } else if (flag == -1 && s[i] > tmp) {
      cnt++;
      flag = 0;
    } else if (flag == 0 && tmp >= 0) {
      if (s[i] > tmp) {
        flag = 1;
      } else if (s[i] < tmp) {
        flag = -1;
      }
    }
    tmp = s[i];
  }
  cout << cnt << endl;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define STREP(i, s) for (int(i) = 0; (s)[(i)]; (i)++)
#define REP(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
#define ALL(c) begin(c), end(c)
#define SIZE(c) int((c).size())
#define INF 0x3f3f3f3f
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define F first
#define S second

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int n, st, res;
vi a(10011), d(10011);

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  REP(i, 1, n) cin >> a[i];
  REP(i, 2, n) {
    if (!st) {
      if (a[i] > a[i - 1])
        st = 1, res++;
      else if (a[i] < a[i - 1])
        st = -1, res++;
    } else {
      if (st == 1) {
        if (a[i] < a[i - 1])
          st = 0;
      } else {
        if (a[i] > a[i - 1])
          st = 0;
      }
    }
  }
  if (!st)
    res++;
  cout << res << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define STREP(i, s) for (int(i) = 0; (s)[(i)]; (i)++)
#define REP(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
#define ALL(c) begin(c), end(c)
#define SIZE(c) int((c).size())
#define INF 0x3f3f3f3f
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define F first
#define S second

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int n, st, res, a[100011];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  REP(i, 1, n) cin >> a[i];
  REP(i, 2, n) {
    if (!st) {
      if (a[i] > a[i - 1])
        st = 1, res++;
      else if (a[i] < a[i - 1])
        st = -1, res++;
    } else {
      if (st == 1) {
        if (a[i] < a[i - 1])
          st = 0;
      } else {
        if (a[i] > a[i - 1])
          st = 0;
      }
    }
  }
  if (!st)
    res++;
  cout << res << '\n';
}
Problem p03745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iostream>
#include <string>

#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define eps 1e-6
#define INF 0x3fffffff
#define PI acos(-1.0)
#define ll long long
#define lson l, m, (rt << 1)
#define rson m + 1, r, (rt << 1) | 1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define N 100005
int dp[N][2], a[N];
#define fr(i, x, y) for (int i = x; i <= y; i++)
#define ms(x, y) memset(x, y, sizeof(x))
int n;
void doit() {
  scanf("%d", &n);
  fr(i, 1, n) scanf("%d", &a[i]);
  ms(dp, 22);
  // printf("%d\n",dp[0][0]);
  dp[1][1] = dp[1][0] = 1;

  fr(i, 2, n) {
    if (a[i] >= a[i - 1])
      dp[i][0] = min(dp[i][0], dp[i - 1][0]);
    if (a[i] <= a[i - 1])
      dp[i][1] = min(dp[i][1], dp[i - 1][1]);
    dp[i][0] = min(dp[i][0], dp[i - 1][1] + 1);
    dp[i][0] = min(dp[i][0], dp[i - 1][0] + 1);
    dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1);
    dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1);
  }
  printf("%d\n", min(dp[n][1], dp[n][0]));
}

int main() {
  while (1) {
    doit();
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iostream>
#include <string>

#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define eps 1e-6
#define INF 0x3fffffff
#define PI acos(-1.0)
#define ll long long
#define lson l, m, (rt << 1)
#define rson m + 1, r, (rt << 1) | 1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define N 100005
int dp[N][2], a[N];
#define fr(i, x, y) for (int i = x; i <= y; i++)
#define ms(x, y) memset(x, y, sizeof(x))
int n;
void doit() {
  scanf("%d", &n);
  fr(i, 1, n) scanf("%d", &a[i]);
  ms(dp, 22);
  // printf("%d\n",dp[0][0]);
  dp[1][1] = dp[1][0] = 1;

  fr(i, 2, n) {
    if (a[i] >= a[i - 1])
      dp[i][0] = min(dp[i][0], dp[i - 1][0]);
    if (a[i] <= a[i - 1])
      dp[i][1] = min(dp[i][1], dp[i - 1][1]);
    dp[i][0] = min(dp[i][0], dp[i - 1][1] + 1);
    dp[i][0] = min(dp[i][0], dp[i - 1][0] + 1);
    dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1);
    dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1);
  }
  printf("%d\n", min(dp[n][1], dp[n][0]));
}

int main() {
  // while (1)
  { doit(); }
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define LLMAX 9223372036854775807

using namespace std;
using ll = long long;

ll checkNP(ll A) {
  if (A > 0) {
    return 1;
  } else if (A < 0) {
    return -1;
  } else {
    return 0;
  }
}

int main() {
  ll argN;
  cin >> argN;

  vector<ll> argA(argN);
  vector<ll> flip(argN, 0);
  for (ll i = 0; i < argN; ++i) {
    cin >> argA.at(i);
  }

  if (argN == 1) {
    cout << 1 << endl;
  }

  ll baseDiff = checkNP(argA.at(0) - argA.at(1));
  for (ll i = 1; i < argN - 1; ++i) {
    if (baseDiff == 0) {
      baseDiff = checkNP(argA.at(i) - argA.at(i + 1));
      continue;
    }

    if (flip.at(i - 1) == 1) {
      baseDiff = checkNP(argA.at(i) - argA.at(i + 1));
      continue;
    }

    ll tempDiff = checkNP(argA.at(i) - argA.at(i + 1));
    if (baseDiff * tempDiff < 0) {
      flip.at(i) = 1;
    }
  }

  cout << accumulate(flip.begin(), flip.end(), 0) + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define LLMAX 9223372036854775807

using namespace std;
using ll = long long;

ll checkNP(ll A) {
  if (A > 0) {
    return 1;
  } else if (A < 0) {
    return -1;
  } else {
    return 0;
  }
}

int main() {
  ll argN;
  cin >> argN;

  vector<ll> argA(argN);
  vector<ll> flip(argN, 0);
  for (ll i = 0; i < argN; ++i) {
    cin >> argA.at(i);
  }

  if (argN == 1) {
    cout << 1 << endl;
    return 0;
  }

  ll baseDiff = checkNP(argA.at(0) - argA.at(1));
  for (ll i = 1; i < argN - 1; ++i) {
    if (baseDiff == 0) {
      baseDiff = checkNP(argA.at(i) - argA.at(i + 1));
      continue;
    }

    if (flip.at(i - 1) == 1) {
      baseDiff = checkNP(argA.at(i) - argA.at(i + 1));
      continue;
    }

    ll tempDiff = checkNP(argA.at(i) - argA.at(i + 1));
    if (baseDiff * tempDiff < 0) {
      flip.at(i) = 1;
    }
  }

  cout << accumulate(flip.begin(), flip.end(), 0) + 1 << endl;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  decltype(a)::iterator result = unique(a.begin(), a.end());
  a.erase(result, a.end());

  int cnt = 1;
  for (int i = 1; i < a.size(); i++) {
    bool incre1 = a.at(i - 1) < a.at(i);
    bool incre2 = a.at(i) < a.at(i + 1);
    if (incre1 != incre2) {
      cnt++;
      i++;
    }
  }

  cout << cnt << endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  decltype(a)::iterator result = unique(a.begin(), a.end());
  a.erase(result, a.end());

  int cnt = 1;
  for (int i = 1; i < a.size() - 1; i++) {
    bool incre1 = a.at(i - 1) < a.at(i);
    bool incre2 = a.at(i) < a.at(i + 1);
    if (incre1 != incre2) {
      cnt++;
      i++;
    }
  }

  cout << cnt << endl;
}
Problem p03745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class question {
public:
  int N;
  vector<int> A;

  int res(int l) {
    if (l == N)
      return 0;
    int i = l;
    while (i < N - 1 && A[i] <= A[i + 1])
      i++;
    int j = l;
    while (j < N - 1 && A[j] >= A[j + 1])
      j++;
    return 1 + min(res(i + 1), res(j + 1));
  }

  void ans() {
    cin >> N;
    A.resize(N);
    REP(i, N) cin >> A[i];
    cout << res(0) << "\n";
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  question q;
  q.ans();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class question {
public:
  int N;
  vector<int> A;

  int res(int l) {
    if (l == N)
      return 0;
    int i = l;
    while (i < N - 1 && A[i] <= A[i + 1])
      i++;
    int j = l;
    while (j < N - 1 && A[j] >= A[j + 1])
      j++;
    return 1 + res(max(i, j) + 1);
  }

  void ans() {
    cin >> N;
    A.resize(N);
    REP(i, N) cin >> A[i];
    cout << res(0) << "\n";
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  question q;
  q.ans();

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a;
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    int ai;
    cin >> ai;
    if (ai != cur) {
      a.push_back(ai);
      cur = ai;
    }
  }
  int ans = 1;
  int dir = -1;
  for (int i = 1; i < n; ++i) {
    if (dir < 0) {
      if (a[i] > a[i - 1]) {
        dir = 1;
      } else {
        dir = 0;
      }
    } else {
      if (dir) {
        if (a[i] < a[i - 1]) {
          ++ans;
          dir = -1;
        }
      } else {
        if (a[i] > a[i - 1]) {
          ++ans;
          dir = -1;
        }
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a;
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    int ai;
    cin >> ai;
    if (ai != cur) {
      a.push_back(ai);
      cur = ai;
    }
  }
  int ans = 1;
  int dir = -1;
  for (int i = 1; i < (int)a.size(); ++i) {
    if (dir < 0) {
      if (a[i] > a[i - 1]) {
        dir = 1;
      } else {
        dir = 0;
      }
    } else {
      if (dir) {
        if (a[i] < a[i - 1]) {
          ++ans;
          dir = -1;
        }
      } else {
        if (a[i] > a[i - 1]) {
          ++ans;
          dir = -1;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  int n;
  cin >> n;
  vector<int> v;
  v.resize(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  int ret = 0;
  for (int i = 0; i < n;) {
    ret++;
    int j = i + 1;
    bool inc = true;
    bool dec = true;
    while (j < n && (inc || dec)) {
      if (v[j] > v[j - 1])
        dec = false;
      if (v[j] < v[j - 1])
        inc = false;
      if (!inc && !dec)
        break;
    }
    i = j;
  }
  cout << ret << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  int n;
  cin >> n;
  vector<int> v;
  v.resize(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  int ret = 0;
  for (int i = 0; i < n;) {
    ret++;
    int j = i + 1;
    bool inc = true;
    bool dec = true;
    while (j < n && (inc || dec)) {
      if (v[j] > v[j - 1])
        dec = false;
      if (v[j] < v[j - 1])
        inc = false;
      if (!inc && !dec)
        break;
      j++;
    }
    i = j;
  }
  cout << ret << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define pb push_back
int n, m, a, b;
bool used[111111];
vector<int> v[111111], ans;
void dfs(int x) {
  used[x] = true;
  ans.pb(x);
  for (int i : v[x]) {
    if (!used[i]) {
      dfs(i);
      return;
    }
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    v[a - 1].pb(b - 1);
    v[b - 1].pb(a - 1);
  }
  dfs(0);
  reverse(ans.begin(), ans.end());
  for (int i : v[0]) {
    if (!used[i]) {
      dfs(i);
      break;
    }
  }
  cout << ans.size() << endl;
  rep(i, ans.size()) cout << ans[i] + 1 << (i == ans.size() - 1 ? "\n" : " ");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define pb push_back
int n, m, a, b;
bool used[111111];
vector<int> v[111111], ans;
void dfs(int x) {
  used[x] = true;
  ans.pb(x);
  for (int i : v[x]) {
    if (!used[i]) {
      dfs(i);
      return;
    }
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    v[a - 1].pb(b - 1);
    v[b - 1].pb(a - 1);
  }
  dfs(0);
  reverse(ans.begin(), ans.end());
  for (int i : v[0]) {
    if (!used[i]) {
      dfs(i);
      break;
    }
  }
  cout << ans.size() << endl;
  rep(i, ans.size()) cout << ans[i] + 1 << (i == ans.size() - 1 ? "\n" : " ");
  return 0;
}
Problem p03746 (Python)

Buggy submission (status: Runtime Error):
# 入力
N, M = map(int, input().split())
A, B = zip(*(map(int, input().split()) for _ in range(M)))

# 隣接リスト表現のグラフに変換
E = [[] for _ in range(N + 1)]
for a, b in zip(A, B):
    E[a].append(b)
    E[b].append(a)

# 訪れたことがあるかどうかのフラグ
v = [i == 1 for i in range(N + 1)]


# 訪れたことのない頂点を深さ優先探索する
def dfs(i, p):
    for j in E[i]:
        if not v[j]:
            v[j] = True
            p.append(j)
            return dfs(j, p)
    else:
        return p


# Hamiltonish Path を求める
p = list(reversed(dfs(1, []))) + [1] + dfs(1, [])

# 出力
ans = "{}\n{}".format(len(p), " ".join(map(str, p)))
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(2 * 10**5)

# 入力
N, M = map(int, input().split())
A, B = zip(*(map(int, input().split()) for _ in range(M)))

# 隣接リスト表現のグラフに変換
E = [[] for _ in range(N + 1)]
for a, b in zip(A, B):
    E[a].append(b)
    E[b].append(a)

# 訪れたことがあるかどうかのフラグ
v = [i == 1 for i in range(N + 1)]


# 訪れたことのない頂点を深さ優先探索する
def dfs(i, p):
    for j in E[i]:
        if not v[j]:
            v[j] = True
            p.append(j)
            return dfs(j, p)
    else:
        return p


# Hamiltonish Path を求める
p = list(reversed(dfs(1, []))) + [1] + dfs(1, [])

# 出力
ans = "{}\n{}".format(len(p), " ".join(map(str, p)))
print(ans)
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100005;
int n, m, i, j, k, head[N], adj[N * 2], nxt[N * 2], p[N], cnt;
bool v[N];
void dfs(int x) {
  v[x] = true;
  for (int y = head[x]; y; y = nxt[y])
    if (!v[adj[y]]) {
      dfs(adj[y]);
      break;
    }
  p[++cnt] = x;
}
void Dfs(int x) {
  if (x != 1)
    p[++cnt] = x;
  for (int y = head[x]; y; y = nxt[y])
    if (!v[adj[y]]) {
      Dfs(adj[y]);
      break;
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d", &j, &k);
    adj[i * 2 - 1] = k;
    nxt[i * 2 - 1] = head[j];
    head[j] = i * 2 - 1;
    adj[i * 2] = j;
    nxt[i * 2] = head[k];
    head[k] = i * 2;
  }
  dfs(1);
  Dfs(1);
  printf("%d\n", cnt);
  for (i = 1; i <= cnt; ++i)
    printf("%d ", p[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100005;
int n, m, i, j, k, head[N], adj[N * 2], nxt[N * 2], p[N], cnt;
bool v[N];
void dfs(int x) {
  v[x] = true;
  for (int y = head[x]; y; y = nxt[y])
    if (!v[adj[y]]) {
      dfs(adj[y]);
      break;
    }
  p[++cnt] = x;
}
void Dfs(int x) {
  if (x != 1)
    p[++cnt] = x;
  v[x] = true;
  for (int y = head[x]; y; y = nxt[y])
    if (!v[adj[y]]) {
      Dfs(adj[y]);
      break;
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d", &j, &k);
    adj[i * 2 - 1] = k;
    nxt[i * 2 - 1] = head[j];
    head[j] = i * 2 - 1;
    adj[i * 2] = j;
    nxt[i * 2] = head[k];
    head[k] = i * 2;
  }
  dfs(1);
  Dfs(1);
  printf("%d\n", cnt);
  for (i = 1; i <= cnt; ++i)
    printf("%d ", p[i]);
  return 0;
}
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

ll N, M;
vector<ll> g[MAX];
vector<ll> ps;
ll used[MAX];

void dfs(ll u) {
  used[u] = 1;
  ps.push_back(u);

  for (auto v : g[u])
    if (!used[v]) {
      dfs(v);
      return;
    }
}

int main(void) {
  ll i, j, k;

  cin >> N >> M;

  for (i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  dfs(0);
  reverse(ps.begin(), ps.end());
  ps.pop_back();
  dfs(0);

  pt(ps.size());
  for (i = 0; i < N; i++)
    pt(ps[i] + 1);
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

ll N, M;
vector<ll> g[MAX];
vector<ll> ps;
ll used[MAX];

void dfs(ll u) {
  used[u] = 1;
  ps.push_back(u);

  for (auto v : g[u])
    if (!used[v]) {
      dfs(v);
      return;
    }
}

int main(void) {
  ll i, j, k;

  cin >> N >> M;

  for (i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  dfs(0);
  reverse(ps.begin(), ps.end());
  ps.pop_back();
  dfs(0);

  pt(ps.size());
  for (i = 0; i < ps.size(); i++)
    pt(ps[i] + 1);
}
Problem p03746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()), ((V).end())
#define revv(V) reverse(allv(V))
using namespace std;
void fg(vector<int> G[], int a, int b) {
  G[a].pb(b);
  G[b].pb(a);
}

const int MAXN = 100005;
const int MAXM = 100005;

vector<int> G[MAXN];

vector<int> Ans;

int A[MAXM], B[MAXM];
bitset<MAXN> chk;

int N, M;

void f(vector<int> &V, int i) {
  chk[i] = true;
  V.pb(i);
  for (int v : G[i])
    if (!chk[v]) {
      f(V, v);
      return;
    }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    cin >> A[i] >> B[i];
    fg(G, A[i], B[i]);
  }

  {
    vector<int> V;
    f(V, 1);
    Ans = V;
  }

  {
    bool flag = false;
    for (int v : G[1])
      flag |= (!chk[v]);
    if (!flag) {
      printf("%d\n", sz(Ans));
      for (int v : Ans)
        printf("%d ", v);
      puts("");
      return 0;
    }
  }

  revv(Ans);

  for (int i = 1;;) {
    int j = -1;
    for (int v : G[i])
      if (!chk[v]) {
        j = v;
        break;
      }
    if (j < 0)
      break;
    Ans.pb(j);
    i = j;
  }

  printf("%d\n", sz(Ans));
  for (int v : Ans)
    printf("%d ", v);
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()), ((V).end())
#define revv(V) reverse(allv(V))
using namespace std;
void fg(vector<int> G[], int a, int b) {
  G[a].pb(b);
  G[b].pb(a);
}

const int MAXN = 100005;
const int MAXM = 100005;

vector<int> G[MAXN];

vector<int> Ans;

int A[MAXM], B[MAXM];
bitset<MAXN> chk;

int N, M;

void f(vector<int> &V, int i) {
  chk[i] = true;
  V.pb(i);
  for (int v : G[i])
    if (!chk[v]) {
      f(V, v);
      return;
    }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    cin >> A[i] >> B[i];
    fg(G, A[i], B[i]);
  }

  {
    vector<int> V;
    f(V, 1);
    Ans = V;
  }

  {
    bool flag = false;
    for (int v : G[1])
      flag |= (!chk[v]);
    if (!flag) {
      printf("%d\n", sz(Ans));
      for (int v : Ans)
        printf("%d ", v);
      puts("");
      return 0;
    }
  }

  revv(Ans);

  for (int i = 1;;) {
    int j = -1;
    for (int v : G[i])
      if (!chk[v]) {
        j = v;
        break;
      }
    if (j < 0)
      break;
    chk[j] = true;
    Ans.pb(j);
    i = j;
  }

  printf("%d\n", sz(Ans));
  for (int v : Ans)
    printf("%d ", v);
  puts("");
  return 0;
}
Problem p03746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
using namespace std;

int main() {
#ifdef LOCAL_RUN
  freopen("task.in", "r", stdin);
  freopen("task.out", "w", stdout);
// freopen("task.err", "w", stderr);
#endif // ONLINE_JUDGE
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> graph(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  vector<int> p1 = {0}, p2 = {0};
  vector<bool> used(n, false);
  used[0] = true;
  while (true) {
    int node = p1.back();
    int next = -1;
    for (int to : graph[node]) {
      if (!used[to]) {
        next = to;
        break;
      }
    }
    if (next != -1) {
      p1.push_back(next);
      used[next] = true;
    } else {
      break;
    }
  }
  while (true) {
    int node = p2.back();
    int next = -1;
    for (int to : graph[node]) {
      if (!used[to]) {
        next = to;
        break;
      }
    }
    if (next != -1) {
      p2.push_back(next);
    } else {
      break;
    }
  }
  vector<int> fans = p1;
  reverse(fans.begin(), fans.end());
  fans.insert(fans.end(), p2.begin() + 1, p2.end());
  cout << SZ(fans) << '\n';
  for (int node : fans) {
    cout << node + 1 << ' ';
  }
  cout << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
using namespace std;

int main() {
#ifdef LOCAL_RUN
  freopen("task.in", "r", stdin);
  freopen("task.out", "w", stdout);
// freopen("task.err", "w", stderr);
#endif // ONLINE_JUDGE
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> graph(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  vector<int> p1 = {0}, p2 = {0};
  vector<bool> used(n, false);
  used[0] = true;
  while (true) {
    int node = p1.back();
    int next = -1;
    for (int to : graph[node]) {
      if (!used[to]) {
        next = to;
        break;
      }
    }
    if (next != -1) {
      p1.push_back(next);
      used[next] = true;
    } else {
      break;
    }
  }
  while (true) {
    int node = p2.back();
    int next = -1;
    for (int to : graph[node]) {
      if (!used[to]) {
        next = to;
        break;
      }
    }
    if (next != -1) {
      p2.push_back(next);
      used[next] = true;
    } else {
      break;
    }
  }
  vector<int> fans = p1;
  reverse(fans.begin(), fans.end());
  fans.insert(fans.end(), p2.begin() + 1, p2.end());
  cout << SZ(fans) << '\n';
  for (int node : fans) {
    cout << node + 1 << ' ';
  }
  cout << '\n';
}
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vec(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
    vec[b].pb(a);
  }

  vector<bool> used(n, false);
  deque<int> d;

  d.pb(0);
  used[0] = true;
  auto search = [&](int p) {
    for (auto to : vec[p]) {
      if (!used[to]) {
        used[to] = true;
        return to;
      }
    }
    return -1;
  };
  int r;
  while ((r = search(d.front())) != -1)
    d.push_front(r);
  while ((r = search(d.back())) != -1)
    d.push_back(r);

  vector<int> res;
  for (auto i : d)
    res.pb(i);
  int s = res.size();
  cout << s << endl;
  rep(i, n) cout << res[i] + 1 << " \n"[i == s - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vec(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
    vec[b].pb(a);
  }

  vector<bool> used(n, false);
  deque<int> d;

  d.pb(0);
  used[0] = true;
  auto search = [&](int p) {
    for (auto to : vec[p]) {
      if (!used[to]) {
        used[to] = true;
        return to;
      }
    }
    return -1;
  };
  int r;
  while ((r = search(d.front())) != -1)
    d.push_front(r);
  while ((r = search(d.back())) != -1)
    d.push_back(r);

  vector<int> res;
  for (auto i : d)
    res.pb(i);
  int s = res.size();
  cout << s << endl;
  rep(i, s) cout << res[i] + 1 << " \n"[i == s - 1];

  return 0;
}
Problem p03746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <deque>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<int>> g(n);
  deque<int> ans;
  vector<int> used(n);
  int from, to;
  scanf("%d%d", &from, &to);
  --from;
  --to;
  ans.push_back(from);
  ans.push_back(to);
  g[from].push_back(to);
  g[to].push_back(from);
  used[from] = 1;
  used[to] = 1;
  for (int i = 1; i < m; ++i) {
    scanf("%d%d", &from, &to);
    --from;
    --to;
    g[from].push_back(to);
    g[to].push_back(from);
  }
  while (true) {
    int one = ans[0];
    int flag = 0;
    for (size_t j = 0; j < g[one].size(); ++j) {
      to = g[one][j];
      if (!used[to]) {
        flag = 1;
        ans.push_front(to);
        used[to] = 1;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  while (true) {
    int one = ans[ans.size() - 1];
    int flag = 0;
    for (size_t j = 0; j < g[one].size(); ++j) {
      to = g[one][j];
      if (!used[to]) {
        flag = 1;
        ans.push_back(to);
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  printf("%zu\n", ans.size());
  for (auto &el : ans) {
    printf("%d ", el + 1);
  }
  printf("\n");
}


Fixed submission:
#include <deque>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<int>> g(n);
  deque<int> ans;
  vector<int> used(n);
  int from, to;
  scanf("%d%d", &from, &to);
  --from;
  --to;
  ans.push_back(from);
  ans.push_back(to);
  g[from].push_back(to);
  g[to].push_back(from);
  used[from] = 1;
  used[to] = 1;
  for (int i = 1; i < m; ++i) {
    scanf("%d%d", &from, &to);
    --from;
    --to;
    g[from].push_back(to);
    g[to].push_back(from);
  }
  while (true) {
    int one = ans[0];
    int flag = 0;
    for (size_t j = 0; j < g[one].size(); ++j) {
      to = g[one][j];
      if (!used[to]) {
        flag = 1;
        ans.push_front(to);
        used[to] = 1;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  while (true) {
    int one = ans[ans.size() - 1];
    int flag = 0;
    for (size_t j = 0; j < g[one].size(); ++j) {
      to = g[one][j];
      if (!used[to]) {
        flag = 1;
        ans.push_back(to);
        used[to] = 1;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  printf("%zu\n", ans.size());
  for (auto &el : ans) {
    printf("%d ", el + 1);
  }
  printf("\n");
}
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

deque<int> path;
int n, m, a, b;
vector<int> adj[10005];
bool onPath[10005];

int main() {
  cin >> n >> m;
  memset(onPath, 0, sizeof(bool) * (n + 1));
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  path.push_front(1);
  onPath[1] = true;
  bool good = false;
  while (!good) {
    good = true;
    int front = path.front();
    for (int i : adj[front]) {
      if (!onPath[i]) {
        path.push_front(i);
        onPath[i] = true;
        good = false;
        break;
      }
    }
    int back = path.back();
    for (int i : adj[back]) {
      if (!onPath[i]) {
        path.push_back(i);
        onPath[i] = true;
        good = false;
        break;
      }
    }
  }
  cout << path.size() << endl;
  while (!path.empty()) {
    int front = path.front();
    path.pop_front();
    cout << front << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

deque<int> path;
int n, m, a, b;
vector<int> adj[100005];
bool onPath[100005];

int main() {
  cin >> n >> m;
  memset(onPath, 0, sizeof(bool) * (n + 1));
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  path.push_front(1);
  onPath[1] = true;
  bool good = false;
  while (!good) {
    good = true;
    int front = path.front();
    for (int i : adj[front]) {
      if (!onPath[i]) {
        path.push_front(i);
        onPath[i] = true;
        good = false;
        break;
      }
    }
    int back = path.back();
    for (int i : adj[back]) {
      if (!onPath[i]) {
        path.push_back(i);
        onPath[i] = true;
        good = false;
        break;
      }
    }
  }
  cout << path.size() << endl;
  while (!path.empty()) {
    int front = path.front();
    path.pop_front();
    cout << front << " ";
  }
  cout << endl;
  return 0;
}
Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// ROAD to... Red

using namespace std;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n, u[N], m;
vector<int> g[N];
vector<int> ans;

void dfs(int v) {
  if (!u[v])
    ans.pb(v);

  for (auto to : g[v]) {
    if (!u[to]) {
      dfs(to);
      return;
    }
  }
}

int main() {
  cin >> n >> m;
  For(i, 1, m) {
    int l, r;
    cin >> l >> r;
    g[r].pb(l);
    g[l].pb(r);
  }
  dfs(1);
  reverse(every(ans));
  dfs(1);

  cout << ans.size() << '\n';
  for (auto it : ans)
    cout << it << ' ';
  return Accepted;
}

// Coded By OB

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// ROAD to... Red

using namespace std;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n, u[N], m;
vector<int> g[N];
vector<int> ans;

void dfs(int v) {
  if (!u[v])
    ans.pb(v);
  u[v] = 1;
  for (auto to : g[v]) {
    if (!u[to]) {
      dfs(to);
      return;
    }
  }
}

int main() {
  cin >> n >> m;
  For(i, 1, m) {
    int l, r;
    cin >> l >> r;
    g[r].pb(l);
    g[l].pb(r);
  }
  dfs(1);
  reverse(every(ans));
  dfs(1);

  cout << ans.size() << '\n';
  for (auto it : ans)
    cout << it << ' ';
  return Accepted;
}

// Coded By OB
Problem p03747 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define For(i, j, k) for (int i = j; i <= k; i++)

using namespace std;

const int N = 100010;

int pos[N], dir[N], x[N], ans[N];
int n, m, T;

int main() {
  scanf("%d%d%d", &n, &m, &T);
  For(i, 1, n) {
    scanf("%d%d", &x[i], &dir[i]);
    pos[i] = dir[i] == 1 ? (x[i] + T) % m : ((x[i] - T) % m + m) % m;
  }
  long long ret = 0;
  For(i, 2, n) if (dir[i] ^ dir[1]) {
    int delt = 0;
    if (dir[1] == 1)
      delt = x[i] - x[1];
    else
      delt = (x[1] + m - x[i]) % m;
    ret += (2 * T + m - delt) / m;
  }
  ret %= n;
  if (dir[1] == 2)
    ret = (n - ret) % n;

  int p = pos[1];

  sort(pos + 1, pos + n + 1);
  if (dir[1] == 2)
    p = int(lower_bound(pos + 1, pos + n + 1, p) - pos);
  else
    p = int(upper_bound(pos + 1, pos + n + 1, p) - pos) - 1;

  --p;
  For(i, 0, n - 1) ans[(ret + i) % n] = pos[(p + i) % n + 1];
  For(i, 0, n - 1) printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define For(i, j, k) for (int i = j; i <= k; i++)

using namespace std;

const int N = 100010;

int pos[N], dir[N], x[N], ans[N];
int n, m, T;

int main() {
  scanf("%d%d%d", &n, &m, &T);
  For(i, 1, n) {
    scanf("%d%d", &x[i], &dir[i]);
    pos[i] = dir[i] == 1 ? (x[i] + T) % m : ((x[i] - T) % m + m) % m;
  }
  long long ret = 0;
  For(i, 2, n) if (dir[i] ^ dir[1]) {
    int delt = 0;
    if (dir[1] == 1)
      delt = x[i] - x[1];
    else
      delt = (x[1] + m - x[i]) % m;
    ret += (2ll * T + m - delt) / m;
  }
  ret %= n;
  if (dir[1] == 2)
    ret = (n - ret) % n;

  int p = pos[1];

  sort(pos + 1, pos + n + 1);
  if (dir[1] == 2)
    p = int(lower_bound(pos + 1, pos + n + 1, p) - pos);
  else
    p = int(upper_bound(pos + 1, pos + n + 1, p) - pos) - 1;

  --p;
  For(i, 0, n - 1) ans[(ret + i) % n] = pos[(p + i) % n + 1];
  For(i, 0, n - 1) printf("%d\n", ans[i]);
  return 0;
}
Problem p03747 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAX = 1e5 + 5;
int a[MAX];
int n, l, t;

int main() {
  cin >> n >> l >> t;
  int p = 0;
  for (int i = 0; i < n; ++i) {
    int x, w;
    cin >> x >> w;
    if (w == 2)
      w = -1;
    x = x + t * w;
    p += x / l;
    a[i] = x % l;
    if (a[i] < 0)
      --p, a[i] += l;
  }
  sort(a, a + n);
  for (int i = 0; i < n; ++i)
    cout << a[(i + p + n) % n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAX = 1e5 + 5;
int a[MAX];
int n, l, t;

int main() {
  cin >> n >> l >> t;
  int p = 0;
  for (int i = 0; i < n; ++i) {
    int x, w;
    cin >> x >> w;
    if (w == 2)
      w = -1;
    x = x + t * w;
    p += x / l;
    a[i] = x % l;
    if (a[i] < 0)
      --p, a[i] += l;
  }
  sort(a, a + n);
  p %= n;
  for (int i = 0; i < n; ++i)
    cout << a[(i + p + n) % n] << endl;
  return 0;
}
Problem p03747 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, t, l, x[N], w[N], ans[N], q[N];
typedef pair<int, int> pii;
pii p[N];
bool cmp(int a, int b) { return p[a] < p[b]; }
int main() {
  scanf("%d%d%d", &n, &l, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &w[i]);
    if (w[i] == 2)
      w[i] = -1;
    p[i] = pii((l + (x[i] + w[i] * t) % l) % l, w[i]);
    q[i] = i;
  }
  sort(q + 1, q + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    q[i + n] = q[i];
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (w[i] != w[1]) {
      int len = 2 * t, dis = (w[1] * (x[i] - x[1]) + l) % l;
      if (len >= dis)
        len -= dis, cnt++;
      cnt += len / l;
    }
  int id = (n + (1 + w[1] * cnt) % n) % n, pos;
  if (id == 0)
    id = n;
  for (int i = 1; i <= n; i++)
    if (q[i] == 1)
      pos = i;
  for (int i = 0; i < n; i++)
    ans[id] = p[q[pos + i]].first, id = (id == n ? 1 : id + 1);
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, t, l, x[N], w[N], ans[N], q[N];
typedef pair<int, int> pii;
pii p[N];
bool cmp(int a, int b) { return p[a] < p[b]; }
int main() {
  scanf("%d%d%d", &n, &l, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &w[i]);
    if (w[i] == 2)
      w[i] = -1;
    p[i] = pii((l + (x[i] + w[i] * t) % l) % l, w[i]);
    q[i] = i;
  }
  sort(q + 1, q + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    q[i + n] = q[i];
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (w[i] != w[1]) {
      int len = 2 * t, dis = (w[1] * (x[i] - x[1]) + l) % l;
      if (len >= dis)
        len -= dis, cnt++;
      cnt += len / l;
    }
  int id = (n + (1 + w[1] * cnt) % n) % n, pos;
  if (id == 0)
    id = n;
  for (int i = 1; i <= n; i++)
    if (q[i] == 1)
      pos = i;
  for (int i = 0; i < n; i++)
    ans[id] = p[q[pos + i]].first, id = (id == n ? 1 : id + 1);
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03747 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[100002];
int main() {
  int n, l, t;
  scanf("%d %d %d", &n, &l, &t);
  long long cur = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d %d", a + i, &temp);
    if (temp == 1) {
      cur += (a[i] + t) / l;
      a[i] = (a[i] + t) % l;
    } else {
      cur += (a[i] - t) / l;
      a[i] = (a[i] - t) % l;
      if (a[i] < 0) { // mutiple circle (1 - 8) / 4 => cur = cur - 2
        a[i] += l;
        cur--;
      }
    }
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    printf("%d\n", a[(i + cur) % n]);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[100002];
int main() {
  int n, l, t;
  scanf("%d %d %d", &n, &l, &t);
  long long cur = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d %d", a + i, &temp);
    if (temp == 1) {
      cur += (a[i] + t) / l;
      a[i] = (a[i] + t) % l;
    } else {
      cur += (a[i] - t) / l;
      a[i] = (a[i] - t) % l;
      if (a[i] < 0) { // mutiple circle (1 - 8) / 4 => cur = cur - 2
        a[i] += l;
        cur--;
      }
    }
  }
  sort(a, a + n);
  cur = cur % n;
  if (cur < 0)
    cur = cur + n;
  for (int i = 0; i < n; i++) {
    printf("%d\n", a[(i + cur) % n]);
  }

  return 0;
}
Problem p03747 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
using namespace std;
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - 48;
  return f ? -x : x;
}
int n, L, T, a[N], p;
int main() {
  freopen("ants.in", "r", stdin);
  freopen("ants.out", "w", stdout);
  n = read();
  L = read();
  T = read();
  For(i, 1, n) {
    int x = read(), y = read();
    if (y == 1) {
      a[i] = (x + T) % L;
      p = (p + (x + T - a[i]) / L) % n;
    } else {
      a[i] = (x - T) % L;
      if (a[i] < 0)
        a[i] += L;
      p = (p + (x - T - a[i]) / L) % n;
      if (p < 0)
        p += n;
    }
  }
  sort(a + 1, a + 1 + n);
  For(i, p + 1, n) cout << a[i] << endl;
  For(i, 1, p) cout << a[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
using namespace std;
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - 48;
  return f ? -x : x;
}
int n, L, T, a[N], p;
int main() {
  n = read();
  L = read();
  T = read();
  For(i, 1, n) {
    int x = read(), y = read();
    if (y == 1) {
      a[i] = (x + T) % L;
      p = (p + (x + T - a[i]) / L) % n;
    } else {
      a[i] = (x - T) % L;
      if (a[i] < 0)
        a[i] += L;
      p = (p + (x - T - a[i]) / L) % n;
      if (p < 0)
        p += n;
    }
  }
  sort(a + 1, a + 1 + n);
  For(i, p + 1, n) cout << a[i] << endl;
  For(i, 1, p) cout << a[i] << endl;
  return 0;
}