prithivMLmods/Coder-Stat huggingface dataset:
Problem p03043 (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 <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;

  double p[n + 1];
  for (int i = 1; i <= n; i++) {
    p[i] = 1.0;
  }

  for (int i = 1; i < k; i++) {
    int score = i;
    while (score < k) {
      score *= 2;
      p[i] /= 2.0;
    }
  }

  double ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += p[i];
  }
  ans /= (double)n;

  printf("%.10f\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;

  double p[n + 1];
  for (int i = 1; i <= n; i++) {
    p[i] = 1.0;
  }

  for (int i = 1; i <= n; i++) {
    if (i >= k) {
      break;
    }

    int score = i;
    while (score < k) {
      score *= 2;
      p[i] /= 2.0;
    }
  }

  double ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += p[i];
  }
  ans /= (double)n;

  printf("%.10f\n", ans);

  return 0;
}
Problem p03043 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;

  array<int, 17> pow2;
  vector<double> ns(N);

  int tmp = 1;
  for (auto &x : pow2) {
    x = tmp;
    tmp *= 2;
  }

  // q(n) は サイコロの目が n-1 の確立
  for (int i = 1; i <= N; ++i) {
    for (int j = 0;; ++j) {
      if (K <= i * pow2.at(j)) {
        ns.at(i - 1) = (1.0 / (double)pow2.at(j));
        break;
      }
    }
  }
  double ans = 0.0;
  for (auto i : ns) {
    ans += i;
  }
  ans /= (double)N;

  cout << fixed << setprecision(9) << ans << endl;
  return 0;
}


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

int main() {
  int N, K;
  cin >> N >> K;

  array<int, 18> pow2;
  vector<double> ns(N);

  int tmp = 1;
  for (auto &x : pow2) {
    x = tmp;
    tmp *= 2;
  }

  // q(n) は サイコロの目が n-1 の確立
  for (int i = 1; i <= N; ++i) {
    for (int j = 0;; ++j) {
      if (K <= i * pow2.at(j)) {
        ns.at(i - 1) = (1.0 / (double)pow2.at(j));
        break;
      }
    }
  }
  double ans = 0.0;
  for (auto i : ns) {
    ans += i;
  }
  ans /= (double)N;

  cout << fixed << setprecision(9) << ans << endl;
  return 0;
}
Problem p03043 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, k, pw = 1;
  cin >> n >> k;
  double p;
  p = max((ll)0, n - k + 1);
  for (ll i = k;; i = (i + 1) / 2, pw++) {
    if ((i + 1) / 2 <= n)
      p += (min(i, n + 1) - (i + 1) / 2) * pow(0.5, pw);
    if (i == 2)
      break;
  }
  cout << fixed << setprecision(9) << p / n << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, k, pw = 1;
  cin >> n >> k;
  double p;
  p = max((ll)0, n - k + 1);
  for (ll i = k;; i = (i + 1) / 2, pw++) {
    if ((i + 1) / 2 <= n)
      p += (min(i, n + 1) - (i + 1) / 2) * pow(0.5, pw);
    if (i == 1)
      break;
  }
  cout << fixed << setprecision(9) << p / n << endl;
  return 0;
}
Problem p03043 (C++)

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

int devide_by2(int i, int K) {
  int count = 0;
  while (i < K) {
    i *= 2;
    count++;
  }
  return count;
}

double bi(double &ans, int i, double N, int K) {
  double count = 1;
  for (int j = 0; j < devide_by2(i, K); j++) {
    count /= 2;
  }
  return count * (1 / N);
}

int main() {
  double N;
  int K;
  cin >> N >> K;
  cout << fixed << setprecision(11);

  double ans = 0;
  for (int i = 0; i < N; i++) {
    ans += bi(ans, i, N, K);
  }

  cout << ans << endl;
}


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

int devide_by2(int i, int K) {
  i++;
  int count = 0;
  while (i < K) {
    i *= 2;
    count++;
  }
  return count;
}

double bi(double &ans, int i, double N, int K) {
  double count = 1;
  for (int j = 0; j < devide_by2(i, K); j++) {
    count /= 2;
  }
  return count * (1 / N);
}

int main() {
  double N;
  int K;
  cin >> N >> K;
  cout << fixed << setprecision(11);

  double ans = 0;
  for (int i = 0; i < N; i++) {
    ans += bi(ans, i, N, K);
  }

  cout << ans << endl;
}
Problem p03043 (Python)

Buggy submission (status: Runtime Error):
import math

n, k = map(int, input().split())

# for small
score = 0
times = []
for i in range(1, min(n + 1, k)):
    times.append(math.ceil(math.log2((k / i))))
bottom_max = times[0]
for t in times:
    score += 2 ** (bottom_max - t)
score /= 2 ** (bottom_max) * n

# for big
big = [i for i in range(k, max(k, n + 1))]
score += len(big) / n
print("{:.10f}".format(score))


Fixed submission:
import math

n, k = map(int, input().split())

# for small
score = 0
times = []
for i in range(1, min(n + 1, k)):
    times.append(math.ceil(math.log2((k / i))))

if len(times):
    bottom_max = times[0]
    for t in times:
        score += 2 ** (bottom_max - t)
    score /= 2 ** (bottom_max) * n

