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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<long long>> vvll;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define INF 5000000000000000LL
#define MOD 1000000007
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define p_q priority_queue

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 200005
ll n;
ll x[MN];
ll ps[MN];
ll c; // x in problem!

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

  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  cin >> n >> c;
  FOR(i, 1, n) {
    cin >> x[n + 1 - i];
    // ps[i] = ps[i-1]+x[i];
  }
  FOR(i, 1, n) {
    ps[i] = ps[i - 1] + x[i];
    // cout << ps[i] << " ";
  }
  // cout << "\n";

  ll ans = INF;

  FOR(i, 1, n) {
    ll cm = 5;

    ll ca = 0;
    for (ll j = i; j <= n + i; j += i) {
      ll sm = ps[min(j, n)] - ps[j - i];

      ca += sm * cm;
      // cm+=2;
      if (j != i)
        cm += 2;
      if (ca > INF)
        ca = INF;
    }

    ca += c * (ll)(i + n);
    if (ca > INF)
      ca = INF;

    // cout << i << " " << ca << "\n";

    ans = min(ans, ca);
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<long long>> vvll;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define INF 5000000000000000LL
#define MOD 1000000007
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define p_q priority_queue

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 200005
ll n;
ll x[MN];
ll ps[MN];
ll c; // x in problem!

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> c;
  FOR(i, 1, n) {
    cin >> x[n + 1 - i];
    // ps[i] = ps[i-1]+x[i];
  }
  FOR(i, 1, n) {
    ps[i] = ps[i - 1] + x[i];
    // cout << ps[i] << " ";
  }
  // cout << "\n";

  ll ans = INF;

  FOR(i, 1, n) {
    ll cm = 5;

    ll ca = 0;
    for (ll j = i; j <= n + i; j += i) {
      ll sm = ps[min(j, n)] - ps[j - i];

      ca += sm * cm;
      // cm+=2;
      if (j != i)
        cm += 2;
      if (ca > INF)
        ca = INF;
    }

    ca += c * (ll)(i + n);
    if (ca > INF)
      ca = INF;

    // cout << i << " " << ca << "\n";

    ans = min(ans, ca);
  }

  cout << ans << "\n";

  return 0;
}
Problem p03255 (C++)

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

using namespace std;
typedef long long ll;

const int N = 2018;
const ll oo = 9e18;

ll x[N];
ll X;
int n;

ll mul(ll a, ll b) { return a > oo / b ? oo : a * b; }

ll add(ll a, ll b) { return a > oo - b ? oo : a + b; }
int main() {

  scanf("%d %lld", &n, &X);

  for (int i = 1; i <= n; i++)
    scanf("%lld", x + i);
  reverse(x + 1, 1 + x + n);
  ll psum[N];
  partial_sum(x + 1, x + 1 + n, psum + 1);

  ll ans = 1e18;
  for (int k = 1; k <= n; k++) {
    int j = 1;
    ll sum = 0;
    int i;
    for (i = k; i <= n; i += k) {
      if (j == 1)
        sum = add(sum, (psum[i] - psum[i - k]) * 5);
      else
        sum = add(sum, mul(psum[i] - psum[i - k], (2 * j + 1)));
      j++;
    }
    if (i - k < n) {
      sum = add(sum, mul(psum[n] - psum[i - k], (2 * j + 1)));
    }
    ans = min(ans, sum + k * X);
  }
  printf("%lld\n", ans + X * n);
}


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

using namespace std;
typedef long long ll;

const int N = 200018;
const ll oo = 9e18;

ll x[N];
ll X;
int n;

ll mul(ll a, ll b) { return a > oo / b ? oo : a * b; }

ll add(ll a, ll b) { return a > oo - b ? oo : a + b; }
int main() {

  scanf("%d %lld", &n, &X);

  for (int i = 1; i <= n; i++)
    scanf("%lld", x + i);
  reverse(x + 1, 1 + x + n);
  ll psum[N];
  partial_sum(x + 1, x + 1 + n, psum + 1);

  ll ans = 1e18;
  for (int k = 1; k <= n; k++) {
    int j = 1;
    ll sum = 0;
    int i;
    for (i = k; i <= n; i += k) {
      if (j == 1)
        sum = add(sum, (psum[i] - psum[i - k]) * 5);
      else
        sum = add(sum, mul(psum[i] - psum[i - k], (2 * j + 1)));
      j++;
    }
    if (i - k < n) {
      sum = add(sum, mul(psum[n] - psum[i - k], (2 * j + 1)));
    }
    ans = min(ans, sum + k * X);
  }
  printf("%lld\n", ans + X * n);
}
Problem p03255 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;

ull sum[200005] = {0};
ull n, x;

ull solve();

int main() {
  cin >> n >> x;
  for (ull i = 1; i <= n; ++i) {
    ull a;
    cin >> a;
    sum[i] = sum[i - 1] + a;
  }
  cout << solve() << endl;
  return 0;
}

ull solve() {
  ull ans = 0;
  for (ull i = 1; i <= n; ++i) {
    ull now = n, j = 1, nans = 0;
    while (1) {
      if (j == 1)
        nans += sum[now] * 5;
      else if (j >= 2)
        nans += sum[now] * (2 * j + 1);
      now -= n * (ull)((long double)i / n);
      if (now == 0 || now > n)
        break;
      if (j == 1)
        nans -= sum[now] * 5;
      else if (j >= 2)
        nans -= sum[now] * (2 * j + 1);
      ++j;
    }
    nans += x * (n + i);
    if (i == 1)
      ans = nans;
    ans = min(nans, ans);
  }
  return ans;
}

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

