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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef tuple<ll, int, int> tup;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;
bool visited[100005];
vp g[100005];
int colors[100005];

void dfs(int s) {
  visited[s] = true;
  for (int i = 0; i < int(g[s].size()); i++) {
    int to = g[s][i].first;
    int len = g[s][i].second;
    if (visited[to])
      continue;
    else {
      if (len % 2 == 0) {
        colors[to] = colors[s];
        dfs(to);
      } else {
        colors[to] = (colors[s] + 1) % 2;
        dfs(to);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int N;
  memset(visited, false, sizeof(visited));
  colors[1] = 0;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back(pii(v, w));
    g[v].push_back(pii(w, v));
  }
  dfs(1);
  for (int i = 1; i <= N; i++) {
    cout << colors[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef tuple<ll, int, int> tup;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;
bool visited[100005];
vp g[100005];
int colors[100005];

void dfs(int s) {
  visited[s] = true;
  for (int i = 0; i < int(g[s].size()); i++) {
    int to = g[s][i].first;
    int len = g[s][i].second;
    if (visited[to])
      continue;
    else {
      if (len % 2 == 0) {
        colors[to] = colors[s];
        dfs(to);
      } else {
        colors[to] = (colors[s] + 1) % 2;
        dfs(to);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int N;
  memset(visited, false, sizeof(visited));
  colors[1] = 0;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back(pii(v, w));
    g[v].push_back(pii(u, w));
  }
  dfs(1);
  for (int i = 1; i <= N; i++) {
    cout << colors[i] << endl;
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define FORe(i, k, n) for (int i = (k); i <= (n); i++)
#define FORr(i, k, n) for (int i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (int i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

vector<vector<int>> to(100000);
map<P, int> weight;
vector<int> color(10000);

void dfs(int u, int p = -1, int len = 0) {
  if (len)
    color[u] = 1;

  for (int v : to[u]) {
    if (v == p)
      continue;
    int w = weight[P(u, v)];
    dfs(v, u, len xor w);
  }
}

int main(void) {
  int n;
  cin >> n;
  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
    weight[P(u, v)] = w;
    weight[P(v, u)] = w;
  }

  dfs(0);
  REP(i, n) cout << color[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define FORe(i, k, n) for (int i = (k); i <= (n); i++)
#define FORr(i, k, n) for (int i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (int i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

vector<vector<int>> to(100000);
map<P, int> weight;
vector<int> color(100000);

void dfs(int u, int p = -1, int len = 0) {
  if (len)
    color[u] = 1;

  for (int v : to[u]) {
    if (v == p)
      continue;
    int w = weight[P(u, v)];
    dfs(v, u, len xor w);
  }
}

int main(void) {
  int n;
  cin >> n;
  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    w %= 2;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
    weight[P(u, v)] = w;
    weight[P(v, u)] = w;
  }

  dfs(0);
  REP(i, n) cout << color[i] << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;

typedef pair<int, int> point_t;
vector<vector<pair<int, int>>> z(100010);
int ans[100010];
bool used[100010];
void dfs(int v, int w) {
  if (w % 2 == 0) {
    ans[v] = 1;
  } else {
    ans[v] = 0;
  }
  for (auto t : z[v]) {
    if (!used[t.first]) {
      used[t.first] = true;
      dfs(t.first, t.second + w);
    }
  }
}

int main() {
  int N, i;
  cin >> N;
  vector<int> x(N);
  rep(i, N) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    z[u].push_back(make_pair(v, w));
    z[v].push_back(make_pair(u, w));
  }
  dfs(0, 0);
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;

typedef pair<int, int> point_t;
vector<vector<pair<int, int>>> z(100010);
int ans[100010];
bool used[100010];
void dfs(int v, int w) {
  if (w % 2 == 0) {
    ans[v] = 1;
  } else {
    ans[v] = 0;
  }
  for (auto t : z[v]) {
    if (!used[t.first]) {
      used[t.first] = true;
      dfs(t.first, t.second + w);
    }
  }
}

int main() {
  int N, i;
  cin >> N;
  vector<int> x(N);
  rep(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    z[u].push_back(make_pair(v, w));
    z[v].push_back(make_pair(u, w));
  }
  dfs(0, 0);
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}
Problem p03044 (C++)

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

#define ln '\n'
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> VP;
typedef vector<PL> VPL;
typedef vector<string> VS;

VI ans;
void dfs(int i, bool is_black, vector<vector<P>> &edge) {
  if (ans[i] != -1)
    return;
  ans[i] = is_black ? 0 : 1;
  for (auto &adj : edge[i]) {
    int cost = adj.first;
    int to = adj.second;
    bool next_is_black = cost % 2 == 0 ? is_black : !is_black;
    dfs(to, next_is_black, edge);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ans.resize(n, -1);         // init
  vector<vector<P>> edge(n); // first: cost, second: to
  REP(i, n) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    edge[u].push_back(P(w, v));
    edge[v].push_back(P(w, u));
  }

  dfs(0, true, edge);

  REP(i, n) cout << ans[i] << ln;
  return 0;
}


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

#define ln '\n'
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> VP;
typedef vector<PL> VPL;
typedef vector<string> VS;

VI ans;
void dfs(int i, bool is_black, vector<vector<P>> &edge) {
  if (ans[i] != -1)
    return;
  ans[i] = is_black ? 0 : 1;
  for (auto &adj : edge[i]) {
    int cost = adj.first;
    int to = adj.second;
    bool next_is_black = cost % 2 == 0 ? is_black : !is_black;
    dfs(to, next_is_black, edge);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ans.resize(n, -1);         // init
  vector<vector<P>> edge(n); // first: cost, second: to
  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    edge[u].push_back(P(w, v));
    edge[v].push_back(P(w, u));
  }

  dfs(0, true, edge);

  REP(i, n) cout << ans[i] << ln;
  return 0;
}
Problem p03044 (C++)

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

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int n;
  cin >> n;
  vector<int> color(n, -1);
  color[0] = 0;
  Graph g(n, vector<int>(n));
  Graph cost(n, vector<int>(n));
  for (int i = 0; i < n - 1; ++i) {
    int a, b, l;
    cin >> a >> b >> l;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    cost[a].push_back(l);
    cost[b].push_back(l);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); ++i) {
      int u = g[v][i];
      int w = cost[v][i];
      if (color[u] != -1)
        continue;
      color[u] = (color[v] + w) % 2;
      q.push(u);
    }
  }
  for (auto ans : color) {
    cout << ans << endl;
  }
}

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

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int n;
  cin >> n;
  vector<int> color(n, -1);
  color[0] = 0;
  Graph g(n), cost(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b, l;
    cin >> a >> b >> l;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    cost[a].push_back(l);
    cost[b].push_back(l);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); ++i) {
      int u = g[v][i];
      int w = cost[v][i];
      if (color[u] != -1)
        continue;
      color[u] = (color[v] + w) % 2;
      q.push(u);
    }
  }
  for (auto ans : color) {
    cout << ans << endl;
  }
}
Problem p03044 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve(string):
    n, *uvw = map(int, string.split())
    uvw = list(zip(uvw[::3], uvw[1::3], uvw[2::3]))
    whites = set([1])
    blacks = set([])
    while len(uvw) > 0:
        tmp = []
        for i in range(len(uvw)):
            t = uvw.pop()
            if t[0] in whites:
                if t[2] % 2 == 0:
                    whites.add(t[1])
                else:
                    blacks.add(t[1])
            elif t[1] in whites:
                if t[2] % 2 == 0:
                    whites.add(t[0])
                else:
                    blacks.add(t[0])
            elif t[0] in blacks:
                if t[2] % 2 == 0:
                    blacks.add(t[1])
                else:
                    whites.add(t[1])
            elif t[1] in blacks:
                if t[2] % 2 == 0:
                    blacks.add(t[0])
                else:
                    whites.add(t[0])
            else:
                tmp.append(t)
        uvw = tmp
    ans = ["0"] * n
    for i in range(n):
        if i + 1 in blacks:
            ans[i] = "1"

    return "\n".join(ans)


if __name__ == "__main__":
    n = int(input())
    print(solve("{}\n".format(n) + "\n".join([input() for _ in range(n - 1)])))


Fixed submission:
N = int(input())
p = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    p[u].append((v, w))
    p[v].append((u, w))
s = [(1, 0)]
l = [-1] * N
while s:
    a, w = s.pop()
    l[a - 1] = w % 2
    for b, c in p[a]:
        if l[b - 1] == -1:
            s.append((b, w + c))
for i in l:
    print(i)
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
en = [[] for _ in range(N)]
on = [[] for _ in range(N)]
for i in range(N - 1):
    u, v, w = map(int, input().split())
    if w % 2:
        on[u - 1].append(v - 1)
        on[v - 1].append(u - 1)
    else:
        en[u - 1].append(v - 1)
        en[v - 1].append(u - 1)

color = [None] * N


def dfs(node, parent, col):
    if color[node] is not None:
        return
    color[node] = col
    for k in en[node]:
        if k != parent:
            dfs(k, node, col)
    for k in on[node]:
        if k != parent:
            dfs(k, node, (col + 1) % 2)


dfs(0, -1, 0)
print(*color, sep="\n")


Fixed submission:
import sys

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

color = [None] * N


def dfs(node, parent, col):
    if color[node] is not None:
        return
    color[node] = col
    for k in en[node]:
        if k != parent:
            dfs(k, node, col)
    for k in on[node]:
        if k != parent:
            dfs(k, node, (col + 1) % 2)


dfs(0, -1, 0)
print(*color, sep="\n")
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
N = int(input())

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

ans = [None for _ in range(N)]


def dfs(v, p, c):
    ans[v - 1] = c
    for next_v, distance in neighbors[v]:
        if next_v == p:
            continue
        next_c = c if distance % 2 == 0 else 1 - c
        dfs(next_v, v, next_c)


dfs(1, -1, 0)
for e in ans:
    print(e)


Fixed submission:
import sys

sys.setrecursionlimit(200000)

N = int(input())

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

ans = [None for _ in range(N)]


def dfs(v, p, c):
    ans[v - 1] = c
    for next_v, distance in neighbors[v]:
        if next_v == p:
            continue
        next_c = c if distance % 2 == 0 else 1 - c
        dfs(next_v, v, next_c)


dfs(1, -1, 0)
for e in ans:
    print(e)
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import defaultdict

input = sys.stdin.readline

N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append((v - 1, w))
    G[v - 1].append((u - 1, w))
res = [None] * N


def dfs(v, c):
    res[v] = c
    for u, w in G[v]:
        if res[u] is not None:
            continue
        if w % 2 == 0:
            dfs(u, c)
        else:
            dfs(u, 1 - c)


dfs(0, 0)
for x in res:
    print(x)


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(100000)
input = sys.stdin.readline

N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append((v - 1, w))
    G[v - 1].append((u - 1, w))
res = [None] * N


def dfs(v, c):
    res[v] = c
    for u, w in G[v]:
        if res[u] is not None:
            continue
        if w % 2 == 0:
            dfs(u, c)
        else:
            dfs(u, 1 - c)


dfs(0, 0)
for x in res:
    print(x)
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
def dfs(v):
    for u, w in e[v]:
        if c[u] != -1:
            continue
        c[u] = (c[v] + w) % 2
        dfs(u)


n = int(input())
e = tuple(set() for _ in range(n))
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    e[u].add((v, w))
    e[v].add((u, w))

c = [-1] * n
c[0] = 0

dfs(0)

print(*c, sep="\n")


Fixed submission:
import sys

sys.setrecursionlimit(10**7)


def dfs(v):
    for u, w in e[v]:
        if c[u] != -1:
            continue
        c[u] = (c[v] + w) % 2
        dfs(u)


n = int(input())
e = tuple(set() for _ in range(n))
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    e[u].add((v, w))
    e[v].add((u, w))

c = [-1] * n
c[0] = 0

dfs(0)

print(*c, sep="\n")
Problem p03044 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-
n = int(input())
g = [[] for i in range(n)]
c = [-1 for i in range(n)]

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


def dfs(p, d):
    if c[p] == -1:
        c[p] = d
        for x, w in g[p]:
            dfs(x, d ^ w)


dfs(0, 0)

for i in range(len(c)):
    print(c[i])


Fixed submission:
# -*- coding: utf-8 -*-
import sys

sys.setrecursionlimit(100100)

n = int(input())
g = [[] for i in range(n)]
c = [-1 for i in range(n)]

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


def dfs(p, d):
    if c[p] == -1:
        c[p] = d
        for x, w in g[p]:
            dfs(x, d ^ w)


dfs(0, 0)

for i in range(len(c)):
    print(c[i])
Problem p03044 (C++)

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

using namespace std;

typedef long long ll;

struct edge {
  ll dst, cost;
  edge() {}
  edge(ll b, ll c) : dst(b), cost(c) {}
};

vector<edge> tree[1005];
int ans[1005];

// 頂点 0 を 0(白) と決める.
// 頂点 0 から dfs
// v: 今いる頂点, p: v の親
// f: 親の色
// cost: 直前のコスト
void dfs(ll v, ll p, ll f, ll cost) {
  if ((f == 0 && (cost & 1)) || (f == 1 && (cost % 2) == 0)) {
    ans[v] = 1;
  } else {
    ans[v] = 0;
  }
  for (ll i = 0; i < tree[v].size(); i++) {
    edge e = tree[v][i];
    // 戻らないようにする処理
    if (e.dst == p) {
      continue;
    }
    dfs(e.dst, v, ans[v], e.cost);
  }
  return;
}

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

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

  dfs(0, -1, 0, 0);

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

  return 0;
}

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

using namespace std;

typedef long long ll;

struct edge {
  ll dst, cost;
  edge() {}
  edge(ll b, ll c) : dst(b), cost(c) {}
};

vector<edge> tree[100005];
int ans[100005];

// 頂点 0 を 0(白) と決める.
// 頂点 0 から dfs
// v: 今いる頂点, p: v の親
// f: 親の色
// cost: 直前のコスト
void dfs(ll v, ll p, ll f, ll cost) {
  if ((f == 0 && (cost & 1)) || (f == 1 && (cost % 2) == 0)) {
    ans[v] = 1;
  } else {
    ans[v] = 0;
  }
  for (ll i = 0; i < tree[v].size(); i++) {
    edge e = tree[v][i];
    // 戻らないようにする処理
    if (e.dst == p) {
      continue;
    }
    dfs(e.dst, v, ans[v], e.cost);
  }
  return;
}

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

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

  dfs(0, -1, 0, 0);

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

  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;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

using Edge = pair<int, ll>;
using Graphy = vector<vector<Edge>>;

int n;
Graphy g;
vector<int> ans(n);

void dfs(int v, int p, int c) {
  ans[v] = c;
  for (auto e : g[v]) {
    if (e.first == p)
      continue;
    if (e.second & 1)
      dfs(e.first, v, 1 - c);
    else
      dfs(e.first, v, c);
  }
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(Edge(v, w));
    g[v].push_back(Edge(u, w));
  }
  ans.assign(n, 0);
  dfs(0, -1, 0);

  for (auto d : ans)
    cout << d << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

using Edge = pair<int, ll>;
using Graphy = vector<vector<Edge>>;

int n;
Graphy g;
vector<int> ans(n);

void dfs(int v, int p, int c) {
  ans[v] = c;
  for (auto e : g[v]) {
    if (e.first == p)
      continue;
    if (e.second & 1)
      dfs(e.first, v, 1 - c);
    else
      dfs(e.first, v, c);
  }
}

int main() {
  cin >> n;
  g.assign(n, vector<Edge>());
  rep(i, n - 1) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(Edge(v, w));
    g[v].push_back(Edge(u, w));
  }
  ans.assign(n, 0);
  dfs(0, -1, 0);

  for (auto d : ans)
    cout << d << endl;

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

vector<vector<int>> g;
vector<vector<int>> gw;
vector<bool> done;
vs32 ans;

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

  int N;
  cin >> N;

  g.resize(N);
  gw.resize(N);
  done.resize(N, false);
  ans.resize(N);
  int u, v, w;
  rep(i, N - 1) {
    cin >> u >> v >> w;
    g[u - 1].push_back(v - 1);
    g[w - 1].push_back(u - 1);
    gw[u - 1].push_back(w);
    gw[v - 1].push_back(w);
  }

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

  while (!q.empty()) {
    int root = q.front();
    q.pop();
    done[root] = true;

    rep(i, g[root].size()) {
      int leaf = g[root][i];
      if (done[leaf])
        continue;
      int dist = gw[root][i];
      if (dist & 1)
        ans[leaf] = ans[root] ^ 1;
      else
        ans[leaf] = ans[root];
      q.push(leaf);
    }
  }

  rep(i, N) cout << ans[i] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

vector<vector<int>> g;
vector<vector<int>> gw;
vector<bool> done;
vs32 ans;

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

  int N;
  cin >> N;

  g.resize(N);
  gw.resize(N);
  done.resize(N, false);
  ans.resize(N);
  int u, v, w;
  rep(i, N - 1) {
    cin >> u >> v >> w;
    g[u - 1].push_back(v - 1);
    g[v - 1].push_back(u - 1);
    gw[u - 1].push_back(w);
    gw[v - 1].push_back(w);
  }

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

  while (!q.empty()) {
    int root = q.front();
    q.pop();
    done[root] = true;

    rep(i, g[root].size()) {
      int leaf = g[root][i];
      if (done[leaf])
        continue;
      int dist = gw[root][i];
      if (dist & 1)
        ans[leaf] = ans[root] ^ 1;
      else
        ans[leaf] = ans[root];
      q.push(leaf);
    }
  }

  rep(i, N) cout << ans[i] << "\n";
  return 0;
}
Problem p03044 (C++)

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

vector<pair<int, int>> N[10009];
int v[10009], n;

void bfs(int k) {
  queue<int> Q;
  Q.push(k);
  while (!Q.empty()) {
    for (int j = 0; j < N[Q.front()].size(); j++) {
      int nei_v = N[Q.front()][j].first;
      int nei_w = N[Q.front()][j].second;
      if (v[nei_v] == -1) {
        v[nei_v] = (v[Q.front()] + nei_w) % 2;
        Q.push(nei_v);
      }
    }
    Q.pop();
  }
}

void solve() {
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  v[0] = 1;
  bfs(0);
  for (int i = 0; i < n; i++) {
    cout << v[i] << endl;
  }
}

int main() {
  cin >> n;
  int tmp_u, tmp_v, tmp_w;
  for (int i = 0; i < n - 1; i++) {
    cin >> tmp_u >> tmp_v >> tmp_w;
    tmp_u--;
    tmp_v--;
    N[tmp_u].push_back(make_pair(tmp_v, tmp_w));
    N[tmp_v].push_back(make_pair(tmp_u, tmp_w));
  }
  solve();
}

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

vector<pair<int, int>> N[100009];
int v[100009], n;

void bfs(int k) {
  queue<int> Q;
  Q.push(k);
  while (!Q.empty()) {
    for (int j = 0; j < N[Q.front()].size(); j++) {
      int nei_v = N[Q.front()][j].first;
      int nei_w = N[Q.front()][j].second;
      if (v[nei_v] == -1) {
        v[nei_v] = (v[Q.front()] + nei_w) % 2;
        Q.push(nei_v);
      }
    }
    Q.pop();
  }
}

void solve() {
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  v[0] = 1;
  bfs(0);
  for (int i = 0; i < n; i++) {
    cout << v[i] << endl;
  }
}

int main() {
  cin >> n;
  int tmp_u, tmp_v, tmp_w;
  for (int i = 0; i < n - 1; i++) {
    cin >> tmp_u >> tmp_v >> tmp_w;
    tmp_u--;
    tmp_v--;
    N[tmp_u].push_back(make_pair(tmp_v, tmp_w));
    N[tmp_v].push_back(make_pair(tmp_u, tmp_w));
  }
  solve();
}
Problem p03044 (C++)

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

using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

vector<int> result;
int N;
Graph G;

void dfs(int v, int c) {
  result[v] = c;
  for (auto nx : G[v]) {
    if (result[nx.first] != -1) {
      continue;
    }
    if (nx.second % 2 == 0) {
      dfs(nx.first, c);
    }
    if (nx.second % 2 == 1) {
      dfs(nx.first, 1 - c);
    }
  }
}

int main() {
  cin >> N;
  G.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].push_back(Edge(v, w));
    G[v].push_back(Edge(u, w));
  }
  result.assign(N, -1);
  dfs(0, 0);
  for (auto x : result) {
    cout << x << endl;
  }
  return 0;
}

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