# for big
big = [i for i in range(k, max(k, n + 1))]
score += len(big) / n
print("{:.10f}".format(score))
Problem p03043 (Python)

Buggy submission (status: Runtime Error):
import math

N, K = map(int, input().split())
p = 0

for i in range(1, min(K, N + 1)):
    print(math.ceil(math.log2(K / i)))
    p += 2 ** (-math.ceil(math.log2(K / i)))

p += max(0, N - K + 1)
print(p / N)


Fixed submission:
import math

N, K = map(int, input().split())
p = 0

count = 1
while True:
    p += (
        max(0, min(N + 1, math.ceil(K / 2 ** (count - 1))) - math.ceil(K / 2**count))
        * 0.5**count
    )
    count += 1
    if K / 2**count < 0.5:
        break

p += max(0, N - K + 1)
print(p / N)
Problem p03043 (Python)

Buggy submission (status: Runtime Error):
def resolve():
    N, K = [int(i) for i in input().split()]
    import math

    ans = 0
    for i in range(1, N + 1):
        if i < K:
            coin = math.ceil(math.log2(K / i))
            ans += (1 / N) * ((1 / 2) ** coin)
        else:
            ans += 1 / N
    print(ans)


resolve()


Fixed submission:
n, k = map(int, input().split())
ans = 0
for i in range(1, n + 1):
    tmp = 1 / n
    p = i
    while p < k:
        p = 2 * p
        tmp = tmp / 2
    ans += tmp
print(ans)
Problem p03043 (Python)