ull sum[200005] = {0};
ull n, x;

ull solve();

int main() {
  cin >> n >> x;
  for (ull i = 1; i <= n; ++i) {
    ull a;
    cin >> a;
    sum[i] = sum[i - 1] + a;
  }
  cout << solve() << endl;
  return 0;
}

ull solve() {
  ull ans = 0;
  for (ull i = 1; i <= n; ++i) {
    ull now = n, j = 1, nans = 0;
    while (1) {
      if (j == 1)
        nans += sum[now] * 5;
      else if (j >= 2)
        nans += sum[now] * (2 * j + 1);
      now -= i;
      if (now == 0 || now > n)
        break;
      if (j == 1)
        nans -= sum[now] * 5;
      else if (j >= 2)
        nans -= sum[now] * (2 * j + 1);
      ++j;
    }
    nans += x * (n + i);
    if (i == 1)
      ans = nans;
    ans = min(nans, ans);
  }
  return ans;
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pss pair<short, short>
#define piii pair<pii, int>
#define vii vector<pair<int, int>>
#define st first
#define nd second
const int mod = 1000000007;
const int inf = 1000000009;
const long long int INF = 1000000000000000009;
using namespace std;
int T[100005];
ll S[100005], D[100005];
int main() {
  int n, x;
  scanf("%d %d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &T[i]);
    S[i] = S[i - 1] + T[i];
  }
  ll wynik = INF;
  D[1] = 5;
  D[2] = 5;
  for (int i = 3; i <= n; i++)
    D[i] = D[i - 1] + 2;
  for (int i = 1; i <= n; i++) {
    ll aktual = (ll)x * n;
    aktual += (ll)i * x;
    int ile = n;
    int wsk = 1;
    while (ile > 0) {
      if (aktual > wynik)
        break;
      if (S[ile] - S[max(0, ile - i)] > INF / (ll)D[wsk])
        break;
      aktual += (ll)D[wsk] * (S[ile] - S[max(0, ile - i)]);
      wsk++;
      ile -= i;
    }
    wynik = min(wynik, aktual);
  }
  printf("%lld", wynik);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pss pair<short, short>
#define piii pair<pii, int>
#define vii vector<pair<int, int>>
#define st first
#define nd second
const int mod = 1000000007;
const int inf = 1000000009;
const long long int INF = 1000000000000000009;
using namespace std;
int T[200005];
ll S[200005], D[200005];
int main() {
  int n, x;
  scanf("%d %d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &T[i]);
    S[i] = S[i - 1] + T[i];
  }
  ll wynik = INF;
  D[1] = 5;
  D[2] = 5;
  for (int i = 3; i <= n; i++)
    D[i] = D[i - 1] + 2;
  for (int i = 1; i <= n; i++) {
    ll aktual = (ll)x * n;
    aktual += (ll)i * x;
    int ile = n;
    int wsk = 1;
    while (ile > 0) {
      if (aktual > wynik)
        break;
      if (S[ile] - S[max(0, ile - i)] > INF / (ll)D[wsk])
        break;
      aktual += (ll)D[wsk] * (S[ile] - S[max(0, ile - i)]);
      wsk++;
      ile -= i;
    }
    wynik = min(wynik, aktual);
  }
  printf("%lld", wynik);
  return 0;
}
Problem p03255 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
*/
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);

using namespace std;

const int maxn = 2e5 + 123, inf = 1e9, mod = 1e9 + 7;
ll n, x, a[maxn], col[maxn];

ll get(ll k) {
  ll res = 0;
  for (int i = n, j = 2, q = k; i >= 0; i--) {
    if (q == 0)
      q = k, j++;
    col[i] = j;
    q--;
  }
  for (int i = 1; i <= n; i++)
    if (col[i] == 2)
      res += a[i] * 2;
  for (int i = n; i >= 1; i--)
    res += a[i] * (col[i] * col[i] - (col[i] - 1) * (col[i] - 1));
  return res + x * (k + n);
}

int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll res = 1e18;

  for (int i = 1; i <= n; i++)
    res = min(res, get(i));
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
*/
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);

using namespace std;

const int maxn = 2e5 + 123, inf = 1e9, mod = 1e9 + 7;
ll n, x, a[maxn], col[maxn];

ll get(ll k) {
  ll res = 0;
  for (int i = n, j = 2, q = k; i >= 0; i--) {
    if (q == 0)
      q = k, j++;
    col[i] = j;
    q--;
  }
  for (int i = 1; i <= n; i++)
    if (col[i] == 2)
      res += a[i] * 2;
  for (int i = n; i >= 1; i--)
    res += a[i] * (col[i] * col[i] - (col[i] - 1) * (col[i] - 1));
  return res + x * (k + n);
}

