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

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

int mod = 1000000007;
long long dp[5010][5010];

int main() {
  vector<int> mov;
  int N, K, M, Q;
  cin >> N >> M >> K >> Q;
  for (int i = 0; i < N; i++) {
    if ((i / K) % 2 == 0) {
      mov.push_back(i);
      // cerr << i << endl;
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i <= 5000; i++) {
    int tot[5010] = {};

    // for(int k = 0 ; k < N ; k++){
    // printf("%3d",dp[i][k]);
    // cout << " ";
    // }
    // cout << endl;
    // for(int k = 0 ; k < N ; k++) cout << (k/K%2==0?"  ^":"   ") << " ";
    // cout << endl;

    for (int k = 0; k < mov.size(); k++) {
      tot[0] += dp[i][mov[k] % N];
      tot[0] %= mod;
    }

    for (int j = 1; j < 2 * K; j++) {
      tot[j] = tot[j - 1];
      int c = -1;
      for (int k = 0; k < N; k += K) {
        tot[j] += c * dp[i][(k + j - 1) % N];
        tot[j] %= mod;
        c *= -1;
      }
    }

    // reverse(tot,tot+2*K);
    //  for(int j = 0 ; j < 2*K ; j++) cout << tot[j] << " " ;
    //  cout << endl;

    for (int j = 0; j < N; j++) {
      dp[i + 1][j] += tot[(K + 1 + j) % (2 * K)];
      dp[i + 1][j] %= mod;
    }
  }
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    int d = ((c - a) % N + N) % N;
    cout << (dp[b][d] % mod + mod) % mod << endl;
  }
  // for(int i = 0 ; i < 15 ; i++){
  // for(int j = 0 ; j < 15 ; j++){
  // printf("%10d ",(dp[i][j]+mod)%mod);
  // }
  // cout << endl;
  // }
}

Error: TLE

Fixed submission:

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

int mod = 1000000007;
int res[100010];
int dp[5010][5010];