Buggy submission (status: Runtime Error):
[n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
for i in range(m):
    idx = a.index(max(a))
    a[idx] = int(a[idx] / 2)
print(sum(a))


Fixed submission:
[n, k] = [int(i) for i in input().split()]
total = 0
for i in range(1, n + 1):
    point = i
    prob = 1 / n
    while point < k:
        point *= 2
        prob /= 2
    total += prob
print(total)
Problem p03043 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<

int main() {
  int n, k;
  cin >> n >> k;
  double ans = 0;
  int temp;
  int cnt;
  for (int i = 1; i <= n; i++) {
    temp = i;
    cnt = 0;
    while (temp < k) {
      cnt++;
      temp * 2;
    }
    ans += pow(0.5, cnt);
  }
  ans /= (double)n;
  cout << fixed << setprecision(11) << ans << endl;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<

int main() {
  int n, k;
  cin >> n >> k;
  double ans = 0;
  int temp;
  int cnt;
  for (int i = 1; i <= n; i++) {
    temp = i;
    cnt = 0;
    for (cnt; temp < k; cnt++) {
      temp *= 2;
    }
    ans += pow(0.5, cnt);
  }
  ans /= (double)n;
  cout << fixed << setprecision(11) << ans << endl;
  return 0;
}
Problem p03043 (C++)

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

using namespace std;

int N, K;
double t[10012];

int main() {
  while (cin >> N >> K) {
    memset(t, 0, sizeof(t));
    for (int i = K; i >= 1; i--) {
      if (i >= K) {
        t[i] = 1.0;
      } else if (i * 2 >= K) {
        t[i] = 0.5;
      } else {
        t[i] = t[i * 2] / 2;
      }
    }
    double sum = 0.0;
    for (int i = 1; i <= N; ++i) {
      if (i >= K) {
        sum += 1.0;
      } else {
        sum += t[i];
      }
    }
    printf("%.012f\n", sum / N);
  }
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int N, K;
double t[100120];

int main() {
  while (cin >> N >> K) {
    memset(t, 0, sizeof(t));
    for (int i = K; i >= 1; i--) {
      if (i >= K) {
        t[i] = 1.0;
      } else if (i * 2 >= K) {
        t[i] = 0.5;
      } else {
        t[i] = t[i * 2] / 2;
      }
    }
    double sum = 0.0;
    for (int i = 1; i <= N; ++i) {
      if (i >= K) {
        sum += 1.0;
      } else {
        sum += t[i];
      }
    }
    printf("%.012f\n", sum / N);
  }
}
Problem p03043 (C++)

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

int main(void) {
  int N, K;
  cin >> N >> K;
  double ans = 0;

  /* 出目がiのとき */
  for (int i = 1; i <= N; i++) {
    double p = 1. / N;
    int num = 0;
    for (int point = i; point <= K - 1; point *= 2) {
      p *= 1. / 2.;
      num++;
    }
    ans += p;
  }
  assert(ans < 1);
  cout << fixed << setprecision(15) << ans << endl;

  return 0;
}


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

int main(void) {
  int N, K;
  cin >> N >> K;
  double ans = 0;

  /* 出目がiのとき */
  for (int i = 1; i <= N; i++) {
    double p = 1. / N;
    int num = 0;
    for (int point = i; point <= K - 1; point *= 2) {
      p *= 1. / 2.;
      num++;
    }
    ans += p;
  }
  cout << fixed << setprecision(15) << ans << endl;

  return 0;
}
Problem p03043 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define MP make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef long long LL;

static const int INF = INT_MAX;
static const int MIN = INT_MIN;
static const LL L_INF = LLONG_MAX;
static const int MOD = 1000000000 + 7;
static const int SIZE = 100005;

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

  double n, k;
  cin >> n >> k;

  vector<double> p;
  double tmp = 1.0;
  int pos = 1;
  while (tmp * tmp < 100005) {
    p.pb(tmp);
    tmp *= 2;
    ++pos;
  }
  p.pb(tmp);

  double res = 0.0;
  pos = 0;
  tmp = n;
  while (n > 0) {
    while (p[pos] * n < k)
      ++pos;
    res += (1.0 / tmp) * (1.0 / p[pos]);
    --n;
  }

  printf("%.9lf\n", res);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define MP make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef long long LL;

static const int INF = INT_MAX;
static const int MIN = INT_MIN;
static const LL L_INF = LLONG_MAX;
static const int MOD = 1000000000 + 7;
static const int SIZE = 100005;

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

  double n, k;
  cin >> n >> k;

  vector<double> p;
  double tmp = 1.0;
  int pos = 1;
  while (tmp < 100005) {
    p.pb(tmp);
    tmp *= 2;
    ++pos;
  }
  p.pb(tmp);

  double res = 0.0;
  pos = 0;
  tmp = n;
  while (n > 0) {
    while (p[pos] * n < k)
      ++pos;
    res += (1.0 / tmp) * (1.0 / p[pos]);
    --n;
  }

  printf("%.9lf\n", res);

  return 0;
}
Problem p03043 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> iP;
typedef pair<P, P> PP;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 100005;

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

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

  double n, k;
  cin >> n >> k;
  vector<pair<double, int>> pw;
  double e = 1.0;
  int cnt = 0;
  while (e < 100005) {
    pw.pb(MP(e, cnt));
    e *= 2.0;
    ++cnt;
  }
  int pos = n;
  int it = 0;
  double res = 0;
  while (pos >= 1) {
    while (pos * pw[it].F < k) {
      ++it;
    }
    double tmp = 1.0 / pow(2.0, (double)pw[it].S);
    res += tmp * (1 / n);
    --pos;
  }
  printf("%.9lf\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> iP;
typedef pair<P, P> PP;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 100005;

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

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

  double n, k;
  cin >> n >> k;
  vector<pair<double, int>> pw;
  double e = 1.0;
  int cnt = 0;
  while (e < 100005) {
    pw.pb(MP(e, cnt));
    e *= 2.0;
    ++cnt;
  }
  pw.pb(MP(e, cnt));

  int pos = n;
  int it = 0;
  double res = 0;
  while (pos >= 1) {
    while (pos * pw[it].F < k) {
      ++it;
    }
    double tmp = 1.0 / pow(2.0, (double)pw[it].S);
    res += tmp * (1 / n);
    --pos;
  }
  printf("%.9lf\n", res);
  return 0;
}
Problem p03043 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  double total = 0;

  for (int i = 0; i < n; i++) {
    long a = i;
    double count = 1.0 / n;
    while (a < k) {
      a *= 2;
      count = count / 2.0;
    }

    total += count;
  }

  cout << total << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  double total = 0;
  cout << fixed << setprecision(16);
  for (int i = 1; i <= n; i++) {
    long a = i;
    double count = 1.0 / n;
    while (a < k) {
      a *= 2;
      count = count / 2.0;
    }

    total += count;
  }

  cout << total << endl;
}
Problem p03043 (C++)

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

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "

using ld = long double;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  vector<ll> pp;
  f(i, 1, n + 1) {
    if (i < k) {
      ll r = ceil(log2(k) - log2(i));
      pp.pb(r);
    } else {
      cnt++;
    }
  }
  if (pp.empty()) {
    ld ans = ((ld)cnt) / ((ld)n);
    cout << setprecision(14) << fixed << ans << "\n";
    return 0;
  }
  ll ans = 1;
  sort(all(pp), greater<ll>());
  ll g = (1 << pp[0]);
  f(i, 1, (ll)pp.size()) {
    ll kk = (1 << pp[i]);
    ans += (g / kk);
  }
  ans = ans + cnt * g;
  ll gg = __gcd(n, ans);
  n = n / gg;
  ans = ans / gg;
  gg = __gcd(ans, g);
  ans = ans / gg;
  g = g / gg;
  g = g * n;
  ld res = ((ld)ans) / ((ld)g);
  cout << setprecision(14) << fixed << res << "\n";
  return 0;
}

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

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "

using ld = long double;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  vector<ll> pp;
  f(i, 1, n + 1) {
    if (i < k) {
      ll r = ceil(log2(k) - log2(i));
      pp.pb(r);
    } else {
      cnt++;
    }
  }
  if (pp.empty()) {
    ld ans = ((ld)cnt) / ((ld)n);
    cout << setprecision(14) << fixed << ans << "\n";
    return 0;
  }
  ll ans = 1;
  sort(all(pp), greater<ll>());
  ll g = (1 << pp[0]);
  f(i, 1, (ll)pp.size()) {
    ll kk = (1 << pp[i]);
    ans += (g / kk);
  }
  ans = ans + cnt * g;
  ll gg = __gcd(n, ans);
  n = n / gg;
  ans = ans / gg;
  gg = __gcd(ans, g);
  ans = ans / gg;
  g = g / gg;
  g = g * n;
  ld res = ((ld)ans) / ((ld)g);
  cout << setprecision(14) << fixed << res << "\n";
  return 0;
}
Problem p03043 (C++)

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