int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll res = 1e18;
  int l = 1, r = n;
  for (int i = 0; i < 50; i++) {
    l = max(1, l);
    r = min(n, 0ll + r);
    int m1 = l + (r - l) / 3;
    int m2 = r - (r - l) / 3;
    if (get(m1) > get(m2))
      l = m1 - 1;
    else
      r = m2 + 1;
  }
  for (int i = max(1, l - 50); i <= min(n, r + 50ll); i++)
    res = min(res, get(i));
  cout << res;
  return 0;
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
const int maxn = 2e5 + 5;
const int inf = 1e9 + 1;
const ll llinf = 1e18 + 5;
int n, a[maxn];
int x;
ll s[maxn];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(taskname ".inp", "r")) {
    freopen(taskname ".inp", "r", stdin);
    freopen(taskname ".out", "w", stdout);
  }
  cin >> n >> x;
  ll res = LLONG_MAX;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    s[i] = a[i] + s[i - 1];
  }
  vector<int> delta;
  delta.pb(5);
  for (int i = 2; i <= n; ++i) {
    delta.pb(i * 2 + 1);
  }
  for (int i = 1; i <= n; ++i) {
    int now = n;
    ll tmp = 0;
    for (int c : delta) {
      function<ll(ll, ll)> Mul = [&](ll x, ll y) {
        if (llinf / y < x)
          return llinf;
        return x * y;
      };
      if (now > i) {
        tmp += Mul((s[now] - s[now - i]), c);
      } else
        tmp += Mul((s[now] - s[now - i]), c);
      tmp = min(tmp, llinf);
      now -= i;
      if (now <= 0)
        break;
    }
    res = min(res, tmp + (ll)(n + i) * x);
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
const int maxn = 2e5 + 5;
const int inf = 1e9 + 1;
const ll llinf = 1e18 + 5;
int n, a[maxn];
int x;
ll s[maxn];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(taskname ".inp", "r")) {
    freopen(taskname ".inp", "r", stdin);
    freopen(taskname ".out", "w", stdout);
  }
  cin >> n >> x;
  ll res = LLONG_MAX;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    s[i] = a[i] + s[i - 1];
  }
  vector<int> delta;
  delta.pb(5);
  for (int i = 2; i <= n; ++i) {
    delta.pb(i * 2 + 1);
  }
  for (int i = 1; i <= n; ++i) {
    int now = n;
    ll tmp = 0;
    for (int c : delta) {
      function<ll(ll, ll)> Mul = [&](ll x, ll y) {
        if (llinf / y < x)
          return llinf;
        return x * y;
      };
      if (now > i) {
        tmp += Mul((s[now] - s[now - i]), c);
      } else
        tmp += Mul((s[now]), c);
      tmp = min(tmp, llinf);
      now -= i;
      if (now <= 0)
        break;
    }
    res = min(res, tmp + (ll)(n + i) * x);
  }
  cout << res;
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define EPS ((long double)1e-12)
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz(x) ((long long)(x).size())
#define endl '\n'
#define DUMP_NAME(name, a) cout << "#dump " << (name) << " = " << (a) << "  "
#define dump(a) DUMP_NAME(#a, (a));
#define FOREACH(it, x)                                                         \
  for (__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define ROF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
const ld PI = 3.141592653589793238L;
const ll N = 500005;
using namespace std;

ull a[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  //<<>>

  ll n, x;
  cin >> n >> x;
  FOR(i, 1, n) cin >> a[i];
  reverse(a + 1, a + n + 1);
  ROF(i, n - 1, 1) a[i] += a[i + 1];

  ull ans = ULLONG_MAX;
  FOR(k, 1, n) {
    ull ans1 = k * x;
    bool take = 1;

    FOR(i, 1, n) {
      ull ans2 = (a[i] - a[i + k]) * max(5ll, 2 * ((i - 1) / k + 1) + 1);
      if (ans - ans2 <= ans1)
        take = 0;
      ans1 += ans2;

      i += k - 1;
    }

    if (take == 1)
      ans = ans1;
  }

  ans += (ull)n * x;

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define EPS ((long double)1e-12)
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz(x) ((long long)(x).size())
#define endl '\n'
#define DUMP_NAME(name, a) cout << "#dump " << (name) << " = " << (a) << "  "
#define dump(a) DUMP_NAME(#a, (a));
#define FOREACH(it, x)                                                         \
  for (__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define ROF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
const ld PI = 3.141592653589793238L;
const ll N = 500005;
using namespace std;

ull a[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
#endif
  //<<>>

  ll n, x;
  cin >> n >> x;
  FOR(i, 1, n) cin >> a[i];
  reverse(a + 1, a + n + 1);
  ROF(i, n - 1, 1) a[i] += a[i + 1];

  ull ans = ULLONG_MAX;
  FOR(k, 1, n) {
    ull ans1 = k * x;
    bool take = 1;

    FOR(i, 1, n) {
      ull ans2 = (a[i] - a[i + k]) * max(5ll, 2 * ((i - 1) / k + 1) + 1);
      if (ans - ans2 <= ans1)
        take = 0;
      ans1 += ans2;

      i += k - 1;
    }

    if (take == 1)
      ans = ans1;
  }

  ans += (ull)n * x;

  cout << ans << endl;

  return 0;
}
Problem p03256 (C++)

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

string S;
bool ok[200010] = {0};
vector<vector<int>> v(200010);
int N, M, visited[200010] = {0};

bool dfs(int n) {
  visited[n] = 1;
  bool j = true;
  if (!ok[n])
    j = false;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i]] == 0 && ok[v[n][i]])
      j = (j | dfs(v[n][i]));
  }
  return j;
}