using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

vector<int> result;
int N;
Graph G;

void dfs(int v, int c) {
  result[v] = c;
  for (auto nx : G[v]) {
    if (result[nx.first] != -1) {
      continue;
    }
    if (nx.second % 2 == 0) {
      dfs(nx.first, c);
    }
    if (nx.second % 2 == 1) {
      dfs(nx.first, 1 - c);
    }
  }
}

int main() {
  cin >> N;
  G.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].push_back(Edge(v, w));
    G[v].push_back(Edge(u, w));
  }
  result.assign(N, -1);
  dfs(0, 0);
  for (auto x : result) {
    cout << x << endl;
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

struct Edge {
  int to, dist;
  Edge(int a, int b) : to(a), dist(b) {}
};

vector<vector<Edge>> G;
vector<int> colors;

void dfs(int v, int c) {
  for (const auto &e : G[v]) {
    int u = e.to;
    int w = e.dist;
    int nc = (w % 2 == 0) ? c : -c;
    if (colors[u] == 0) {
      colors[u] = nc;
      dfs(u, nc);
    }
  }
}

int main() {
  int N;
  cin >> N;
  G.resize(N);
  colors.resize(N, 0);

  for (int i = 0; i < N; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    G[u].push_back(Edge(v, w));
    G[v].push_back(Edge(u, w));
  }

  colors[0] = 1;
  dfs(0, 1);
  for (int c : colors) {
    cout << max(c, 0) << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

struct Edge {
  int to, dist;
  Edge(int a, int b) : to(a), dist(b) {}
};

vector<vector<Edge>> G;
vector<int> colors;

void dfs(int v, int c) {
  for (const auto &e : G[v]) {
    int u = e.to;
    int w = e.dist;
    int nc = (w % 2 == 0) ? c : -c;
    if (colors[u] == 0) {
      colors[u] = nc;
      dfs(u, nc);
    }
  }
}

int main() {
  int N;
  cin >> N;
  G.resize(N);
  colors.resize(N, 0);

  for (int i = 0; i < N - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    G[u].push_back(Edge(v, w));
    G[v].push_back(Edge(u, w));
  }

  colors[0] = 1;
  dfs(0, 1);
  for (int c : colors) {
    cout << max(c, 0) << endl;
  }
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<int> vecint;
typedef vector<double> vecd;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<bool> vecbool;
////////////////////////////////////////////////
int n;
vector<vector<pair<int, int>>>
    u; // u[i].first = node from u[i], u[i].second = cost
// output
vecint ans;
////////////////////////////////////////////////
void dfs(int pos, int color) {
  ans[pos] = color;
  REPvec(itr, u[pos]) {
    if (ans[itr->first] > 0)
      continue;
    dfs(itr->first, (color + (itr->second)) % 2);
  }
}
////////////////////////////////////////////////
int main() {
  cin >> n;
  u.resize(n);
  ans.resize(n);
  ll a, b, c;
  REP(i, n - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    u[a].push_back(make_pair(b, c));
    u[b].push_back(make_pair(a, c));
  }
  REP(i, n) { ans[i] = -1; }
  dfs(0, 0);
  REP(i, n) { cout << ans[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<int> vecint;
typedef vector<double> vecd;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<bool> vecbool;
////////////////////////////////////////////////
int n;
vector<vector<pair<int, int>>>
    u; // u[i].first = node from u[i], u[i].second = cost
// output
vecint ans;
////////////////////////////////////////////////
void dfs(int pos, int color) {
  ans[pos] = color;
  REPvec(itr, u[pos]) {
    if (ans[itr->first] != -1)
      continue;
    dfs(itr->first, (color + (itr->second)) % 2);
  }
}
////////////////////////////////////////////////
int main() {
  cin >> n;
  u.resize(n);
  ans.resize(n);
  ll a, b, c;
  REP(i, n - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    u[a].push_back(make_pair(b, c));
    u[b].push_back(make_pair(a, c));
  }
  REP(i, n) { ans[i] = -1; }
  dfs(0, 0);
  REP(i, n) { cout << ans[i] << endl; }
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct edge {
  int next, to, v;
} e[maxn];
int h[maxn], col[maxn], n, cnt;
void addedge(int x, int y, int z) {
  e[++cnt].next = h[x];
  e[cnt].to = y;
  e[cnt].v = z;
  h[x] = cnt;
}
void dfs(int u, int fa, int now) {
  // cout<<u<<' '<<fa<<' '<<now<<endl;
  col[u] = now;
  for (register int i = h[u]; i; i = e[i].next) {
    int j = e[i].to;
    if (j == fa) {
      continue;
    }
    dfs(j, u, e[i].v % 2 == 1 ? (now ^ 1) : now);
  }
}
int main() {
  int x, y, z;
  scanf("%d", &n);
  for (register int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    addedge(x, y, z);
    addedge(y, x, z);
  }
  dfs(1, 0, 0);
  for (register int i = 1; i <= n; i++) {
    printf("%d\n", col[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
struct edge {
  int next, to, v;
} e[maxn];
int h[maxn], col[maxn], n, cnt;
void addedge(int x, int y, int z) {
  e[++cnt].next = h[x];
  e[cnt].to = y;
  e[cnt].v = z;
  h[x] = cnt;
}
void dfs(int u, int fa, int now) {
  // cout<<u<<' '<<fa<<' '<<now<<endl;
  col[u] = now;
  for (register int i = h[u]; i; i = e[i].next) {
    int j = e[i].to;
    if (j == fa) {
      continue;
    }
    dfs(j, u, e[i].v % 2 == 1 ? (now ^ 1) : now);
  }
}
int main() {
  int x, y, z;
  scanf("%d", &n);
  for (register int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    addedge(x, y, z);
    addedge(y, x, z);
  }
  dfs(1, 0, 0);
  for (register int i = 1; i <= n; i++) {
    printf("%d\n", col[i]);
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

int color[10010];

void dfs(vector<vector<pair<int, int>>> &g, int v, int p, int c) {
  color[v] = c;
  for (auto &e : g[v]) {
    if (e.first == p)
      continue;
    dfs(g, e.first, v, e.second ? !c : c);
  }
}

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

  dfs(g, 1, -1, 0);

  for (int i = 1; i <= n; ++i) {
    cout << color[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

int color[100010];

void dfs(vector<vector<pair<int, int>>> &g, int v, int p, int c) {
  color[v] = c;
  for (auto &e : g[v]) {
    if (e.first == p)
      continue;
    dfs(g, e.first, v, e.second ? !c : c);
  }
}

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

  dfs(g, 1, -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>
using namespace std;

int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
void dfs(int a, int b) {
  seen[a] = b;
  for (auto x : G[a]) {
    int s, t;
    tie(s, t) = x;
    if (seen[s] == -1) {
      dfs(s, (b + t) % 2);
    }
  }
}

int main() {
  cin >> N;
  G.resize(N);
  seen.resize(N);
  for (int i = 0; i < N; i++) {
    seen[i] = -1;
  }
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[b].push_back(make_pair(a, c));
    G[a].push_back(make_pair(b, c));
  }
  dfs(0, 0);
  for (int i = 0; i < N; i++) {
    cout << seen[i] << endl;
  }
}

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

int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
void dfs(int a, int b) {
  seen[a] = b;
  for (auto x : G[a]) {
    int s, t;
    tie(s, t) = x;
    if (seen[s] == -1) {
      dfs(s, (b + t) % 2);
    }
  }
}

int main() {
  cin >> N;
  G.resize(N);
  seen.resize(N);
  for (int i = 0; i < N; i++) {
    seen[i] = -1;
  }
  for (int i = 1; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[b].push_back(make_pair(a, c));
    G[a].push_back(make_pair(b, c));
  }
  dfs(0, 0);
  for (int i = 0; i < N; i++) {
    cout << seen[i] << endl;
  }
}
Problem p03044 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<vector<int>> b(n);
  for (int i = 0; i < n - 1; i++) {
    int a, s, w;
    cin >> a >> s >> w;
    G[a].push_back(s);
    G[s].push_back(a);
    b[a].push_back(w);
    b[s].push_back(w);
  }
  vector<int> co(n, -1);
  co[0] = 0;
  queue<int> que;
  que.push(0);
  int j = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv = 0; nv < G[v].size(); nv++) {
      int u = G[v][nv];
      int w = b[v][nv];
      if (co[u] != -1)
        continue;
      co[u] = (co[v] + w) % 2;
      que.push(u);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << co[i] << endl;
  }
}


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

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

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

vector<vector<Edge>> g(100005);
vector<int> visited(10005, 0);

void dfs(int u, int c) {
  visited[u] = c;
  for (Edge e : g[u]) {
    int to = e.to, cost = e.cost;
    if (visited[to] != 0)
      continue;
    if (cost % 2)
      dfs(to, -c);
    else
      dfs(to, c);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  dfs(0, 1);
  rep(i, n) { cout << (visited[i] == 1 ? 1 : 0) << endl; }
}

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

vector<vector<Edge>> g(100005);
vector<int> visited(100005, 0);

void dfs(int u, int c) {
  visited[u] = c;
  for (Edge e : g[u]) {
    int to = e.to, cost = e.cost;
    if (visited[to] != 0)
      continue;
    if (cost % 2)
      dfs(to, -c);
    else
      dfs(to, c);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  dfs(0, 1);
  rep(i, n) { cout << (visited[i] == 1 ? 1 : 0) << endl; }
}
Problem p03044 (C++)

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

int N;
vector<vector<int>> G;
vector<vector<int>> W;
vector<int> res;

void dfs(int u) {
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (res[v] != -1)
      continue;
    res[v] = (res[u] + W[u][i]) % 2;
    dfs(v);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  W.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
    W[u].push_back(w);
    G[v].push_back(w);
  }

  res.resize(N, -1);
  res[0] = 0;
  dfs(0);

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

  return 0;
}

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

int N;
vector<vector<int>> G;
vector<vector<int>> W;
vector<int> res;

void dfs(int u) {
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (res[v] != -1)
      continue;
    res[v] = (res[u] + W[u][i]) % 2;
    dfs(v);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  W.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
    W[u].push_back(w);
    W[v].push_back(w);
  }

  res.resize(N, -1);
  res[0] = 0;
  dfs(0);

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

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
using pqueg = priority_queue<int, vector<int>, greater<int>>;
using pquel = priority_queue<int, vector<int>, less<int>>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int r_int(int a, int b) { return rand() % b + a; }
const lint mod = 1e9 + 7;

lint n;
vector<vector<plint>> cnct;
vector<int> seen;

void dfs(lint node, int color) {
  seen[node] = color;
  for (plint next : cnct[node]) {
    // 隣接していて，かつ未探索ノードを探す
    if (next.second != 0 && seen[next.first] == -1) {
      // 辺の長さが偶数なら同じ色で塗る
      if (next.second % 2 == 0)
        dfs(next.first, color);
      else
        dfs(next.first, (color + 1) % 2);
    }
  }
}

int main() {
  cin >> n;
  cnct.assign(n, vector<plint>(n));
  REP(i, n - 1) {
    lint x, y, edge;
    cin >> x >> y >> edge;
    x--;
    y--;
    cnct[x].push_back({y, edge});
    cnct[y].push_back({x, edge});
  }

  seen.assign(n, -1);
  dfs(0, 0);

  REP(i, n) cout << seen[i] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
using pqueg = priority_queue<int, vector<int>, greater<int>>;
using pquel = priority_queue<int, vector<int>, less<int>>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int r_int(int a, int b) { return rand() % b + a; }
const lint mod = 1e9 + 7;

lint n;
vector<vector<plint>> cnct;
vector<int> seen;

void dfs(lint node, int color) {
  seen[node] = color;
  for (plint next : cnct[node]) {
    // 隣接していて，かつ未探索ノードを探す
    if (next.second != 0 && seen[next.first] == -1) {
      // 辺の長さが偶数なら同じ色で塗る
      if (next.second % 2 == 0)
        dfs(next.first, color);
      else
        dfs(next.first, (color + 1) % 2);
    }
  }
}

int main() {
  cin >> n;
  cnct.assign(n, vector<plint>(0));
  REP(i, n - 1) {
    lint x, y, edge;
    cin >> x >> y >> edge;
    x--;
    y--;
    cnct[x].push_back({y, edge});
    cnct[y].push_back({x, edge});
  }

  seen.assign(n, -1);
  dfs(0, 0);

  REP(i, n) cout << seen[i] << "\n";
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[10005];
bool use[10005];
int d[10005];
void dfs(int n, int w) {
  use[n] = true;
  d[n] = (w & 1);
  for (int i = 0; i < v[n].size(); i++) {
    if (!use[v[n][i].first])
      dfs(v[n][i].first, v[n][i].second + w);
  }
}
int n, mb;
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << d[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[100005];
bool use[100005];
int d[100005];
void dfs(int n, int w) {
  use[n] = true;
  d[n] = (w & 1);
  for (int i = 0; i < v[n].size(); i++) {
    if (!use[v[n][i].first])
      dfs(v[n][i].first, v[n][i].second + w);
  }
}
int n, mb;
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << d[i] << endl;
  }
  return 0;
}