int main() {

  /*long long n, k; cin >> n >> k;
double res = 0.0;
for(long long i=1; i<n+1; ++i){
  double tmp = 1.0;
  long long d = i;
  while (d < k) d *= 2, tmp /= 2.0;
  res += tmp;
}
res /= n;
cout << fixed << setprecision(10) << res << endl;*/

  long long n, k;
  cin >> n >> k;
  double res = 0.0;
  for (long long i = 1; i < n + 1; ++i) {
    double tmp = 1.0;
    long long d = i;
    while (d < k) {
      i *= 2;
      tmp /= 2.0;
    }
    res += tmp;
  }
  res /= n;
  cout << fixed << setprecision(15) << res << endl;
  return 0;
}

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

int main() {

  /*long long n, k; cin >> n >> k;
double res = 0.0;
for(long long i=1; i<n+1; ++i){
  double tmp = 1.0;
  long long d = i;
  while (d < k) d *= 2, tmp /= 2.0;
  res += tmp;
}
res /= n;
cout << fixed << setprecision(10) << res << endl;*/

  long long n, k;
  cin >> n >> k;
  double res = 0.0;
  for (long long i = 1; i < n + 1; ++i) {
    double tmp = 1.0;
    long long d = i;
    while (d < k) {
      d *= 2;
      tmp /= 2.0;
    }
    res += tmp;
  }
  res /= n;
  cout << fixed << setprecision(15) << res << endl;
  return 0;
}
Problem p03043 (C++)

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

int main() {
  double n, k;
  cin >> n >> k;
  double ans = 0;

  for (int i = 1; i <= n; i++) {
    int tmp = i;
    double count = 1.0 / n;
    while (k > i) {
      tmp *= 2;
      count /= 2;
    }
    ans += count;
  }

  cout << setprecision(15) << ans << endl;
}


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

int main() {
  double n, k;
  cin >> n >> k;
  double ans = 0;

  for (int i = 1; i <= n; i++) {
    int tmp = i;
    double count = 1.0 / n;
    while (k > tmp) {
      tmp *= 2;
      count /= 2;
    }
    ans += count;
  }

  cout << setprecision(15) << ans << endl;
}
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
def dfs(i, d):
    # print(i, d)
    for j, w in node_array[i]:
        if color_array[j] == -1:
            color_array[j] = (d + w) % 2
            dfs(j, d + w)


N = int(input())
node_array = [[] for i in range(N)]
color_array = [-1] * N

for _ in range(N - 1):
    i, j, w = map(int, input().split())
    node_array[i - 1].append((j - 1, w))
    node_array[j - 1].append((i - 1, w))

color_array[0] = 0
dfs(0, 0)

for i in range(N):
    print(color_array[i])


Fixed submission:
from sys import setrecursionlimit

setrecursionlimit(10**8)


def dfs(i, d):
    # print(i, d)
    for j, w in node_array[i]:
        if color_array[j] == -1:
            color_array[j] = (d + w) % 2
            dfs(j, d + w)


N = int(input())
node_array = [[] for i in range(N)]
color_array = [-1] * N

for _ in range(N - 1):
    i, j, w = map(int, input().split())
    node_array[i - 1].append((j - 1, w))
    node_array[j - 1].append((i - 1, w))

color_array[0] = 0
dfs(0, 0)

for i in range(N):
    print(color_array[i])
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK


if __name__ == "__main__":
    n = int(input())
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]

    g = [[] for _ in range(n)]

    for u, v, w in uvw:
        u -= 1
        v -= 1
        g[u].append((v, w))
        g[v].append((u, w))

    tree = Tree(g)
    tree.dfs(0)
    ans = [x % 2 for x in tree.depths]
    print(*ans, sep="\n")


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK


if __name__ == "__main__":
    n = int(input())
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]

    g = [[] for _ in range(n)]

    for u, v, w in uvw:
        u -= 1
        v -= 1
        g[u].append((v, w))
        g[v].append((u, w))

    tree = Tree(g)
    tree.dfs(0)
    ans = [x % 2 for x in tree.depths]
    print(*ans, sep="\n")
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

vector<int> dir;

void dfs(const Graph &G, int v, int p, int cur) {
  dir[v] = cur;
  for (auto e : G[v]) {
    if (e.first == p)
      continue; // e.first(nv)が親pだったらダメ

    if (e.second % 2)
      dfs(G, e.first, v, 1 - cur);
    else
      dfs(G, e.first, v, cur);
  }
}