int main() {
  cin >> N >> M >> S;
  int a, b;
  int A[100010] = {0}, B[100010] = {0};
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> Q;
  int count = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (S[v[i][j] - 1] == 'A')
        A[i]++;
      if (S[v[i][j] - 1] == 'B')
        B[i]++;
    }
    if (A[i] == 0 || B[i] == 0) {
      Q.push(i);
      visited[i] = 1;
      count++;
    }
    if (A && B)
      ok[i] = true;
  }
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    for (int i = 0; i < v[t].size(); i++) {
      if (S[t - 1] == 'A')
        A[v[t][i]]--;
      if (S[t - 1] == 'B')
        B[v[t][i]]--;
      if ((A[v[t][i]] == 0 || B[v[t][i]] == 0) && visited[v[t][i]] == 0) {
        Q.push(v[t][i]);
        count++;
        visited[v[t][i]] = 1;
      }
    }
  }
  /*	bool judge = false;
          for(int i=1;i<=N;i++){
                  if(visited[i]==0 && dfs(i)) judge = true;
          }
  */ cout << (count < N ? "Yes" : "No") << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

string S;
bool ok[200010] = {0};
vector<vector<int>> v(200010);
int N, M, visited[200010] = {0};

bool dfs(int n) {
  visited[n] = 1;
  bool j = true;
  if (!ok[n])
    j = false;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i]] == 0 && ok[v[n][i]])
      j = (j | dfs(v[n][i]));
  }
  return j;
}

int main() {
  cin >> N >> M >> S;
  int a, b;
  int A[200010] = {0}, B[200010] = {0};
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> Q;
  int count = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (S[v[i][j] - 1] == 'A')
        A[i]++;
      if (S[v[i][j] - 1] == 'B')
        B[i]++;
    }
    if (A[i] == 0 || B[i] == 0) {
      Q.push(i);
      visited[i] = 1;
      count++;
    }
    if (A && B)
      ok[i] = true;
  }
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    for (int i = 0; i < v[t].size(); i++) {
      if (S[t - 1] == 'A')
        A[v[t][i]]--;
      if (S[t - 1] == 'B')
        B[v[t][i]]--;
      if ((A[v[t][i]] == 0 || B[v[t][i]] == 0) && visited[v[t][i]] == 0) {
        Q.push(v[t][i]);
        count++;
        visited[v[t][i]] = 1;
      }
    }
  }
  /*	bool judge = false;
          for(int i=1;i<=N;i++){
                  if(visited[i]==0 && dfs(i)) judge = true;
          }
  */ cout << (count < N ? "Yes" : "No") << endl;
}
Problem p03256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
const int maxn = 2e5 + 10;
char label[maxn];

struct edge {
  int v, next;
  edge(int v = 0, int next = 0) : v(v), next(next) {}
} e[maxn * 2];
int hd[maxn], ek = 1;
inline void adde(int u, int v) {
  e[ek] = edge(v, hd[u]);
  hd[u] = ek++;
}
int clock;
bool vis[maxn];
int dep[maxn];
bool dfs(int len, char pas, int u) {
  vis[u] = 1;
  ++clock;
  if (clock >= 5e7)
    return 0;
  for (int i = hd[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (label[v] == pas)
      continue;
    if (len >= 3e5)
      return 1;
    if (dfs(len + 1, label[u], v))
      return 1;
  }
  return 0;
}
int main() {
  memset(dep, 0x3f, sizeof dep);
  dep[1] = 1;
  int n, m;
  scanf("%d%d%s", &n, &m, label + 1);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adde(u, v);
    adde(v, u);
  }
  for (int i = 1; i <= n; i++)
    if (dfs(1, '$', i))
      return !printf("Yes");
  return !printf("No");
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
const int maxn = 2e5 + 10;
char label[maxn];

struct edge {
  int v, next;
  edge(int v = 0, int next = 0) : v(v), next(next) {}
} e[maxn * 2];
int hd[maxn], ek = 1;
inline void adde(int u, int v) {
  e[ek] = edge(v, hd[u]);
  hd[u] = ek++;
}
int clock;
bool vis[maxn];
int dep[maxn];
bool dfs(int len, char pas, int u) {
  vis[u] = 1;
  ++clock;
  if (clock >= 3e7)
    return 0;
  for (int i = hd[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (label[v] == pas)
      continue;
    if (len >= 3e5)
      return 1;
    if (dfs(len + 1, label[u], v))
      return 1;
  }
  return 0;
}
int main() {
  memset(dep, 0x3f, sizeof dep);
  dep[1] = 1;
  int n, m;
  scanf("%d%d%s", &n, &m, label + 1);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adde(u, v);
    adde(v, u);
  }
  for (int i = 1; i <= n; i++)
    if (dfs(1, '$', i))
      return !printf("Yes");
  return !printf("No");
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL rd() {
  char ch = getchar();
  LL i = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    i = (i << 1) + (i << 3) + ch - '0';
    ch = getchar();
  }
  return i * f;
}

const int N = 1e5 + 50, mod = 1e9 + 7;
int n, m, vis[N];
int cnt[N][2], type[N];
vector<int> edge[N];
char ch[N];

int main() {
  n = rd(), m = rd();
  scanf("%s", ch + 1);
  for (int i = 1; i <= n; i++)
    type[i] = (ch[i] == 'A') ? 0 : 1;
  for (int i = 1; i <= m; i++) {
    int x = rd(), y = rd();
    if (x != y) {
      edge[x].push_back(y);
      edge[y].push_back(x);
    }
    ++cnt[x][type[y]];
    ++cnt[y][type[x]];
  }
  queue<int> q;
  int cc = 0;
  for (int i = 1; i <= n; ++i)
    if (!cnt[i][0] || !cnt[i][1])
      q.push(i), vis[i] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    ++cc;
    for (auto e : edge[u])
      if (!vis[e]) {
        cnt[e][type[u]]--;
        if (!cnt[e][0] || !cnt[e][1])
          q.push(e), vis[e] = 1;
      }
  }
  if (cc == n)
    puts("No");
  else
    puts("Yes");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL rd() {
  char ch = getchar();
  LL i = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    i = (i << 1) + (i << 3) + ch - '0';
    ch = getchar();
  }
  return i * f;
}

