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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  int n;
  cin >> n;
  vector<int> ans(n, 0);
  queue<int> q;
  //    set<int> S;
  vector<bool> vis(n + 10, false);
  vector<vector<pair<int, int>>> adj(n + 10);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].emplace_back(v, w);
    adj[v].emplace_back(u, w);
    //        if (w % 2 == 0) {
    //            ans[u] = ans[v] = 1;
    //            vis[u] = vis[v] = true;
    //            S.insert(u);
    //            S.insert(v);
    //        }
  }
  //    for (auto it : S) {
  //        q.push(it);
  //    }
  q.push(1);
  vis[1] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto &edge : adj[v]) {
      int u = edge.first, w = edge.second;
      if (vis[u]) {
        continue;
      }
      if (w % 2 == 0) {
        ans[u] = ans[v];
      } else {
        ans[u] = !ans[v];
      }
      q.push(u);
      vis[u] = true;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << "\n";
  }
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  int n;
  cin >> n;
  vector<int> ans(n + 10, 0);
  queue<int> q;
  //    set<int> S;
  vector<bool> vis(n + 10, false);
  vector<vector<pair<int, int>>> adj(n + 10);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].emplace_back(v, w);
    adj[v].emplace_back(u, w);
    //        if (w % 2 == 0) {
    //            ans[u] = ans[v] = 1;
    //            vis[u] = vis[v] = true;
    //            S.insert(u);
    //            S.insert(v);
    //        }
  }
  //    for (auto it : S) {
  //        q.push(it);
  //    }
  q.push(1);
  vis[1] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto &edge : adj[v]) {
      int u = edge.first, w = edge.second;
      if (vis[u]) {
        continue;
      }
      if (w % 2 == 0) {
        ans[u] = ans[v];
      } else {
        ans[u] = !ans[v];
      }
      q.push(u);
      vis[u] = true;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << "\n";
  }
}
Problem p03044 (C++)

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

vector<pair<int, int>> neighbour[10001];
int color[10001];

void dfs(int now, int prev) {
  for (auto p : neighbour[now]) {
    if (p.first != prev) {
      color[p.first] = p.second % 2 ? !color[now] : color[now];
      dfs(p.first, now);
    }
  }
}

int main() {
  int n, u, v, w;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> w;
    neighbour[u].push_back(make_pair(v, w));
    neighbour[v].push_back(make_pair(u, w));
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << color[i] << endl;
  }
  return 0;
}

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

vector<pair<int, int>> neighbour[100001];
int color[100001];