int main() {
  int N;
  cin >> N;

  Graph G(N);
  G.assign(N, vector<Edge>());
  for (int i = 0; i < N - 1; ++i) {
    int a, b, w;
    --a, --b;
    G[a].push_back(Edge(b, w));
    G[b].push_back(Edge(a, w));
  }

  dir.assign(N, 0);
  dfs(G, 0, -1, 0);
  for (int i = 0; i < N; ++i)
    cout << dir[i] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

vector<int> dir;

void dfs(const Graph &G, int v, int p, int cur) {
  dir[v] = cur;
  for (auto e : G[v]) {
    if (e.first == p)
      continue; // e.first(nv)が親pだったらダメ

    if (e.second % 2)
      dfs(G, e.first, v, 1 - cur);
    else
      dfs(G, e.first, v, cur);
  }
}

int main() {
  int N;
  cin >> N;

  Graph G(N);
  G.assign(N, vector<Edge>());
  for (int i = 0; i < N - 1; ++i) {
    int a, b, w;
    cin >> a >> b >> w;
    --a, --b;
    G[a].push_back(Edge(b, w));
    G[b].push_back(Edge(a, w));
  }

  dir.assign(N, 0);
  dfs(G, 0, -1, 0);
  for (int i = 0; i < N; ++i)
    cout << dir[i] << endl;
}
Problem p03044 (C++)

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

using namespace std;

typedef long long int lli;

vector<vector<int>> vec(100001, vector<int>());
vector<bool> ans(100001, true);
vector<bool> used(100001, false);

void func(int idx) {
  used[idx] = true;
  // cout << "idx : " << idx << endl;

  for (int i = 0; i < vec[idx].size(); i += 2) {
    if (used[vec[idx][i]]) {
      continue;
    }

    if (vec[idx][i + 1] % 2) {
      ans[vec[idx][i]] = !ans[idx];
    } else {
      ans[vec[idx][i + 1]] = ans[idx];
    }
    func(vec[idx][i]);
  }
}

int main() {
  int n, u, v, w;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> w;
    vec[u].push_back(v);
    vec[u].push_back(w);
    vec[v].push_back(u);
    vec[v].push_back(w);
  }

  func(1);

  for (int i = 0; i < n; i++) {
    cout << ((ans[i + 1]) ? 1 : 0) << endl;
  }

  return 0;
}

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

using namespace std;

typedef long long int lli;

vector<vector<int>> vec(100001, vector<int>());
vector<bool> ans(100001, true);
vector<bool> used(100001, false);

void func(int idx) {
  used[idx] = true;
  // cout << "idx : " << idx << endl;

  for (int i = 0; i < vec[idx].size(); i += 2) {
    if (used[vec[idx][i]]) {
      continue;
    }

    if (vec[idx][i + 1] % 2) {
      ans[vec[idx][i]] = !ans[idx];
    } else {
      ans[vec[idx][i]] = ans[idx];
    }
    func(vec[idx][i]);
  }
}