const int N = 2e5 + 50, mod = 1e9 + 7;
int n, m, vis[N];
int cnt[N][2], type[N];
vector<int> edge[N];
char ch[N];

int main() {
  n = rd(), m = rd();
  scanf("%s", ch + 1);
  for (int i = 1; i <= n; i++)
    type[i] = (ch[i] == 'A') ? 0 : 1;
  for (int i = 1; i <= m; i++) {
    int x = rd(), y = rd();
    if (x != y) {
      edge[x].push_back(y);
      edge[y].push_back(x);
    }
    ++cnt[x][type[y]];
    ++cnt[y][type[x]];
  }
  queue<int> q;
  int cc = 0;
  for (int i = 1; i <= n; ++i)
    if (!cnt[i][0] || !cnt[i][1])
      q.push(i), vis[i] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    ++cc;
    for (auto e : edge[u])
      if (!vis[e]) {
        cnt[e][type[u]]--;
        if (!cnt[e][0] || !cnt[e][1])
          q.push(e), vis[e] = 1;
      }
  }
  if (cc == n)
    puts("No");
  else
    puts("Yes");
}
Problem p03256 (C++)

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

string s;
multiset<int> edge[200005];
int memo[200005][2];

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m >> s;
  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    --a;
    --b;
    edge[a].insert(b);
    ++memo[a][s[b] - 'A'];
    if (a != b) {
      edge[b].insert(a);
      ++memo[b][s[a] - 'A'];
    }
  }
  multiset<int> que;
  for (int i = 0; i < n; ++i) {
    if (memo[i][0] == 0 || memo[i][1] == 0) {
      que.insert(i);
      while (que.size() > 0) {
        int now = *que.begin();
        for (auto itr = edge[now].begin(); itr != edge[now].end();) {
          int v = *itr;
          itr = edge[now].erase(itr);
          --memo[now][s[v] - 'A'];
          edge[v].erase(now);
          --memo[v][s[now] - 'A'];
          if (memo[v][0] == 0 || memo[v][1] == 0) {
            que.insert(v);
          }
        }
        que.erase(now);
      }
    }
  }
  int f = 0;
  for (int i = 0; i < n; ++i) {
    if (edge[i].size() > 0) {
      f = 1;
    }
  }
  if (f) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
  return 0;
}


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

string s;
multiset<int> edge[200005];
int memo[200005][2];

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m >> s;
  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    --a;
    --b;
    if (edge[a].count(b) > 0) {
      continue;
    }
    edge[a].insert(b);
    ++memo[a][s[b] - 'A'];
    if (a != b) {
      edge[b].insert(a);
      ++memo[b][s[a] - 'A'];
    }
  }
  multiset<int> que;
  for (int i = 0; i < n; ++i) {
    if (memo[i][0] == 0 || memo[i][1] == 0) {
      que.insert(i);
      while (que.size() > 0) {
        int now = *que.begin();
        for (auto itr = edge[now].begin(); itr != edge[now].end();) {
          int v = *itr;
          itr = edge[now].erase(itr);
          --memo[now][s[v] - 'A'];
          edge[v].erase(now);
          --memo[v][s[now] - 'A'];
          if (memo[v][0] == 0 || memo[v][1] == 0) {
            que.insert(v);
          }
        }
        que.erase(now);
      }
    }
  }
  int f = 0;
  for (int i = 0; i < n; ++i) {
    if (edge[i].size() > 0) {
      f = 1;
    }
  }
  if (f) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int> v[404040];
int used[404040], ans;

void dfs(int x) {
  used[x] = -1;
  for (auto &u : v[x]) {
    if (used[u] == -1) {
      ans = true;
    } else
      dfs(u);
  }
  used[x] = 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    if (s[x] == s[y]) {
      v[x].push_back(y + n);
      v[y].push_back(x + n);
    } else {
      v[x + n].push_back(y);
      v[y + n].push_back(x);
    }
  }
  rep(i, 2 * n) if (!used[i]) dfs(i);
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int> v[404040];
int used[404040], ans;

void dfs(int x) {
  used[x] = -1;
  for (auto &u : v[x]) {
    if (used[u] == -1) {
      ans = true;
    } else if (used[u] == 1)
      continue;
    else
      dfs(u);
  }
  used[x] = 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    if (s[x] == s[y]) {
      v[x].push_back(y + n);
      v[y].push_back(x + n);
    } else {
      v[x + n].push_back(y);
      v[y + n].push_back(x);
    }
  }
  rep(i, 2 * n) if (!used[i]) dfs(i);
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const string s = "AABB";