int main() {
  vector<int> mov;
  int N, K, M, Q;
  cin >> N >> M >> K >> Q;
  for (int i = 0; i < N; i++) {
    if ((i / K) % 2 == 0) {
      mov.push_back(i);
      // cerr << i << endl;
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i <= 5000; i++) {
    int tot[5010] = {};
    for (int j = 0; j < 2 * K; j++) {
      for (int k = 0; k < mov.size(); k++) {
        tot[j] += dp[i][(j - mov[k] + N) % N];
        tot[j] %= mod;
      }
    }
    // for(int j = 0 ; j < K ; j++) cout << tot[j] << " " ;
    // cout << endl;

    for (int j = 0; j < N; j++) {
      dp[i + 1][j] += tot[j % (2 * K)];
      dp[i + 1][j] %= mod;
    }
  }
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    int d = ((c - a) % N + N) % N;
    cout << dp[b][d] << endl;
  }
  /*for(int i = 0 ; i < 15 ; i++){
          for(int j = 0 ; j < 15 ; j++){
                  printf("%10d ",dp[i][j]);
          }
          cout << endl;
  }*/
}
Problem p01054 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#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;
int main() {
  int n;
  string a, b;
  cin >> n >> a >> b;

  int cnt1[256] = {0};
  rep(i, n) cnt1[(char)a[i]]++;
  int cnt2[256] = {0};
  rep(i, n) cnt2[(char)b[i]]++;

  vector<int> A, B;
  rep(i, 256) if (cnt1[i]) A.push_back(cnt1[i]);
  rep(i, 256) if (cnt2[i]) B.push_back(cnt2[i]);

  n = max(A.size(), B.size());
  while (A.size() != n)
    A.push_back(0);
  while (B.size() != n)
    B.push_back(0);
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  ////////////////////////////
  /*rep(i,n){
    rep(j,n){
      if(B[i]==A[j]){
        A.erase(A.begin()+j);
        B.erase(B.begin()+i);
        i--;j--;
        break;
      }
    }
  }*/
  int out = 0;
  rep(i, A.size()) {
    if (B[i] > A[i]) {
      out += B[i] - A[i];
    }
  }
  cout << out << endl;
  /*
  rep(i,A.size())cout<<A[i]<<endl;
  cout<<endl;
  rep(i,B.size())cout<<B[i]<<endl;
  */
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#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;
int main() {
  int n;
  string a, b;
  cin >> n >> a >> b;
  int cnt1[256] = {0};
  rep(i, n) cnt1[a[i]]++;
  int cnt2[256] = {0};
  rep(i, n) cnt2[b[i]]++;
  vector<int> A, B;
  rep(i, 256) if (cnt1[i]) A.push_back(cnt1[i]);
  rep(i, 256) if (cnt2[i]) B.push_back(cnt2[i]);
  n = max(A.size(), B.size());
  while (A.size() != n)
    A.push_back(0);
  while (B.size() != n)
    B.push_back(0);
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  rep(i, n) {
    rep(j, n) {
      if (B[i] == A[j]) {
        A.erase(A.begin() + j);
        B.erase(B.begin() + i);
        i--;
        j--;
        break;
      }
    }
  }
  int out = 0;
  rep(i, n) {
    if (B[i] > A[i]) {
      out += B[i] - A[i];
    }
  }
  cout << out << endl;
  /*
  rep(i,A.size())cout<<A[i]<<endl;
  cout<<endl;
  rep(i,B.size())cout<<B[i]<<endl;
  */
}
Problem p01054 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1 << 28;

int main() {
  int n;
  cin >> n;
  string S, T;
  cin >> S >> T;

  vector<int> s(256), t(256);
  for (int i = 0; i < n; i++) {
    s[S[i]]++;
    t[T[i]]++;
  }

  sort(s.begin(), s.end(), greater<int>());
  sort(t.begin(), t.end(), greater<int>());

  int res = 0;
  for (int i = 0; i < t.size() && i < s.size(); i++) {
    res += abs(t[i] - s[i]);
  }

  cout << res / 2 << endl;

  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1 << 28;

int main() {
  int n;
  cin >> n;
  string S, T;
  cin >> S >> T;

  vector<int> s(256), t(256);
  for (int i = 0; i < n; i++) {
    s[S[i]]++;
    t[T[i]]++;
  }

  for (int i = 'a'; i <= 'z'; i++) {
    for (int j = 'a'; j <= 'z'; j++) {
      if (s[i] == t[j]) {
        t[j] = 0;
      }
    }
  }

  vector<int> cs, ct;
  for (int i = 'a'; i <= 'z'; i++) {
    if (s[i] > 0)
      cs.push_back(s[i]);
    if (t[i] > 0)
      ct.push_back(t[i]);
  }

  sort(cs.begin(), cs.end());
  sort(ct.begin(), ct.end());

  int res = 0;
  for (int i = 0; i < ct.size(); i++) {
    int c = -1;
    int sub = INF;
    for (int j = 0; j < cs.size(); j++) {
      if (cs[j] != INF && abs(ct[i] - cs[j]) < sub) {
        sub = abs(ct[i] - cs[j]);
        c = j;
      }
    }

    cs[c] = INF;
    res += sub;
  }

  cout << res << endl;

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

#include <bits/stdc++.h>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define FORD(x, y, z) for (int x = y; x >= z; x--)
#define MSET(x, y) memset(x, y, sizeof(x))
#define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 1000005
void RI() {}
template <typename... T> void RI(int &head, T &...tail) {
  scanf("%d", &head);
  RI(tail...);
}
using namespace std;
typedef long long LL;
int n, c1[30], c2[30];
char buf[M];
int main() {
  RI(n);

  scanf("%s", buf + 1);
  REP(i, 1, n) c1[buf[i] - 'a']++;

  scanf("%s", buf + 1);
  REP(i, 1, n) c2[buf[i] - 'a']++;

  sort(c1, c1 + 26);
  sort(c2, c2 + 26);
  int ans = 0;
  REP(i, 0, 25) ans += abs(c1[i] - c2[i]);
  printf("%d\\n", ans / 2);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define FORD(x, y, z) for (int x = y; x >= z; x--)
#define MSET(x, y) memset(x, y, sizeof(x))
#define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
void RI() {}
template <typename... T> void RI(int &head, T &...tail) {
  scanf("%d", &head);
  RI(tail...);
}
using namespace std;
typedef long long LL;
int n, c1[26], c2[26];
char buf[M];
int main() {
  RI(n);
  scanf("%s", buf + 1);
  REP(i, 1, n) c1[buf[i] - 'a']++;
  scanf("%s", buf + 1);
  REP(i, 1, n) c2[buf[i] - 'a']++;

  sort(c1, c1 + 26);
  sort(c2, c2 + 26);
  int ans = 0;
  REP(i, 0, 25) ans += abs(c1[i] - c2[i]);
  printf("%d\\n", ans / 2);
  return 0;
}
Problem p01057 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/rope>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define FORD(x, y, z) for (int x = y; x >= z; x--)
#define MSET(x, y) memset(x, y, sizeof(x))
#define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 500005
void RI() {}
template <typename... T> void RI(int &head, T &...tail) {
  scanf("%d", &head);
  RI(tail...);
}
using namespace std;
using namespace __gnu_cxx;
typedef long long LL;
int n, q;
rope<char> in[M], t1, t2;
char buf[M * 2];
int main() {
  int a, b, c, d;
  int z;
  RI(n, q);
  REP(i, 1, n) {
    scanf("%s", buf);
    in[i] = buf;
  }

  while (q--) {
    RI(z);
    if (z == 1) {
      RI(a, b, c, d);

      t1 = in[a].substr(b - 1, in[a].length() - b + 1);
      t2 = in[c].substr(d - 1, in[c].length() - d + 1);

      in[a] = in[a].substr(0, b - 1) + t2;
      in[c] = in[c].substr(0, d - 1) + t1;
    } else {
      RI(a, b);
      scanf("%s", buf);

      t1 = buf;

      int len = in[a].length();
      in[a] = in[a].substr(0, b - 1) + t1 + in[a].substr(b, len - b);
    }
  }

  REP(i, 1, n) printf("%s\\n", in[i].c_str());
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/rope>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define FORD(x, y, z) for (int x = y; x >= z; x--)
#define MSET(x, y) memset(x, y, sizeof(x))
#define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 200005
void RI() {}
template <typename... T> void RI(int &head, T &...tail) {
  scanf("%d", &head);
  RI(tail...);
}
using namespace std;
using namespace __gnu_cxx;
typedef long long LL;
int n, q;
rope<char> in[M], t1, t2;
char buf[M * 2];
int main() {
  int a, b, c, d;
  int z;
  RI(n, q);
  REP(i, 1, n) {
    scanf("%s", buf);
    in[i] = buf;
  }

  while (q--) {
    RI(z);
    if (z == 1) {
      RI(a, b, c, d);

      t1 = in[a].substr(b - 1, in[a].length() - b + 1);
      t2 = in[c].substr(d - 1, in[c].length() - d + 1);

      in[a] = in[a].substr(0, b - 1) + t2;
      in[c] = in[c].substr(0, d - 1) + t1;
    } else {
      RI(a, b);
      scanf("%s", buf);

      t1 = buf;

      int len = in[a].length();
      in[a] = in[a].substr(0, b - 1) + t1 + in[a].substr(b, len - b);
    }
  }

  REP(i, 1, n) printf("%s\\n", in[i].c_str());
  return 0;
}
Problem p01060 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

int w, h, n;

bool adj(int a, int b) {
  if (0 <= a && a <= w - 2 && 0 <= b && b <= w - 2) {
    return a == b;
  }

  if (w <= a && a <= w + h - 3 && w - 2 <= b && b <= w - 2 + h - 3) {
    return a - w == b - (w - 2);
  }

  if (w + h - 1 <= a && w - 2 + h - 3 <= b) {
    return a - (w + h - 1) == b - (w - 2 + h - 3);
  }
  return false;
}

int main() {
  while (cin >> w >> h >> n) {
    int a = 0, b = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int p;
      cin >> p;
      if (p)
        b++;
      else
        a++;
      ans += adj(a, b);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

int w, h, n;

bool adj(int a, int b) {
  if (0 <= a && a <= w - 2 && 0 <= b && b <= w - 2) {
    return a == b;
  }
  if (w - 2 <= a && a <= w - 2 + h - 3 && w <= b && b <= w + h - 3) {
    return a - (w - 2) == b - w;
  }
  if (w - 2 + h - 3 <= a && w + h - 1 <= b) {
    return a - (w - 2 + h - 3) == b - (w + h - 1);
  }
  assert(false);
}

int main() {
  while (cin >> w >> h >> n) {
    int a = 0, b = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int p;
      cin >> p;
      if (p)
        b++;
      else
        a++;
      ans += adj(a, b);
    }
    cout << ans << endl;
  }
}
Problem p01061 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (ll i = 0; i < (n); ++i)
using ll = long long;
using vl = vector<ll>;

struct union_find {
  vl v;
  union_find(ll n) : v(n, -1) {}
  ll find(ll x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (-v[x] < -v[y])
      swap(x, y);
    v[x] += v[y];
    v[y] = x;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
  ll size(ll x) { return -v[find(x)]; }
};

int main() {
#ifdef _WIN32
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cout << fixed << setprecision(8);

  ll n, m;
  cin >> n >> m;
  vl a(m), b(m);
  REP(i, m) cin >> a[i] >> b[i];

  union_find uf(n);
  REP(i, m) uf.unite(a[i] - 1, b[i] - 1);

  ll cntm = 0;
  ll cnts = 0;
  REP(i, n) {
    if (uf.size(i) == 1) {
      cntm++;
    } else {
      if (uf.find(i) == i) {
        cnts++;
      }
    }
  }

  cout << abs(cntm - cnts) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (ll i = 0; i < (n); ++i)
using ll = long long;
using vl = vector<ll>;

struct union_find {
  vl v;
  union_find(ll n) : v(n, -1) {}
  ll find(ll x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (-v[x] < -v[y])
      swap(x, y);
    v[x] += v[y];
    v[y] = x;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
  ll size(ll x) { return -v[find(x)]; }
};

int main() {
#ifdef _WIN32
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cout << fixed << setprecision(8);

  ll n, m;
  cin >> n >> m;
  vl a(n), b(n);
  REP(i, m) cin >> a[i] >> b[i];

  union_find uf(n);
  REP(i, m) uf.unite(a[i] - 1, b[i] - 1);

  ll cntm = 0;
  ll cnts = 0;
  REP(i, n) {
    if (uf.size(i) == 1) {
      cntm++;
    } else {
      if (uf.find(i) == i) {
        cnts++;
      }
    }
  }

  cout << abs(cntm - cnts) << endl;
}
Problem p01061 (C++) — buggy submission, verdict: Runtime Error

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

struct UnionFind {
  vector<int> data;

  UnionFind(int size) { data.assign(size, -1); }
  int Find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = Find(data[k]));
  }
  void unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return;
    data[x] += data[y];
    data[y] = x;
  }
  int Size(int k) { return (-data[Find(k)]); }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    tree.unite(A, B);
  }
  int mura = 0;
  int used[1000] = {};
  for (int i = 0; i < N; i++) {
    int K = tree.Find(i);
    if (used[K]++)
      continue;
    if (tree.Size(K) == 1)
      ++mura;
    else
      --mura;
  }
  cout << abs(mura) << endl;
}

Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> data;

  UnionFind(int size) { data.assign(size, -1); }
  int Find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = Find(data[k]));
  }
  void unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return;
    data[x] += data[y];
    data[y] = x;
  }
  int Size(int k) { return (-data[Find(k)]); }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    tree.unite(A, B);
  }
  int mura = 0;
  int used[100] = {};
  for (int i = 0; i < N; i++) {
    int K = tree.Find(i);
    if (used[K]++)
      continue;
    if (tree.Size(K) == 1)
      ++mura;
    else
      --mura;
  }
  cout << abs(mura) << endl;
}
Problem p01062 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
vvi in;
vector<bool> d;
int n;
int coa, cob;
bool f(int a, int b) {
  if (coa == cob && coa == n / 2)
    return true;
  rep(i, in[a].size()) {
    if (d[in[a][i]])
      continue;
    d[in[a][i]] = true;
    coa++;
    rep(j, in[b].size()) {
      if (d[in[b][j]])
        continue;
      d[in[b][j]] = true;
      cob++;
      if (f(in[a][i], in[b][j]))
        return true;
      d[in[b][j]] = false;
      cob--;
    }
    d[in[a][i]] = false;
    coa--;
  }
  return false;
}
int main() {
  int a, b;
  coa = cob = 1;
  cin >> n >> a >> b;
  d = vector<bool>(n, false);
  a--;
  b--;
  d[a] = d[b] = true;
  vvi tmp(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tmp[a].pb(b);
    tmp[b].pb(a);
  }
  if (n % 2) {
    cout << "No" << endl;
    return 0;
  }
  in = tmp;
  int q = 0;
  rep(i, n) if (in[i].size() > 3) q++;
  if (q) {
    cout << "No" << endl;
    return 0;
  }
  q = 0;
  rep(i, n) if (in[i].size() == 3) q++;
  if (q > 2) {
    cout << "No" << endl;
    return 0;
  }
  cout << (f(a, b) ? "Yes" : "No") << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
vvi in;
vector<bool> d;
int n;
int coa, cob;
bool f(int a, int b) {
  if (coa == cob && coa == n / 2)
    return true;
  rep(i, in[a].size()) {
    if (d[in[a][i]])
      continue;
    d[in[a][i]] = true;
    coa++;
    rep(j, in[b].size()) {
      if (d[in[b][j]])
        continue;
      d[in[b][j]] = true;
      cob++;
      if (f(in[a][i], in[b][j]))
        return true;
      d[in[b][j]] = false;
      cob--;
    }
    d[in[a][i]] = false;
    coa--;
  }
  return false;
}
int main() {
  int a, b;
  coa = cob = 1;
  cin >> n >> a >> b;
  d = vector<bool>(n, false);
  a--;
  b--;
  d[a] = d[b] = true;
  vvi tmp(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tmp[a].pb(b);
    tmp[b].pb(a);
  }
  if (n % 2) {
    cout << "No" << endl;
    return 0;
  }
  in = tmp;
  cout << (f(a, b) ? "Yes" : "No") << endl;
}
Problem p01062 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

vector<int> G[100003];
int N;

bool dfs(int pa, int pb, int number, bool used[100003]) {
  //    cout<<pa<<" "<<pb<<" "<<number<<endl;
  bool res = false;

  if (number == N)
    return true;
  vector<int> next1, next2;

  rep(i, G[pa].size()) {
    int to = G[pa][i];
    if (used[to])
      continue;
    next1.pb(to);
  }

  rep(i, G[pb].size()) {
    int to = G[pb][i];
    if (used[to])
      continue;
    next2.pb(to);
  }

  rep(i, next1.size()) {
    rep(j, next2.size()) {
      if (next1[i] == next2[j])
        continue;
      used[next1[i]] = true;
      used[next2[j]] = true;
      res |= dfs(next1[i], next2[j], number + 2, used);
      used[next1[i]] = false;
      used[next2[j]] = false;
    }
  }
  return res;
}

int main() {
  int u, v;
  cin >> N >> u >> v;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  rep(i, N + 1) {
    if (G[i].size() >= 4) {
      cout << "No" << endl;
      return 0;
    }
  }

  bool used[100003] = {};
  used[u] = used[v] = true;

  bool res = dfs(u, v, 2, used);

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

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

vector<int> G[100003];
int N;

bool dfs(int pa, int pb, int number, bool used[100003]) {
  //    cout<<pa<<" "<<pb<<" "<<number<<endl;
  bool res = false;

  if (number == N)
    return true;
  vector<int> next1, next2;

  rep(i, G[pa].size()) {
    int to = G[pa][i];
    if (used[to])
      continue;
    next1.pb(to);
  }

  rep(i, G[pb].size()) {
    int to = G[pb][i];
    if (used[to])
      continue;
    next2.pb(to);
  }

  rep(i, next1.size()) {
    rep(j, next2.size()) {
      if (next1[i] == next2[j])
        continue;
      used[next1[i]] = true;
      used[next2[j]] = true;
      res |= dfs(next1[i], next2[j], number + 2, used);
      used[next1[i]] = false;
      used[next2[j]] = false;
    }
  }
  return res;
}

int main() {
  int u, v;
  cin >> N >> u >> v;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  bool used[100003] = {};
  used[u] = used[v] = true;

  bool res = dfs(u, v, 2, used);

  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p01064 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n, a, d, m, k;
  cin >> n;
  cin >> a >> d;
  cin >> m;
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    y[i]--;
    z[i]--;
  }
  cin >> k;
  k--;
  vector<int> com;
  for (int i = m - 1; i >= 0; i--) {
    if (x[i] == 0 && (y[i] <= k && k <= z[i])) {
      k = y[i] + z[i] - k;
      // cout << "1 " << i << endl;;
    } else {
      if (y[i] <= k && k <= z[i]) {
        com.push_back(x[i]);
      }
    }
  }
  // cout << k << endl;
  int num = a + d * k;
  // cout << com.size() << endl;
  for (int i = com.size() - 1; i >= 0; i--) {
    if (com[i] == 1) {
      num++;
    } else if (com[i] == 2) {
      num /= 2;
    }
  }

  cout << num << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, a, d;
  cin >> n >> a >> d;
  vector<int> an(n);
  for (int i = 0; i < n; i++)
    an[i] = a + d * i;

  int m;
  cin >> m;
  int x, y, z;
  for (int i = 0; i < m; i++) {
    //  cout << an[0]<<" ";
    cin >> x >> y >> z;
    // cout << x << " " << y << " " << z << endl;
    if (x == 0) {
      // cout << "1 \\n";
      for (int j = y - 1; j < z - 1; j++)
        swap(an[j], an[z - 1 - j]);
      // reverse(an[y-1],an[y-1]+z-y+1);
    } else if (x == 1) {
      // cout << "2 \\n";
      for (int j = y - 1; j < z - 1; j++)
        an[j]++;
    } else if (x == 2) {
      // cout << "3 \\n";
      for (int j = y - 1; j < z; j++)
        an[j] /= 2;
    }
    //   cout << an[0] <<" "<<an[n-1]<<endl;
  }
  int k;
  cin >> k;
  cout << an[k - 1] << endl;

  return 0;
}