int main() {
  int n, u, v, w;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> w;
    vec[u].push_back(v);
    vec[u].push_back(w);
    vec[v].push_back(u);
    vec[v].push_back(w);
  }

  func(1);

  for (int i = 0; i < n; i++) {
    cout << ((ans[i + 1]) ? 1 : 0) << endl;
  }

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> to(N);
  vector<vector<int>> cost(N);
  for (int i = 1; i < N; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    to[a].push_back(b);
    cost[a].push_back(w);
    to[b].push_back(a);
    cost[b].push_back(w);
  }

  int cnt = 0;
  vector<int> ans(N, -1);

  queue<int> Q;
  Q.push(0);
  ans[0] = 0;

  while (!Q.empty()) {
    int p = Q.front();
    Q.pop();
    for (int i = 0; i < to[p].size(); i++) {
      int u = to[p][i];
      if (ans[u] != -1) {
        continue;
      }
      ans[u] = (ans[p] + cost[p][i]) % 2;
      Q.push(u);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> to(N);
  vector<vector<int>> cost(N);
  for (int i = 1; i < N; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to[a].push_back(b);
    cost[a].push_back(w);
    to[b].push_back(a);
    cost[b].push_back(w);
  }

  int cnt = 0;
  vector<int> ans(N, -1);

  queue<int> Q;
  Q.push(0);
  ans[0] = 0;

  while (!Q.empty()) {
    int p = Q.front();
    Q.pop();
    for (int i = 0; i < to[p].size(); i++) {
      int u = to[p][i];
      if (ans[u] != -1) {
        continue;
      }
      ans[u] = (ans[p] + cost[p][i]) % 2;
      Q.push(u);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
vector<vector<int>> to(n + 100);
vector<vector<int>> cost(n + 100);

int main() {
  /* 入力受け取り */
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
    cost[a].push_back(w);
    cost[b].push_back(w);
  }

  /* BFS */
  queue<int> que;
  vector<int> ans(n, -1);
  que.push(0);
  ans[0] = 0;
  // キューが0になるまでループ
  while (que.size()) {
    // 先頭のキューを取ってくる
    int p = que.front();
    que.pop();
    // 先頭のキューに繋がっているノードを探索
    for (int i = 0; i < to[p].size(); ++i) {
      int u = to[p][i];
      int w = cost[p][i];
      if (ans[u] != -1)
        continue;
      ans[u] = (ans[p] + w) % 2;
      que.push(u);
    }
  }

  for (int i = 0; i < n; ++i)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
vector<vector<int>> to(n + 1000000);
vector<vector<int>> cost(n + 1000000);

int main() {
  /* 入力受け取り */
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
    cost[a].push_back(w);
    cost[b].push_back(w);
  }

  /* BFS */
  queue<int> que;
  vector<int> ans(n, -1);
  que.push(0);
  ans[0] = 0;
  // キューが0になるまでループ
  while (que.size()) {
    // 先頭のキューを取ってくる
    int p = que.front();
    que.pop();
    // 先頭のキューに繋がっているノードを探索
    for (int i = 0; i < to[p].size(); ++i) {
      int u = to[p][i];
      int w = cost[p][i];
      if (ans[u] != -1)
        continue;
      ans[u] = (ans[p] + w) % 2;
      que.push(u);
    }
  }

  for (int i = 0; i < n; ++i)
    cout << ans[i] << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<pil>> G(N, vector<pil>(N));
  for (int n = 1; n <= N - 1; ++n) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    --u;
    --v;
    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }

  vector<ll> D(N, INF);
  vector<int> Used(N, 0);
  D[0] = 0;
  Used[0] = 1;
  queue<int> Que;
  Que.push(0);
  while (!Que.empty()) {
    int Temp = Que.front();
    Que.pop();
    for (auto e : G[Temp]) {
      int u = e.first;
      ll w = e.second;
      if (Used[u])
        continue;
      Used[u] = 1;
      D[u] = D[Temp] + w;
      Que.push(u);
    }
  }
  for (int n = 0; n < N; ++n) {
    cout << D[n] % 2 << '\n';
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<pil>> G(N);
  for (int n = 1; n <= N - 1; ++n) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    --u;
    --v;
    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }

  vector<ll> D(N, INF);
  vector<int> Used(N, 0);
  D[0] = 0;
  Used[0] = 1;
  queue<int> Que;
  Que.push(0);
  while (!Que.empty()) {
    int Temp = Que.front();
    Que.pop();
    for (auto e : G[Temp]) {
      int u = e.first;
      ll w = e.second;
      if (Used[u])
        continue;
      Used[u] = 1;
      D[u] = D[Temp] + w;
      Que.push(u);
    }
  }
  for (int n = 0; n < N; ++n) {
    cout << D[n] % 2 << '\n';
  }

  return 0;
}
Problem p03044 (C++)

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

int col[100000];
struct edge {
  int to;
  ll w;
};
vector<edge> G[100000];

void dfs(int u, bool c) {

  col[u] = c;

  for (int i = 0; i < G[u].size(); i++) {
    if (col[G[u][i].to] != -1)
      continue;

    ll dif;
    dif = G[u][i].w;

    if (dif % 2)
      dfs(G[u][i].to, !c);
    else
      dfs(G[u][i].to, c);
  }
}

int main() {
  fill(col, col + 100000, -1);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    u--, v--;
    G[u].push_back({v, w});
    G[v].push_back({u, w});
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << col[i] << endl;
  }
  return 0;
}

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

int col[100000];
struct edge {
  int to;
  ll w;
};
vector<edge> G[100000];

void dfs(int u, bool c) {

  col[u] = c;

  for (int i = 0; i < G[u].size(); i++) {
    if (col[G[u][i].to] != -1)
      continue;

    ll dif;
    dif = G[u][i].w;

    if (dif % 2)
      dfs(G[u][i].to, !c);
    else
      dfs(G[u][i].to, c);
  }
}

int main() {
  fill(col, col + 100000, -1);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    u--, v--;
    G[u].push_back({v, w});
    G[v].push_back({u, w});
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << col[i] << endl;
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

using int_t = int64_t;
struct edge_t {
  int_t to, len;
  edge_t(int_t t, int_t l) : to(t), len(l) {}
};

void dfs(const vector<vector<edge_t>> &E, vector<int> &color, int s, int c) {
  color[s] = c;

  for (auto &next : E[s]) {
    if (color[next.to] != -1)
      continue;

    dfs(E, color, next.to, (next.len % 2 == 0) ? c : !c);
  }
}

int main(void) {
  int N;
  cin >> N;

  vector<vector<edge_t>> E(N * N);
  vector<int> color(N, -1);

  for (int i = 0; i < N - 1; i++) {
    int_t from;
    cin >> from;
    int_t to;
    cin >> to;
    int_t len;
    cin >> len;

    from--, to--;

    E[from].emplace_back(to, len);
    E[to].emplace_back(from, len);
  }

  for (int i = 0; i < N; i++) {
    if (color[i] == -1) {
      dfs(E, color, i, 0);
    }
  }

  for (auto c : color) {
    cout << c << endl;
  }

  return 0;
}


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

using namespace std;

using int_t = int64_t;
struct edge_t {
  int_t to, len;
  edge_t(int_t t, int_t l) : to(t), len(l) {}
};

void dfs(const vector<vector<edge_t>> &E, vector<int> &color, int s, int c) {
  color[s] = c;

  for (auto &next : E[s]) {
    if (color[next.to] != -1)
      continue;

    dfs(E, color, next.to, (next.len % 2 == 0) ? c : !c);
  }
}

int main(void) {
  int N;
  cin >> N;

  vector<vector<edge_t>> E(N);
  vector<int> color(N, -1);

  for (int i = 0; i < N - 1; i++) {
    int_t from;
    cin >> from;
    int_t to;
    cin >> to;
    int_t len;
    cin >> len;

    from--, to--;

    E[from].emplace_back(to, len);
    E[to].emplace_back(from, len);
  }

  for (int i = 0; i < N; i++) {
    if (color[i] == -1) {
      dfs(E, color, i, 0);
    }
  }

  for (auto c : color) {
    cout << c << endl;
  }

  return 0;
}
Problem p03044 (C++)

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

using namespace std;

void dfs(const vector<vector<pair<int, int>>> &g, int u, int c, vector<int> &a,
         vector<int> &vi) {
  if (vi[u])
    return;
  vi[u] = 1;
  a[u] = c;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first;
    if (!vi[v])
      dfs(g, v, g[u][i].second ? c ^ 1 : c, a, vi);
  }
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(make_pair(v, w % 2));
    g[v].push_back(make_pair(u, w % 2));
  }

  vector<int> a(n), vi(n);
  dfs(g, 0, 0, a, vi);

  for (int i = 0; i < n; i++)
    cout << a[i] << endl;

  return 0;
}