bool dfs(const vector<vector<int>> &G, const string &S, vector<int> &V,
         vector<bool> &visited, int v, int t, int u) {
  bool ret = false;
  V[v] |= (1 << t);
  visited[v] = true;
  t = (t + 1) % 4;
  for (size_t i = 0; i < G[v].size(); ++i) {
    int v_ = G[v][i];
    if (S[v_] != s[t])
      continue;
    if (v_ == u and not t)
      return true;
    // if(not t and V[v_]%2) return true;
    if ((V[v_] >> t) & 1)
      return true;
    // if((V[v_]>>t)&1) continue;
    ret |= dfs(G, S, V, visited, v_, t, u);
  }
  V[v] = 0;
  return ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  assert(N <= 1000);
  bool f = false;
  vector<bool> visited(N, false);
  vector<int> V(N, 0);
  for (int i = 0; i < N; ++i) {
    if (S[i] != 'A')
      continue;
    if (visited[i])
      continue;
    f |= dfs(G, S, V, visited, i, 0, i);
    if (f) {
      cerr << i << endl;
      break;
    }
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Fixed submission:
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const string s = "AABB";

bool dfs(const vector<vector<int>> &G, const string &S, vector<int> &V,
         vector<bool> &visited, int v, int t, int u) {
  bool ret = false;
  V[v] |= (1 << t);
  visited[v] = true;
  t = (t + 1) % 4;
  for (size_t i = 0; i < G[v].size(); ++i) {
    int v_ = G[v][i];
    if (S[v_] != s[t])
      continue;
    if (v_ == u and not t)
      return true;
    // if(not t and V[v_]%2) return true;
    if ((V[v_] >> t) & 1)
      return true;
    // if((V[v_]>>t)&1) continue;
    ret |= dfs(G, S, V, visited, v_, t, u);
  }
  V[v] = 0;
  return ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  // assert(N <= 1000);
  bool f = false;
  vector<bool> visited(N, false);
  vector<int> V(N, 0);
  for (int i = 0; i < N; ++i) {
    if (S[i] != 'A')
      continue;
    if (visited[i])
      continue;
    f |= dfs(G, S, V, visited, i, 0, i);
    if (f) {
      cerr << i << endl;
      break;
    }
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int MX = 200005;
int n, m, c[MX][2], a, b, qu[MX], t, k = 1, out[MX];
char s[MX];
vector<int> G[MX];
int main() {
  scanf("%d%d\n%s", &n, &m, s);
  rep(i, m) {
    scanf("%d%d", &a, &b);
    G[--a].push_back(--b);
    G[b].push_back(a);
    c[a][s[b] - 'A']++;
    c[b][s[a] - 'A']++;
  }
  rep(i, n) if (!out[i] && !(c[i][0] && c[i][1])) {
    out[qu[b++] = i] = 1;
    while (a < b) {
      t = qu[a++];
      for (int u : G[t])
        if (!out[u] && !--c[u][s[t] - 'A']) {
          out[qu[b++] = u] = 1;
        }
    }
  }
  rep(i, n) k &= out[i];
  puts(k ? "No" : "Yes");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int MX = 200005;
int n, m, c[MX][2], a, b, qu[MX], t, k = 1, out[MX];
char s[MX];
vector<int> G[MX];
int main() {
  scanf("%d%d\n%s", &n, &m, s);
  rep(i, m) {
    scanf("%d%d", &a, &b);
    G[--a].push_back(--b);
    G[b].push_back(a);
    c[a][s[b] - 'A']++;
    c[b][s[a] - 'A']++;
  }
  a = b = 0;
  rep(i, n) if (!out[i] && !(c[i][0] && c[i][1])) {
    out[qu[b++] = i] = 1;
    while (a < b) {
      t = qu[a++];
      for (int u : G[t])
        if (!out[u] && !--c[u][s[t] - 'A']) {
          out[qu[b++] = u] = 1;
        }
    }
  }
  rep(i, n) k &= out[i];
  puts(k ? "No" : "Yes");
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

vector<int> edge[216000];
queue<int> q;
int n, m, a, b;
bool removed[216000];
bool loop[216000];
string s;
int cnt[216000][2];
int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    if (a == b) {
      loop[a] = true;
    } else {
      edge[a].push_back(b);
      edge[b].push_back(a);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < edge[i].size(); j++) {
      int to = edge[i][j];
      cnt[i][s[to] - 'A']++;
    }
    if (loop[i])
      cnt[i][s[i] - 'A']++;
    if (cnt[i][0] == 0 || cnt[i][1] == 0) {
      q.push(i);
    }
  }
  int remain = n;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (removed[x])
      continue;
    removed[x] = true;
    remain--;
    for (int i = 0; i < edge[x].size(); i++) {
      int to = edge[x][i];
      if (removed[to])
        continue;
      if (--cnt[to][s[x] - 'a'] == 0) {
        q.push(to);
      }
    }
  }
  if (remain)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

vector<int> edge[216000];
queue<int> q;
int n, m, a, b;
bool removed[216000];
bool loop[216000];
string s;
int cnt[216000][2];
int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    if (a == b) {
      loop[a] = true;
    } else {
      edge[a].push_back(b);
      edge[b].push_back(a);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < edge[i].size(); j++) {
      int to = edge[i][j];
      cnt[i][s[to] - 'A']++;
    }
    if (loop[i])
      cnt[i][s[i] - 'A']++;
    if (cnt[i][0] == 0 || cnt[i][1] == 0) {
      q.push(i);
    }
  }
  int remain = n;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (removed[x])
      continue;
    removed[x] = true;
    remain--;
    for (int i = 0; i < edge[x].size(); i++) {
      int to = edge[x][i];
      if (removed[to])
        continue;
      if (--cnt[to][s[x] - 'A'] == 0) {
        q.push(to);
      }
    }
  }
  if (remain)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

const ll MOD = 1000000007;
vector<int> E[400000];
int flag[400000];
int N, M;
bool dfs(int x, int ind) {
  if (flag[x] == ind)
    return true;
  flag[x] = ind;
  for (int &to : E[x]) {
    if (dfs(to, ind))
      return true;
  }
  flag[x] = 1000000;
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  string s;
  cin >> s;
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a == b) {
      E[N + a].pb(a);
    } else if (s[a] == s[b]) {
      E[N + a].pb(b);
      E[b + N].pb(a);
    } else {
      E[a].pb(b + N);
      E[b].pb(a + N);
    }
  }
  rep(i, 0, 2 * N) {
    if (!flag[i]) {
      if (dfs(i, i + 1)) {
        cout << "Yes"
             << "\n";
        return 0;
      }
    }
  }
  cout << "No"
       << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

const ll MOD = 1000000007;
vector<int> E[400000];
int flag[400000];
int N, M;
bool dfs(int x, int ind) {
  if (flag[x] == ind)
    return true;
  flag[x] = ind;
  for (int &to : E[x]) {
    if (flag[to] < 1000000 && dfs(to, ind))
      return true;
  }
  flag[x] = 1000000;
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  string s;
  cin >> s;
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a == b) {
      E[N + a].pb(a);
    } else if (s[a] == s[b]) {
      E[N + a].pb(b);
      E[b + N].pb(a);
    } else {
      E[a].pb(b + N);
      E[b].pb(a + N);
    }
  }
  rep(i, 0, 2 * N) {
    if (!flag[i]) {
      if (dfs(i, i + 1)) {
        cout << "Yes"
             << "\n";
        return 0;
      }
    }
  }
  cout << "No"
       << "\n";
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 100005;
int n, m;
char ss[N];
int X[N], Y[N];
int du[N][2];
vector<int> vec[N];
queue<int> q;
bool in[N];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", ss + 1);
  for (int u = 1; u <= m; u++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vec[x].push_back(y);
    vec[y].push_back(x);
    du[y][ss[x] - 'A']++;
    du[x][ss[y] - 'A']++;
  }
  memset(in, true, sizeof(in));
  for (int u = 1; u <= n; u++) {
    if (du[u][0] <= 0 || du[u][1] <= 0) {
      in[u] = false;
      q.push(u);
    }
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int siz = vec[x].size();
    for (int u = 0; u < siz; u++) {
      int y = vec[x][u];
      if (in[y] == false)
        continue;
      du[y][ss[x] - 'A']--;
      if (du[y][ss[x] - 'A'] == 0) {
        in[y] = false;
        q.push(y);
      }
    }
  }
  for (int u = 1; u <= n; u++)
    if (in[u]) {
      printf("Yes\n");
      return 0;
    }
  printf("No\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 200005;
int n, m;
char ss[N];
int X[N], Y[N];
int du[N][2];
vector<int> vec[N];
queue<int> q;
bool in[N];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", ss + 1);
  for (int u = 1; u <= m; u++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vec[x].push_back(y);
    vec[y].push_back(x);
    du[y][ss[x] - 'A']++;
    du[x][ss[y] - 'A']++;
  }
  memset(in, true, sizeof(in));
  for (int u = 1; u <= n; u++) {
    if (du[u][0] <= 0 || du[u][1] <= 0) {
      in[u] = false;
      q.push(u);
    }
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int siz = vec[x].size();
    for (int u = 0; u < siz; u++) {
      int y = vec[x][u];
      if (in[y] == false)
        continue;
      du[y][ss[x] - 'A']--;
      if (du[y][ss[x] - 'A'] == 0) {
        in[y] = false;
        q.push(y);
      }
    }
  }
  for (int u = 1; u <= n; u++)
    if (in[u]) {
      printf("Yes\n");
      return 0;
    }
  printf("No\n");
  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
// includes {{{
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
// #include<deque>
// #include<multiset>
// #include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

const int N = 1e5;
std::vector<std::vector<int>> g;
int n, m;
string s;

bool ok[N];

void check(int i) {
  if (!ok[i])
    return;
  auto ng = g[i];
  ng.clear();
  int cntA = 0, cntB = 0;
  for (int j : g[i]) {
    if (ok[j]) {
      ng.push_back(j);
      if (s[j] == 'A')
        cntA++;
      else
        cntB++;
    }
  }
  g[i] = ng;
  if (!cntA || !cntB) {
    ok[i] = 0;
    for (int j : g[i]) {
      check(j);
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> m;
  cin >> s;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

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

  for (int i = 0; i < n; i++) {
    check(i);
  }

  bool ans = 0;
  for (int i = 0; i < n; i++)
    ans |= ok[i];

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

  return 0;
}


Fixed submission:
// includes {{{
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
// #include<deque>
// #include<multiset>
// #include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

const int N = 2e5;
std::vector<std::vector<int>> g;
int n, m;
string s;

bool ok[N];

void check(int i) {
  if (!ok[i])
    return;
  auto ng = g[i];
  ng.clear();
  int cntA = 0, cntB = 0;
  for (int j : g[i]) {
    if (ok[j]) {
      ng.push_back(j);
      if (s[j] == 'A')
        cntA++;
      else
        cntB++;
    }
  }
  g[i] = ng;
  if (!cntA || !cntB) {
    ok[i] = 0;
    for (int j : g[i]) {
      check(j);
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> m;
  cin >> s;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

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

  for (int i = 0; i < n; i++) {
    check(i);
  }

  bool ans = 0;
  for (int i = 0; i < n; i++)
    ans |= ok[i];

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

  return 0;
}
Problem p03256 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const ll mod = 1e9 + 7;
int n, m, col[200200], t = 0;
string s;
vvi g;
void dfs(int v) {
  sort(all(g[v]));
  g[v].erase(unique(all(g[v])), g[v].end());
  col[v] = 1;
  for (auto i : g[v]) {
    if (col[i] == 1)
      cout << "Yes", exit(0);
    if (col[i])
      continue;
    dfs(i);
  }
  col[v] = 2;
}
int main() { // DINIC ORZ
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> s;
  g.resize(2 * n);
  for (int f, t, i = 0; i < m; i++) {
    cin >> f >> t, f--, t--;
    f *= 2, t *= 2;
    if (s[f >> 1] == s[t >> 1]) {
      g[f].pb(t + 1);
      if (f != t)
        g[t].pb(f + 1);
    } else {
      g[f + 1].pb(t);
      g[t + 1].pb(f);
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    if (col[i])
      continue;
    dfs(i);
  }
  cout << "No";
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const ll mod = 1e9 + 7;
int n, m, col[500200], t = 0;
string s;
vvi g;
void dfs(int v) {
  sort(all(g[v]));
  g[v].erase(unique(all(g[v])), g[v].end());
  col[v] = 1;
  for (auto i : g[v]) {
    if (col[i] == 1)
      cout << "Yes", exit(0);
    if (col[i])
      continue;
    dfs(i);
  }
  col[v] = 2;
}
int main() { // DINIC ORZ
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> s;
  g.resize(2 * n);
  for (int f, t, i = 0; i < m; i++) {
    cin >> f >> t, f--, t--;
    f *= 2, t *= 2;
    if (s[f >> 1] == s[t >> 1]) {
      g[f].pb(t + 1);
      if (f != t)
        g[t].pb(f + 1);
    } else {
      g[f + 1].pb(t);
      g[t + 1].pb(f);
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    if (col[i])
      continue;
    dfs(i);
  }
  cout << "No";
}
Problem p03256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define neko 400010
#define f(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
int book[neko], fa[neko], t, head[neko], vis[neko], n, m, ans;
char s[neko];
struct node {
  int v, w, nex;
} e[neko << 1];
void add(int x, int y, int z) {
  e[++t].v = y, e[t].w = z, e[t].nex = head[x], head[x] = t;
  e[++t].v = x, e[t].w = z, e[t].nex = head[y], head[y] = t;
}
void dfs(int u, int col) {
  int tag = 0;
  // printf("%d %d\n",u,col);
  for (register int i = head[u], v = e[i].v; i; i = e[i].nex, v = e[i].v) {
    if (!book[i] && e[i].w ^ col) {
      book[i] = 1, vis[e[i].w] = 1;
      dfs(v, e[i].w);
      book[i] = 0;
    } else if (book[i] && e[i].w ^ col) {
      printf("Yes\n");
      exit(0);
    }
  }
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  f(i, 1, m) {
    scanf("%d%d", &x, &y);
    if (s[x] ^ s[y])
      add(x, y, 0);
    else
      add(x, y, 1);
  }
  f(i, 1, m << 1) if (!vis[i]) vis[i] = 1, dfs(e[i].v, e[i].w);
  printf("No\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define neko 400010
#define f(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
int book[neko], fa[neko], t, head[neko], vis[neko], n, m, ans;
char s[neko];
struct node {
  int v, w, nex;
} e[neko << 1];
void add(int x, int y, int z) {
  e[++t].v = y, e[t].w = z, e[t].nex = head[x], head[x] = t;
  e[++t].v = x, e[t].w = z, e[t].nex = head[y], head[y] = t;
}
void dfs(int u, int col) {
  int tag = 0;
  // printf("%d %d\n",u,col);
  for (register int i = head[u], v = e[i].v; i; i = e[i].nex, v = e[i].v) {
    if (!book[i] && e[i].w ^ col) {
      book[i] = 1, vis[i] = 1;
      dfs(v, e[i].w);
      book[i] = 0;
    } else if (book[i] && e[i].w ^ col) {
      printf("Yes\n");
      exit(0);
    }
  }
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  f(i, 1, m) {
    scanf("%d%d", &x, &y);
    if (s[x] ^ s[y])
      add(x, y, 0);
    else
      add(x, y, 1);
  }
  f(i, 1, m << 1) if (!vis[i]) vis[i] = 1, dfs(e[i].v, e[i].w);
  printf("No\n");
  return 0;
}