void dfs(int now, int prev) {
  for (auto p : neighbour[now]) {
    if (p.first != prev) {
      color[p.first] = p.second % 2 ? !color[now] : color[now];
      dfs(p.first, now);
    }
  }
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define rint register int
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;

struct Edge {
  int vv, next, ww;
} e[maxn];

int n, u, v, w, tot;
int head[maxn], clr[maxn];

void add(int u, int v, int w) {
  e[++tot].vv = v;
  e[tot].ww = w;
  e[tot].next = head[u];
  head[u] = tot;
}

void dfs(int x, int fa) {
  for (rint i = head[x]; i; i = e[i].next) {
    int kx = e[i].vv;
    if (fa == kx)
      continue;
    clr[kx] = clr[x] ^ (e[i].ww % 2);
    dfs(kx, x);
  }
}

int main() {
  scanf("%d", &n);
  for (rint i = 1; i < n; ++i)
    scanf("%d%d%d", &u, &v, &w), add(u, v, w), add(v, u, w);
  clr[1] = 1;
  dfs(1, 0);
  for (rint i = 1; i <= n; ++i)
    printf("%d\n", clr[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define rint register int
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;

struct Edge {
  int vv, next, ww;
} e[2 * maxn];

int n, u, v, w, tot;
int head[maxn], clr[maxn];

void add(int u, int v, int w) {
  e[++tot].vv = v;
  e[tot].ww = w;
  e[tot].next = head[u];
  head[u] = tot;
}

void dfs(int x, int fa) {
  for (rint i = head[x]; i; i = e[i].next) {
    int kx = e[i].vv;
    if (fa == kx)
      continue;
    clr[kx] = clr[x] ^ (e[i].ww % 2);
    dfs(kx, x);
  }
}

int main() {
  scanf("%d", &n);
  for (rint i = 1; i < n; ++i)
    scanf("%d%d%d", &u, &v, &w), add(u, v, w), add(v, u, w);
  clr[1] = 1;
  dfs(1, 0);
  for (rint i = 1; i <= n; ++i)
    printf("%d\n", clr[i]);
  return 0;
}
Problem p03044 (C++)

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

// vector<int> to[100005], co[100005];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> to(n), co(n);
  rep(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    cin >> a >> b >> w;
    to[a].push_back(b);
    co[a].push_back(w);
    to[b].push_back(a);
    co[b].push_back(w);
  }
  vector<int> ans(n, -1);
  queue<int> q;
  ans[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    rep(i, to[v].size()) {
      int u = to[v][i];
      int w = co[v][i];
      if (ans[u] != -1)
        continue;
      ans[u] = (ans[v] + w) % 2;
      q.push(u);
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// vector<int> to[100005], co[100005];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> to(n), co(n);
  rep(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to[a].push_back(b);
    co[a].push_back(w);
    to[b].push_back(a);
    co[b].push_back(w);
  }
  vector<int> ans(n, -1);
  queue<int> q;
  ans[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    rep(i, to[v].size()) {
      int u = to[v][i];
      int w = co[v][i];
      if (ans[u] != -1)
        continue;
      ans[u] = (ans[v] + w) % 2;
      q.push(u);
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
#define REP(i, n) for (int i = 0; i < n; i++)

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

  vector<vector<int>> a(N, vector<int>(N));
  vector<vector<int>> cost(N, vector<int>(N));
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    a[u].emplace_back(v);
    cost[u].emplace_back(w);
    a[v].emplace_back(u);
    cost[v].emplace_back(w);
  }

  vector<int> ans(N, -1);
  queue<int> q;
  ans[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < a[v].size(); i++) {
      int u = a[v][i];
      int w = cost[v][i];

      if (ans[u] != -1)
        continue;
      ans[u] = (ans[v] + w) % 2;
      q.push(u);
    }
  }

  for (auto iter : ans) {
    cout << iter << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
#define REP(i, n) for (int i = 0; i < n; i++)

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

  vector<vector<int>> a(N);
  vector<vector<int>> cost(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    a[u].emplace_back(v);
    cost[u].emplace_back(w);
    a[v].emplace_back(u);
    cost[v].emplace_back(w);
  }

  vector<int> ans(N, -1);
  queue<int> q;
  ans[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < a[v].size(); i++) {
      int u = a[v][i];
      int w = cost[v][i];

      if (ans[u] != -1)
        continue;
      ans[u] = (ans[v] + w) % 2;
      q.push(u);
    }
  }

  for (auto iter : ans) {
    cout << iter << endl;
  }
}
Problem p03044 (C++)

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

vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;

void dfs(int v, int dist) {
  ans.at(v) = dist % 2;
  if (accumulate(V.begin(), V.end(), 0) == V.size() - 1)
    return;
  for (auto g : G.at(v)) {
    if (V.at(g.first))
      continue;
    V.at(v) = 1;
    dfs(g.first, dist + g.second);
    V.at(v) = 0;
  }
}

int main() {
  int N;
  cin >> N;
  G.resize(N);
  V.resize(N);
  ans.resize(N);
  for (int i = 0, a, b, c; cin >> a >> b >> c; i++) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  V.at(0) = 1;
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}

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

vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;

void dfs(int v, int dist) {
  ans.at(v) = dist % 2;
  for (auto g : G.at(v)) {
    if (V.at(g.first))
      continue;
    V.at(v) = 1;
    dfs(g.first, dist + g.second);
    V.at(v) = 0;
  }
}

int main() {
  int N;
  cin >> N;
  G.resize(N);
  V.resize(N);
  ans.resize(N);
  for (int i = 0, a, b, c; cin >> a >> b >> c; i++) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  V.at(0) = 1;
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}
Problem p03044 (C++)

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

int N;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;

void dfs(int v, int dist) {
  V.at(v) = 1;
  ans.at(v) = dist % 2;
  if (accumulate(V.begin(), V.end(), 0L) == N)
    return;
  for (auto g : G.at(v)) {
    if (V.at(g.first))
      continue;
    dfs(g.first, dist + g.second);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  V.resize(N);
  ans.resize(N);
  for (int a, b, c; cin >> a >> b >> c;) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}

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

int N;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;

void dfs(int v, int dist) {
  V.at(v) = 1;
  ans.at(v) = dist % 2;
  // if (accumulate(V.begin(), V.end(), 0L) == N) return;
  for (auto g : G.at(v)) {
    if (V.at(g.first))
      continue;
    dfs(g.first, dist + g.second);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  V.resize(N);
  ans.resize(N);
  for (int a, b, c; cin >> a >> b >> c;) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}
Problem p03044 (C++)

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

int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
vector<int> ans;

void dfs(int v, int dist) {
  seen.at(v) = 1;
  ans.at(v) = dist % 2;
  if (accumulate(seen.begin(), seen.end(), 0) == N)
    return;
  for (auto to : G.at(v)) {
    if (!seen.at(to.first))
      dfs(to.first, dist + to.second);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  seen.resize(N);
  ans.resize(N);
  for (int a, b, c; cin >> a >> b >> c;) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}

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

int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
vector<int> ans;

void dfs(int v, int dist) {
  seen.at(v) = 1;
  ans.at(v) = dist % 2;
  // if (accumulate(seen.begin(), seen.end(), 0) == N) return;
  for (auto to : G.at(v)) {
    if (!seen.at(to.first))
      dfs(to.first, dist + to.second);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  seen.resize(N);
  ans.resize(N);
  for (int a, b, c; cin >> a >> b >> c;) {
    G.at(--a).push_back({--b, c % 2});
    G.at(b).push_back({a, c % 2});
  }
  dfs(0, 0);
  for (auto a : ans)
    cout << a << "\n";
}
Problem p03044 (C++)

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

vector<vector<pair<int, int>>> tree;
vector<int> col;

void dfs(int current, int c) {
  col.at(current) = c;
  for (pair<int, int> e : tree.at(current)) {
    int to = e.first;
    if (col.at(to) != -1)
      continue;
    int dist = e.second;
    if (dist % 2 == 0) {
      dfs(to, c);
    } else {
      dfs(to, 1 - c);
    }
  }
}

int main() {
  int N;
  cin >> N;
  tree.resize(N);
  col.resize(N, -1);

  for (int i = 0; i < N; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;

    tree.at(u).push_back(make_pair(v, w));
    tree.at(v).push_back(make_pair(u, w));
  }

  dfs(0, 0);

  for (int i = 0; i < N; i++) {
    cout << col.at(i) << endl;
  }

  return 0;
}

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

vector<vector<pair<int, int>>> tree;
vector<int> col;

void dfs(int current, int c) {
  col.at(current) = c;
  for (pair<int, int> e : tree.at(current)) {
    int to = e.first;
    if (col.at(to) != -1)
      continue;
    int dist = e.second;
    if (dist % 2 == 0) {
      dfs(to, c);
    } else {
      dfs(to, 1 - c);
    }
  }
}

int main() {
  int N;
  cin >> N;
  tree.resize(N);
  col.resize(N, -1);

  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;

    tree.at(u).push_back(make_pair(v, w));
    tree.at(v).push_back(make_pair(u, w));
  }

  dfs(0, 0);

  for (int i = 0; i < N; i++) {
    cout << col.at(i) << endl;
  }

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

vector<vector<pair<ll, ll>>> g;
vector<ll> seen;
void dfs(int v, int p = -1, int c = 1) {
  seen[v] = c;
  for (auto x : g[v]) {
    if (x.first == p)
      continue;
    if (x.second % 2 == 0)
      dfs(x.first, v, c);
    else
      dfs(x.first, v, 1 - c);
  }
}
int main() {
  int n;
  cin >> n;
  g.resize(n);
  seen.assign(n, -1);
  REP(i, n - 1) {
    ll a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    g[a].push_back({b, w});
    g[b].push_back({a, w});
  }
  REP(i, n) dfs(i);
  REP(i, n) cout << seen[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

vector<vector<pair<ll, ll>>> g;
vector<ll> seen;
void dfs(int v, int p = -1, int c = 1) {
  seen[v] = c;
  for (auto x : g[v]) {
    if (x.first == p)
      continue;
    if (x.second % 2 == 0)
      dfs(x.first, v, c);
    else
      dfs(x.first, v, 1 - c);
  }
}
int main() {
  int n;
  cin >> n;
  g.resize(n);
  seen.assign(n, -1);
  REP(i, n - 1) {
    ll a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    g[a].push_back({b, w});
    g[b].push_back({a, w});
  }
  dfs(0);
  REP(i, n) cout << seen[i] << endl;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct pp {
  ll fi;
  ll se;
  ll th;
};
ll n, a, b, c, d[100000];
queue<pp> q;
vector<P> v[100000];
int main(void) {
  cin >> n;
  d[0] = 1;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    a--, b--;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  for (auto x : v[0]) {
    struct pp P1;
    P1.fi = x.first, P1.se = x.second, P1.th = 0LL;
    q.push(P1);
  }
  while (!q.empty()) {
    struct pp p = q.front();
    q.pop();
    if (d[p.fi] == 0) {
      if (p.se % 2 == 0)
        d[p.fi] = d[p.th];
      else
        d[p.fi] = -d[p.th];
      for (auto y : v[p.fi]) {
        struct pp P2;
        P2.fi = y.first, P2.se = y.second, P2.th = p.fi;
        q.push(P2);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << (d[i] == 1) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct pp {
  ll fi;
  ll se;
  ll th;
};
ll n, a, b, c, d[100000];
queue<pp> q;
vector<P> v[100000];
int main(void) {
  cin >> n;
  d[0] = 1;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b >> c;
    a--, b--;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  for (auto x : v[0]) {
    struct pp P1;
    P1.fi = x.first, P1.se = x.second, P1.th = 0LL;
    q.push(P1);
  }
  while (!q.empty()) {
    struct pp p = q.front();
    q.pop();
    if (d[p.fi] == 0) {
      if (p.se % 2 == 0)
        d[p.fi] = d[p.th];
      else
        d[p.fi] = -d[p.th];
      for (auto y : v[p.fi]) {
        struct pp P2;
        P2.fi = y.first, P2.se = y.second, P2.th = p.fi;
        q.push(P2);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << (d[i] == 1) << endl;
  }
}
Problem p03044 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

struct Edge {
  int to;     // 辺の行き先
  int weight; // 辺の重み
  Edge(int t, int w) : to(t), weight(w) {}
};
using vve = vector<vector<Edge>>;

vi ans;
void dfs(vve graph, int v) {
  for (auto n : graph[v]) {
    int u = n.to;
    int w = n.weight;
    if (ans[u] != -1)
      continue;
    ans[u] = (ans[v] + w) % 2;
    dfs(graph, u);
  }
}

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

  int N;
  cin >> N;
  vve graph(N);
  REP(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    graph[u].push_back(Edge(v, w));
    graph[v].push_back(Edge(u, w));
  }
  ans.assign(N, -1);
  ans[0] = 0;
  dfs(graph, 0);

  for (auto n : ans) {
    cout << n << endl;
  }

  return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

struct Edge {
  int to;     // 辺の行き先
  int weight; // 辺の重み
  Edge(int t, int w) : to(t), weight(w) {}
};
using vve = vector<vector<Edge>>;

vi ans;
void dfs(vve &graph, int v) {
  for (auto n : graph[v]) {
    int u = n.to;
    int w = n.weight;
    if (ans[u] != -1)
      continue;
    ans[u] = (ans[v] + w) % 2;
    dfs(graph, u);
  }
}

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

  int N;
  cin >> N;
  vve graph(N);
  REP(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    graph[u].push_back(Edge(v, w));
    graph[v].push_back(Edge(u, w));
  }
  ans.assign(N, -1);
  ans[0] = 0;
  dfs(graph, 0);

  for (auto n : ans) {
    cout << n << endl;
  }

  return 0;
}
Problem p03044 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<pair<long, long>>> e(
      N + 1, vector<pair<long, long>>{}); // dest, weight
  for (int i = 0; i < N - 1; ++i) {
    long u, v, w;
    cin >> u >> v >> w;
    e[u].push_back({v, w});
    e[v].push_back({u, w});
  }

  vector<int> c(N);
  int count = 0;
  function<void(int, int, int, int)> solve =
      [&](int current, int prev_node, int prev_color, int prev_dist) {
        c[current] = (prev_color + prev_dist) % 2;
        ++count;
        if (count >= N)
          return;
        for (auto p : e[current]) {
          if (p.first != prev_node) {
            solve(p.first, current, c[current], p.second);
          }
        }
      };
  solve(1, 0, 0, 0);
  for (int i = 1; i <= N; ++i) {
    cout << c[i] << endl;
  }
}


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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<pair<long, long>>> e(
      N + 1, vector<pair<long, long>>{}); // dest, weight
  for (int i = 0; i < N - 1; ++i) {
    long u, v, w;
    cin >> u >> v >> w;
    e[u].push_back({v, w});
    e[v].push_back({u, w});
  }

  vector<int> c(N + 1);
  int count = 0;
  function<void(int, int, int, int)> solve =
      [&](int current, int prev_node, int prev_color, int prev_dist) {
        c[current] = (prev_color + prev_dist) % 2;
        ++count;
        if (count >= N)
          return;
        for (auto p : e[current]) {
          if (p.first != prev_node) {
            solve(p.first, current, c[current], p.second);
          }
        }
      };
  solve(1, 0, 0, 0);
  for (int i = 1; i <= N; ++i) {
    cout << c[i] << endl;
  }
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;
vector<pair<int, int>> G[10000];
/*
辺に属性がある場合
struct edge{int to,cost;};
vector<edge> G[10000];
*/
long long int co[1000000], t[1000000];
void Representing_graph(long long int s, long long int t, long long int c) {
  G[s].push_back(make_pair(t, c));
  G[t].push_back(make_pair(s, c));
}

void dfs(long long int n, long long int r) {

  t[n] = 1;
  for (long long int i = 0; i < G[n].size(); i++) {
    long long int len = G[n][i].second, po = G[n][i].first;
    if ((r + len) % 2 == 0) {
      co[po] = 1;
    }
    if (t[po] == 0)
      dfs(po, r + len);
  }
  return;
};

int main() {
  long long int n, a, b, c;
  cin >> n;
  vector<pair<long long int, long long int>> vec(n);

  for (long long int i = 0; i < n - 1; i++) {
    cin >> a >> b >> c;
    Representing_graph(a - 1, b - 1, c);
  }
  co[0] = 1;
  dfs(0, 0);
  for (long long int i = 0; i < n; i++) {
    cout << co[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;
vector<pair<int, int>> G[1000000];
/*
辺に属性がある場合
struct edge{int to,cost;};
vector<edge> G[10000];
*/
long long int co[1000000], t[1000000];
void Representing_graph(long long int s, long long int t, long long int c) {
  G[s].push_back(make_pair(t, c));
  G[t].push_back(make_pair(s, c));
}

void dfs(long long int n, long long int r) {

  t[n] = 1;
  for (long long int i = 0; i < G[n].size(); i++) {
    long long int len = G[n][i].second, po = G[n][i].first;
    if ((r + len) % 2 == 0) {
      co[po] = 1;
    }
    if (t[po] == 0)
      dfs(po, r + len);
  }
  return;
};

int main() {
  long long int n, a, b, c;
  cin >> n;
  vector<pair<long long int, long long int>> vec(n);

  for (long long int i = 0; i < n - 1; i++) {
    cin >> a >> b >> c;
    Representing_graph(a - 1, b - 1, c);
  }
  co[0] = 1;
  dfs(0, 0);
  for (long long int i = 0; i < n; i++) {
    cout << co[i] << endl;
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 0x7fffffff;
const ll LINF = 0x7fffffffffffffff;
const int maxn = 1e5;
const int modn = 1e9 + 7;

struct node {
  int p, w;
};
vector<node> tree[maxn + 10];
int color[maxn + 10];

void dfs(int o) {
  int tmp = color[o];
  for (auto it = tree[o].begin(); it != tree[o].end(); ++it) {
    if (!color[(*it).p]) {
      if (!((*it).w % 2))
        color[(*it).p] = tmp;
      else
        color[(*it).p] = 3 - tmp;
      dfs((*it).p);
    }
  }
}

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

  int n, u, v, w;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v >> w;
    tree[u].push_back({v, w});
    tree[w].push_back({u, w});
  }
  color[1] = 1;
  dfs(1);
  for (int i = 1; i <= n; ++i) {
    cout << color[i] % 2 << "\n";
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 0x7fffffff;
const ll LINF = 0x7fffffffffffffff;
const int maxn = 1e5;
const int modn = 1e9 + 7;

struct node {
  int p, w;
};
vector<node> tree[maxn + 10];
int color[maxn + 10];

void dfs(int o) {
  int tmp = color[o];
  for (auto it = tree[o].begin(); it != tree[o].end(); ++it) {
    if (!color[(*it).p]) {
      if (!((*it).w % 2))
        color[(*it).p] = tmp;
      else
        color[(*it).p] = 3 - tmp;
      dfs((*it).p);
    }
  }
}

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

  int n, u, v, w;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v >> w;
    tree[u].push_back({v, w});
    tree[v].push_back({u, w});
  }
  color[1] = 1;
  dfs(1);
  for (int i = 1; i <= n; ++i) {
    cout << color[i] % 2 << "\n";
  }

  return 0;
}
Problem p03044 (C++)

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

int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> G[100000];
  vector<long long> dis(100000, 0);
  for (int i = 0; i < n - 1; i++) {
    int f, g, w;
    scanf("%d %d %d", &f, &g, &w);
    G[f].push_back({g, w});
    G[g].push_back({f, w});
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    for (auto to : G[node]) {
      if (dis[to.first] != 0)
        continue;
      dis[to.first] = dis[node] + to.second;
      q.push(to.first);
    }
  }
  for (int i = 0; i < n; i++)
    cout << dis[i] % 2 << endl;
  return 0;
}

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

int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> G[100000];
  vector<long long> dis(100000, 0);
  for (int i = 0; i < n - 1; i++) {
    int f, g, w;
    scanf("%d %d %d", &f, &g, &w);
    f--;
    g--;
    G[f].push_back({g, w});
    G[g].push_back({f, w});
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    for (auto to : G[node]) {
      if (dis[to.first] != 0)
        continue;
      dis[to.first] = dis[node] + to.second;
      q.push(to.first);
    }
  }
  for (int i = 0; i < n; i++)
    cout << dis[i] % 2 << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;

vector<ll> col(100005);
vector<vector<P>> g(100005);

int dfs(int t, int p, int d) {
  //  cout<<"t"<<t<<"p"<<p<<"d"<<d<<endl;
  for (P T : g[t]) {
    int to = T.first;
    if (to == p)
      continue;
    int dnow = d + T.second;
    if (dnow % 2 == 0)
      col[to] = 0;
    else
      col[to] = 1;
    dfs(to, t, dnow);
  }
}

int main() {
  ll n;
  cin >> n;
  rep(i, n - 1) {
    ll u1, v1, w1;
    cin >> u1 >> v1 >> w1;
    u1--;
    v1--;
    g[u1].push_back(make_pair(v1, w1));
    g[v1].push_back(make_pair(u1, w1));
  }
  dfs(0, -1, 0);
  rep(i, n) cout << col[i] << endl;
}


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;

vector<ll> col(100005);
vector<vector<P>> g(100005);

void dfs(int t, int p, int d) {
  //  cout<<"t"<<t<<"p"<<p<<"d"<<d<<endl;
  for (P T : g[t]) {
    int to = T.first;
    if (to == p)
      continue;
    int dnow = d + T.second;
    if (dnow % 2 == 0)
      col[to] = 0;
    else
      col[to] = 1;
    dfs(to, t, dnow);
  }
}

int main() {
  ll n;
  cin >> n;
  rep(i, n - 1) {
    ll u1, v1, w1;
    cin >> u1 >> v1 >> w1;
    u1--;
    v1--;
    g[u1].push_back(make_pair(v1, w1));
    g[v1].push_back(make_pair(u1, w1));
  }
  dfs(0, -1, 0);
  rep(i, n) cout << col[i] << endl;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  vector<vi> len(n, vi(n, -1));
  vector<vi> g(n);
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    len[u - 1][v - 1] = w;
    len[v - 1][u - 1] = w;
    g[u - 1].push_back(v - 1);
    g[v - 1].push_back(u - 1);
  }
  vi ans(n, -1);
  ans[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (auto j : g[i]) {
      int w = len[i][j];
      if (w == -1 || ans[j] != -1)
        continue;
      if (w % 2)
        ans[j] = !ans[i];
      else
        ans[j] = ans[i];
      q.push(j);
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  map<int, map<int, int>> len;
  vector<vi> g(n);
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    len[u - 1][v - 1] = w;
    len[v - 1][u - 1] = w;
    g[u - 1].push_back(v - 1);
    g[v - 1].push_back(u - 1);
  }
  vi ans(n, -1);
  ans[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (auto j : g[i]) {
      int w = len[i][j];
      if (w == -1 || ans[j] != -1)
        continue;
      if (w % 2)
        ans[j] = !ans[i];
      else
        ans[j] = ans[i];
      q.push(j);
    }
  }
  rep(i, n) cout << ans[i] << endl;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<char>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;

int main() {

  int n;
  cin >> n;
  Graph to(n), co(n);
  rep(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to.at(a).push_back(b);
    to.at(b).push_back(a);
    co.at(a).push_back(w);
    co.at(b).push_back(w);
  }

  vector<int> ans(n, -1);
  queue<int> q;
  ans.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    rep(i, to.at(v).size()) {
      int u = to.at(v).at(i);
      int w = co.at(v).at(i);
      if (ans.at(u) != -1)
        continue;
      ans.at(u) = (ans.at(v) + w) % 2;
      q.push(u);
    }
  }
  rep(i, n) cout << ans.at(i) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;

int main() {

  int n;
  cin >> n;
  Graph to(n), co(n);
  rep(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    to.at(a).push_back(b);
    to.at(b).push_back(a);
    co.at(a).push_back(w);
    co.at(b).push_back(w);
  }

  vector<int> ans(n, -1);
  queue<int> q;
  ans.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    rep(i, to.at(v).size()) {
      int u = to.at(v).at(i);
      int w = co.at(v).at(i);
      if (ans.at(u) != -1)
        continue;
      ans.at(u) = (ans.at(v) + w) % 2;
      q.push(u);
    }
  }
  rep(i, n) cout << ans.at(i) << endl;

  return 0;
}
Problem p03044 (C++)

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

using namespace std;
void dfs(const vector<vector<long>> &graph,
         const map<pair<long, long>, long> &dist, vector<int> &color, long now,
         long from, long res) {

  color[now] = res % 2;

  for (auto e : graph[now]) {
    if (e == from)
      continue;

    dfs(graph, dist, color, e, now, res + dist.at(make_pair(now, e)));
  }
}

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

  vector<vector<long>> graph(N);
  map<pair<long, long>, long> dist;
  vector<int> color(N);

  for (int i = 0; i < N; ++i) { // 入力。
    long a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
    dist.insert(make_pair(make_pair(a, b), d));
    dist.insert(make_pair(make_pair(b, a), d));
  }

  // 頂点0を根にする
  color[0] = 0;

  dfs(graph, dist, color, 0, -1, 0);

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

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

using namespace std;
void dfs(const vector<vector<long>> &graph,
         const map<pair<long, long>, long> &dist, vector<int> &color, long now,
         long from, long res) {

  color[now] = res % 2;

  for (auto e : graph[now]) {
    if (e == from)
      continue;

    dfs(graph, dist, color, e, now, res + dist.at(make_pair(now, e)));
  }
}

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

  vector<vector<long>> graph(N);
  map<pair<long, long>, long> dist;
  vector<int> color(N);

  for (int i = 0; i < N - 1; ++i) { // 入力。
    long a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
    dist.insert(make_pair(make_pair(a, b), d));
    dist.insert(make_pair(make_pair(b, a), d));
  }

  // 頂点0を根にする
  color[0] = 0;

  dfs(graph, dist, color, 0, -1, 0);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100010
struct NODE {
  int w;
  int e;
  int next;
} edge[MAXN];
int cnt = 1;
int head[MAXN];
int color[MAXN];
void add(int u, int v, int w) {
  edge[cnt].w = w;
  edge[cnt].e = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
  edge[cnt].w = w;
  edge[cnt].e = u;
  edge[cnt].next = head[v];
  head[v] = cnt++;
}
bool vis[MAXN];
void dfs(int i, int Color) {
  color[i] = Color;
  int curedge = head[i];
  while (curedge != 0) {
    if (vis[edge[curedge].e]) {
      curedge = edge[curedge].next;
      continue;
    }
    vis[edge[curedge].e] = true;
    int w = edge[curedge].w;
    if (w % 2)
      dfs(edge[curedge].e, Color ^ 1);
    else
      dfs(edge[curedge].e, Color);
    curedge = edge[curedge].next;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    add(a, b, c);
  }
  vis[1] = true;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << color[i] << ' ';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100010
struct NODE {
  int w;
  int e;
  int next;
} edge[MAXN * 2];
int cnt = 1;
int head[MAXN];
int color[MAXN];
void add(int u, int v, int w) {
  edge[cnt].w = w;
  edge[cnt].e = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
  edge[cnt].w = w;
  edge[cnt].e = u;
  edge[cnt].next = head[v];
  head[v] = cnt++;
}
bool vis[MAXN];
void dfs(int i, int Color) {
  color[i] = Color;
  int curedge = head[i];
  while (curedge != 0) {
    if (vis[edge[curedge].e]) {
      curedge = edge[curedge].next;
      continue;
    }
    vis[edge[curedge].e] = true;
    int w = edge[curedge].w;
    if (w % 2)
      dfs(edge[curedge].e, Color ^ 1);
    else
      dfs(edge[curedge].e, Color);
    curedge = edge[curedge].next;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    add(a, b, c);
  }
  vis[1] = true;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << color[i] << ' ';
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;

int N;
int color[100001];
vector<pair<int, int>> G[10000];
void hukasa(int i) {
  for (int j = 0; j < G[i].size(); j++) {
    if (color[G[i][j].first] == 0) {
      color[G[i][j].first] = color[i] + G[i][j].second;
      hukasa(G[i][j].first);
    }
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s - 1].push_back(pair<int, int>(t - 1, w));
    G[t - 1].push_back(pair<int, int>(s - 1, w));
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      color[i] = 1;
      hukasa(i);
    }
  }

  for (int i = 0; i < N; i++) {
    if (color[i] % 2 == 0) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;

int N;
int color[100005];
vector<pair<int, int>> G[100005];
void hukasa(int i) {
  for (int j = 0; j < G[i].size(); j++) {
    if (color[G[i][j].first] == 0) {
      color[G[i][j].first] = color[i] + G[i][j].second;
      hukasa(G[i][j].first);
    }
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s - 1].push_back(pair<int, int>(t - 1, w));
    G[t - 1].push_back(pair<int, int>(s - 1, w));
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      color[i] = 1;
      hukasa(i);
    }
  }

  for (int i = 0; i < N; i++) {
    if (color[i] % 2 == 0) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}
Problem p03044 (C++)

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

const int MOD = (int)1e9 + 7;

void dfs(vector<vector<pair<int, int>>> &graph, vector<int> &ans, int now,
         int parent, int color) {
  ans.at(now) = color;

  for (int i = 0; i < graph.at(now).size(); i++) {
    int next = graph.at(now).at(i).first;
    if (next == parent)
      continue;
    if (graph.at(now).at(i).second == 0)
      dfs(graph, ans, next, now, color);
    else
      dfs(graph, ans, next, now, 1 - color);
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> graph(N);
  for (int i = 0; i < N; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    graph.at(u).push_back(make_pair(v, w));
    graph.at(v).push_back(make_pair(u, w));
  }

  vector<int> ans(N, -1);
  dfs(graph, ans, 0, -1, 0);
  for (int i = 0; i < N; i++) {
    cout << ans.at(i) << endl;
  }

  return 0;
}


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

const int MOD = (int)1e9 + 7;

void dfs(vector<vector<pair<int, int>>> &graph, vector<int> &ans, int now,
         int parent, int color) {
  ans.at(now) = color;

  for (int i = 0; i < graph.at(now).size(); i++) {
    int next = graph.at(now).at(i).first;
    if (next == parent)
      continue;
    if (graph.at(now).at(i).second == 0)
      dfs(graph, ans, next, now, color);
    else
      dfs(graph, ans, next, now, 1 - color);
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> graph(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    graph.at(u).push_back(make_pair(v, w));
    graph.at(v).push_back(make_pair(u, w));
  }

  vector<int> ans(N, -1);
  dfs(graph, ans, 0, -1, 0);
  for (int i = 0; i < N; i++) {
    cout << ans.at(i) << endl;
  }

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)

class edge_t {
public:
  edge_t(int vv, int ww) : v(vv), w(ww){};

public:
  int v;
  int w;
};

int N, a[111111];

std::vector<std::vector<edge_t>> G;

bool visit[111111];

void dfs(int i, int j) {
  for (const auto &e : G[i]) {
    if (visit[e.v])
      continue;

    visit[e.v] = true;

    int jj = j ^ (e.w & 1);

    dfs(e.v, a[e.v] = jj);
  }
}

int main(int argc, char **argv) {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  std::cin >> N;

  G.resize(N);

  int u, v, w;

  for (int i = 0; i < N; i++) {
    std::cin >> u >> v >> w;

    u--;
    v--;

    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }

  memset(a, -1, sizeof(a));

  a[0] = 0;

  memset(visit, 0, sizeof(visit));

  dfs(0, 0);

  for (int i = 0; i < N; i++)
    std::cout << a[i] << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)

class edge_t {
public:
  edge_t(int vv, int ww) : v(vv), w(ww){};

public:
  int v;
  int w;
};

int N, a[111111];

std::vector<std::vector<edge_t>> G;

bool visit[111111];

void dfs(int i, int j) {
  for (const auto &e : G[i]) {
    if (visit[e.v])
      continue;

    visit[e.v] = true;

    int jj = j ^ (e.w & 1);

    dfs(e.v, a[e.v] = jj);
  }
}

int main(int argc, char **argv) {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  std::cin >> N;

  G.resize(N);

  int u, v, w;

  for (int i = 0; i < N - 1; i++) {
    std::cin >> u >> v >> w;

    u--;
    v--;

    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }

  memset(a, -1, sizeof(a));

  a[0] = 0;

  memset(visit, 0, sizeof(visit));

  dfs(0, 0);

  for (int i = 0; i < N; i++)
    std::cout << a[i] << std::endl;

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
// Ryo Kamoi
#define DEBUG

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)

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

int INF = 1000000000;

struct edge {
  int to;
  int len;
};

int n;
int col[10010];
vector<edge> graph[10010];

int main() {
  cin >> n;

  REP(i, n) { col[i] = -1; }

  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    edge e1;
    e1.to = v - 1;
    e1.len = w;
    graph[u - 1].push_back(e1);

    edge e2;
    e2.to = u - 1;
    e2.len = w;
    graph[v - 1].push_back(e2);
  }

  queue<int> que;
  que.push(0);
  col[0] = 0;

  while (!que.empty()) {
    int vertex = que.front();
    que.pop();
    REP(i, graph[vertex].size()) {
      int next_v = graph[vertex][i].to;
      if (col[next_v] >= 0)
        continue;

      if (graph[vertex][i].len % 2 == 1) {
        col[next_v] = (col[vertex] + 1) % 2;
      } else {
        col[next_v] = col[vertex];
      }

      que.push(next_v);
    }
  }

  REP(i, n) { cout << col[i] << endl; }
}


Fixed submission:
// Ryo Kamoi
#define DEBUG

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)

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

int INF = 1000000000;

struct edge {
  int to;
  int len;
};

int n;
int col[100010];
vector<edge> graph[100010];

int main() {
  cin >> n;

  REP(i, n) { col[i] = -1; }

  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    edge e1;
    e1.to = v - 1;
    e1.len = w;
    graph[u - 1].push_back(e1);

    edge e2;
    e2.to = u - 1;
    e2.len = w;
    graph[v - 1].push_back(e2);
  }

  queue<int> que;
  que.push(0);
  col[0] = 0;

  while (!que.empty()) {
    int vertex = que.front();
    que.pop();
    REP(i, graph[vertex].size()) {
      int next_v = graph[vertex][i].to;
      if (col[next_v] >= 0)
        continue;

      if (graph[vertex][i].len % 2 == 1) {
        col[next_v] = (col[vertex] + 1) % 2;
      } else {
        col[next_v] = col[vertex];
      }

      que.push(next_v);
    }
  }

  REP(i, n) { cout << col[i] << endl; }
}