Fixed submission:
// D - Even Relation
#include <bits/stdc++.h>

using namespace std;

void dfs(const vector<vector<pair<int, int>>> &g, int u, int c, vector<int> &a,
         vector<int> &vi) {
  if (vi[u])
    return;
  vi[u] = 1;
  a[u] = c;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first;
    if (!vi[v])
      dfs(g, v, g[u][i].second ? c ^ 1 : c, a, vi);
  }
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(make_pair(v, w % 2));
    g[v].push_back(make_pair(u, w % 2));
  }

  vector<int> a(n), vi(n);
  dfs(g, 0, 0, a, vi);

  for (int i = 0; i < n; i++)
    cout << a[i] << endl;

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 10010
#define ll long long
const ll mod = 1e9 + 7;
struct node {
  int v, w;
};
vector<node> g[N];
int vis[N];
void dfs(int x) {
  int sz = g[x].size();
  for (int i = 0; i < sz; i++) {
    int v = g[x][i].v, w = g[x][i].w;
    if (vis[v] != -1)
      continue;
    if (w & 1)
      vis[v] = 1 ^ vis[x];
    else
      vis[v] = vis[x];
    dfs(v);
  }
}
int main() {
  memset(vis, -1, sizeof vis);
  int n, u, v, w;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  vis[1] = 0;
  dfs(1);
  for (int i = 1; i <= n; i++)
    printf("%d\n", vis[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
const ll mod = 1e9 + 7;
struct node {
  int v, w;
};
vector<node> g[N];
int vis[N];
void dfs(int x) {
  int sz = g[x].size();
  for (int i = 0; i < sz; i++) {
    int v = g[x][i].v, w = g[x][i].w;
    if (vis[v] != -1)
      continue;
    if (w & 1)
      vis[v] = 1 ^ vis[x];
    else
      vis[v] = vis[x];
    dfs(v);
  }
}
int main() {
  memset(vis, -1, sizeof vis);
  int n, u, v, w;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  vis[1] = 0;
  dfs(1);
  for (int i = 1; i <= n; i++)
    printf("%d\n", vis[i]);
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  // vector<pair<int, int> > G[10001];
  map<int, vector<pair<int, int>>> G;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
  }
  queue<pair<int, int>> Q;
  Q.push(make_pair(0, 0));
  int color[10001] = {0};
  int checked[10001] = {0};
  while (Q.size()) {
    pair<int, int> p = Q.front();
    Q.pop();
    checked[p.first]++;

    if (p.second % 2)
      color[p.first] = 1;
    for (int i = 0; i < G[p.first].size(); i++) {
      pair<int, int> n = G[p.first][i];
      if (checked[n.first]) {
        continue;
      }
      Q.push(make_pair(n.first, p.second + n.second));
    }
  }
  for (int i = 0; i < N; i++) {
    cout << color[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  // vector<pair<int, int> > G[10001];
  map<int, vector<pair<int, int>>> G;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
  }
  queue<pair<int, int>> Q;
  Q.push(make_pair(0, 0));
  int color[100001] = {0};
  int checked[100001] = {0};
  while (Q.size()) {
    pair<int, int> p = Q.front();
    Q.pop();
    checked[p.first]++;

    if (p.second % 2)
      color[p.first] = 1;
    for (int i = 0; i < G[p.first].size(); i++) {
      pair<int, int> n = G[p.first][i];
      if (checked[n.first]) {
        continue;
      }
      Q.push(make_pair(n.first, p.second + n.second));
    }
  }
  for (int i = 0; i < N; i++) {
    cout << color[i] << endl;
  }

  return 0;
}
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
def dfs(u):
    for v, w in edge[u]:
        if trail[v] is None:
            trail[v] = (trail[u] + w) % 2
            dfs(v)


N = int(input())
trail = [None] * N
edge = [[] for _ in range(N)]

for i in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    edge[u].append((v, w))
    edge[v].append((u, w))

trail[0] = 0
dfs(0)

for t in trail:
    print(t)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


def dfs(u):
    for v, w in edge[u]:
        if trail[v] is None:
            trail[v] = (trail[u] + w) % 2
            dfs(v)


N = int(input())
trail = [None] * N
edge = [[] for _ in range(N)]

for i in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    edge[u].append((v, w))
    edge[v].append((u, w))

trail[0] = 0
dfs(0)

for t in trail:
    print(t)
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
def rec(edges, nodes, cp, cdistance, visited):
    if not visited[cp]:
        visited[cp] = True
        nodes[cp] = cdistance % 2 == 0
        if edges[cp]:
            for v, w in edges[cp]:
                rec(edges, nodes, v, cdistance + w, visited)


def main():
    N = int(input())
    nodes = [False] * N
    visited = [False] * N
    edges = [[] for _ in range(N)]

    for _ in range(N - 1):
        u, v, w = map(int, input().split())
        edges[u - 1].append((v - 1, w))
        edges[v - 1].append((u - 1, w))

    rec(edges, nodes, 0, 0, visited)

    for n in nodes:
        print(0 if n else 1)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(10**9)


def rec(edges, nodes, cp, cdistance, visited):
    if not visited[cp]:
        visited[cp] = True
        nodes[cp] = cdistance % 2 == 0
        if edges[cp]:
            for v, w in edges[cp]:
                rec(edges, nodes, v, cdistance + w, visited)


def main():
    N = int(input())
    nodes = [False] * N
    visited = [False] * N
    edges = [[] for _ in range(N)]

    for _ in range(N - 1):
        u, v, w = map(int, input().split())
        edges[u - 1].append((v - 1, w))
        edges[v - 1].append((u - 1, w))

    rec(edges, nodes, 0, 0, visited)

    for n in nodes:
        print(0 if n else 1)


if __name__ == "__main__":
    main()
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
vector<pair<lld, lld>> G[55555];
lld dis[555555];
void dfs(lld u, lld par = -1) {
  for (lld i = 0; i < G[u].size(); i++) {
    lld to = G[u][i].first;
    lld w = G[u][i].second;
    if (to != par) {
      dis[to] += dis[u] + w;
      dfs(to, u);
    }
  }
  return;
}
int main() {
  std::ios::sync_with_stdio(false);

  lld n;
  cin >> n;
  for (lld i = 0; i < n - 1; i++) {
    lld u, v, w;
    cin >> u >> v >> w;
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
  }
  dfs(1);
  vector<lld> color(n + 1, 0);
  for (lld i = 1; i <= n; i++) {
    if (dis[i] % 2) {
      color[i] = 0;
    } else {
      color[i] = 1;
    }
  }
  for (lld i = 1; i <= n; i++) {
    cout << color[i] << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
vector<pair<lld, lld>> G[555555];
lld dis[555555];
void dfs(lld u, lld par = -1) {
  for (lld i = 0; i < G[u].size(); i++) {
    lld to = G[u][i].first;
    lld w = G[u][i].second;
    if (to != par) {
      dis[to] += dis[u] + w;
      dfs(to, u);
    }
  }
  return;
}
int main() {
  std::ios::sync_with_stdio(false);

  lld n;
  cin >> n;
  for (lld i = 0; i < n - 1; i++) {
    lld u, v, w;
    cin >> u >> v >> w;
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
  }
  dfs(1);
  vector<lld> color(n + 1, 0);
  for (lld i = 1; i <= n; i++) {
    if (dis[i] % 2) {
      color[i] = 0;
    } else {
      color[i] = 1;
    }
  }
  for (lld i = 1; i <= n; i++) {
    cout << color[i] << "\n";
  }
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;

struct edge {
  ll v, w;
};

int main() {
  ll n;
  cin >> n;
  vector<vector<edge>> adj(n);
  ll a, b, c;
  rep(i, n) {
    cin >> a >> b >> c;
    a--, b--;
    adj[a].push_back((edge){b, c});
    adj[b].push_back((edge){a, c});
  }
  vector<ll> col(n, -1);
  queue<ll> q;
  q.push(0);
  col[0] = 0;
  while (!q.empty()) {
    ll now = q.front();
    q.pop();
    rep(i, adj[now].size()) {
      ll nv = adj[now][i].v, nw = adj[now][i].w;
      if (col[nv] == -1) {
        if (nw % 2)
          col[nv] = !(bool)col[now];
        else
          col[nv] = col[now];
        q.push(nv);
      }
    }
  }
  rep(i, n) cout << col[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;

struct edge {
  ll v, w;
};

int main() {
  ll n;
  cin >> n;
  vector<vector<edge>> adj(n);
  ll a, b, c;
  rep(i, n - 1) {
    cin >> a >> b >> c;
    a--, b--;
    adj[a].push_back((edge){b, c});
    adj[b].push_back((edge){a, c});
  }
  vector<ll> col(n, -1);
  queue<ll> q;
  q.push(0);
  col[0] = 0;
  while (!q.empty()) {
    ll now = q.front();
    q.pop();
    rep(i, adj[now].size()) {
      ll nv = adj[now][i].v, nw = adj[now][i].w;
      if (col[nv] == -1) {
        if (nw % 2)
          col[nv] = !(bool)col[now];
        else
          col[nv] = col[now];
        q.push(nv);
      }
    }
  }
  rep(i, n) cout << col[i] << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n;
int d[100005], cc[100005];
vector<pair<int, int>> edge[100005];
void dfs(int x, int prev) {
  for (int i = 0; i < edge[x].size(); i++) {
    int y = edge[x][i].first;
    int z = edge[x][i].second;
    if (z == prev)
      continue;
    d[y] = d[x] + z;
    cc[y] = d[y] % 2;
    dfs(y, x);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c = c % 2;
    edge[a].push_back(make_pair(b, c));
    edge[b].push_back(make_pair(a, c));
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    cout << cc[i] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n;
int d[100005], cc[100005];
vector<pair<int, int>> edge[100005];
void dfs(int x, int prev) {
  for (int i = 0; i < edge[x].size(); i++) {
    int y = edge[x][i].first;
    int z = edge[x][i].second;
    if (y == prev)
      continue;
    d[y] = d[x] + z;
    cc[y] = d[y] % 2;
    dfs(y, x);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c = c % 2;
    edge[a].push_back(make_pair(b, c));
    edge[b].push_back(make_pair(a, c));
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    cout << cc[i] << endl;
  return 0;
}