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

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

const int Maxv = 100005;
struct edge {
  int next, to, w;
} e[Maxv];
int head[Maxv], cnt = 0;

void add_edge(int x, int y, int w) {
  cnt++;
  e[cnt].to = y;
  e[cnt].next = head[x];
  e[cnt].w = w;
  head[x] = cnt;
}

int ans[Maxv];
void dfs(int v, int f) {
  int y, w, i;
  for (i = head[v]; i != -1; i = e[i].next) {
    y = e[i].to;
    w = e[i].w % 2;
    if (y != f) {
      ans[y] = ans[v] ^ w;
      dfs(y, v);
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  int n, i, j, x, y, w;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y >> w;
    add_edge(x, y, w);
    add_edge(y, x, w);
  }

  dfs(1, 0);

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

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

const int Maxv = 1000005;
struct edge {
  int next, to, w;
} e[Maxv];
int head[Maxv], cnt = 0;

void add_edge(int x, int y, int w) {
  cnt++;
  e[cnt].to = y;
  e[cnt].next = head[x];
  e[cnt].w = w;
  head[x] = cnt;
}

int ans[Maxv];
void dfs(int v, int f) {
  int y, w, i;
  for (i = head[v]; i != -1; i = e[i].next) {
    y = e[i].to;
    w = e[i].w % 2;
    if (y != f) {
      ans[y] = ans[v] ^ w;
      dfs(y, v);
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  int n, i, j, x, y, w;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y >> w;
    add_edge(x, y, w);
    add_edge(y, x, w);
  }

  dfs(1, 0);

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

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

int n, m;
vector<vector<P>> dat(101);
vector<int> col(10, -1);
vector<bool> check(10, false);

int main() {
  int n;
  cin >> n;
  col = vector<int>(n + 10, -1);
  check = vector<bool>(n + 10, false);
  dat = vector<vector<P>>(n + 10);
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    dat[u].push_back(P(v, w));
    dat[v].push_back(P(u, w));
  }
  queue<int> que;
  que.push(1);
  col[1] = 0;
  while (que.size()) {
    int q = que.front();
    que.pop();
    check[q] = true;
    for (auto p : dat[q]) {
      if (col[p.first] != -1)
        continue;
      if (dat[q][p.first].second)
        col[p.first] = col[q];
      else
        col[p.first] = abs(1 - col[q]);
      if (check[p.first])
        continue;
      que.push(p.first);
    }
  }
  rep(i, n) { cout << col[i] << endl; }
}

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

int n, m;
vector<vector<P>> dat(101);
vector<int> col(10, -1);
vector<bool> check(10, false);

int main() {
  int n;
  cin >> n;
  col = vector<int>(n + 10, -1);
  check = vector<bool>(n + 10, false);
  dat = vector<vector<P>>(n + 10);
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    dat[u].push_back(P(v, w));
    dat[v].push_back(P(u, w));
  }
  queue<int> que;
  que.push(1);
  col[1] = 0;
  while (que.size()) {
    int q = que.front();
    que.pop();
    check[q] = true;
    for (auto p : dat[q]) {
      if (col[p.first] != -1)
        continue;
      if (p.second % 2 == 0)
        col[p.first] = col[q];
      else
        col[p.first] = abs(1 - col[q]);
      if (check[p.first])
        continue;
      que.push(p.first);
    }
  }
  rep(i, n) { cout << col[i] << endl; }
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
/* basic header */
#include <bits/stdc++.h>
/* define */
#define ll long long
#define dou double
#define pb emplace_back
#define mp make_pair
#define sot(a, b) sort(a + 1, a + 1 + b)
#define rep1(i, a, b) for (int i = a; i <= b; ++i)
#define rep0(i, a, b) for (int i = a; i < b; ++i)
#define eps 1e-8
#define int_inf 0x3f3f3f3f
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lson curPos << 1
#define rson curPos << 1 | 1
/* namespace */
using namespace std;
/* header end */

const int maxn = 1e5 + 10;
struct Edge {
  int u, v, w;
} edge[maxn];
int n, cnt, ans[maxn], vis[maxn];
vector<int> g[maxn];

void addedge(int u, int v, int w) {
  edge[++cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = w;
  g[u].push_back(cnt);
}

void bfs() {
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (int i = 0; i < g[x].size(); i++) {
      int cur = g[x][i];
      if (!vis[edge[cur].v]) {
        if (edge[cur].w % 2 == 0) {
          ans[edge[cur].v] = ans[x];

        } else {
          ans[edge[cur].v] = 1 - ans[x];
        }
        Q.push(edge[cur].v);
      }
    }
  }
}

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

Fixed submission:
/* basic header */
#include <bits/stdc++.h>
/* define */
#define ll long long
#define dou double
#define pb emplace_back
#define mp make_pair
#define sot(a, b) sort(a + 1, a + 1 + b)
#define rep1(i, a, b) for (int i = a; i <= b; ++i)
#define rep0(i, a, b) for (int i = a; i < b; ++i)
#define eps 1e-8
#define int_inf 0x3f3f3f3f
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lson curPos << 1
#define rson curPos << 1 | 1
/* namespace */
using namespace std;
/* header end */

const int maxn = 1e5 + 10;
struct Edge {
  int u, v, w;
} edge[maxn * 2];
int n, cnt, ans[maxn], vis[maxn];
vector<int> g[maxn];

void addedge(int u, int v, int w) {
  edge[++cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = w;
  g[u].push_back(cnt);
}

void bfs() {
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (int i = 0; i < g[x].size(); i++) {
      int cur = g[x][i];
      if (!vis[edge[cur].v]) {
        if (edge[cur].w % 2 == 0) {
          ans[edge[cur].v] = ans[x];

        } else {
          ans[edge[cur].v] = 1 - ans[x];
        }
        Q.push(edge[cur].v);
      }
    }
  }
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 1e5;

struct edge {
  int to, cost;
};

int n;
vector<vector<edge>> G(MAXN);
vector<int> color(MAXN, -1);

void dfs(int u, int c) {
  color[u] = c;
  for (auto e : G[u]) {
    if (color[e.to] != -1)
      continue;
    if (e.cost % 2 == 0)
      dfs(e.to, c);
    else if (e.cost % 2 == 1)
      dfs(e.to, 1 - c);
  }
  return;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int from, to, cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    G[from].push_back(edge{to, cost});
    G[to].push_back(edge{from, cost});
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << color[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 1e5;

struct edge {
  int to, cost;
};

int n;
vector<vector<edge>> G(MAXN);
vector<int> color(MAXN, -1);

void dfs(int u, int c) {
  color[u] = c;
  for (auto e : G[u]) {
    if (color[e.to] != -1)
      continue;
    if (e.cost % 2 == 0)
      dfs(e.to, c);
    else if (e.cost % 2 == 1)
      dfs(e.to, 1 - c);
  }
  return;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int from, to, cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    G[from].push_back(edge{to, cost});
    G[to].push_back(edge{from, cost});
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << color[i] << endl;
  }
  return 0;
}
Problem p03044 (C++)

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

void solve(long long N, std::vector<long long> u, std::vector<long long> v,
           std::vector<long long> w) {
  vector<bool> visited(N + 1, false);
  vector<ll> length(N + 1, 0);
  vector<vector<pair<ll, ll>>> link_list(N + 1);
  for (ll i = 0; i < N; i++) {
    link_list[u[i]].push_back(make_pair(v[i], w[i]));
    link_list[v[i]].push_back(make_pair(u[i], w[i]));
  }
  queue<ll> q;
  q.push(1);
  visited[1] = true;
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    for (const auto &next_node : link_list[node]) {
      if (!visited[next_node.first]) {
        q.push(next_node.first);
        length[next_node.first] = length[node] + next_node.second;
        visited[next_node.first] = true;
      }
    }
  }
  for (ll i = 1; i <= N; i++) {
    printf("%s\n", length[i] % 2 == 0 ? "0" : "1");
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> u(N - 1);
  std::vector<long long> v(N - 1);
  std::vector<long long> w(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    scanf("%lld", &w[i]);
  }
  solve(N, std::move(u), std::move(v), std::move(w));
  return 0;
}


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

void solve(long long N, std::vector<long long> u, std::vector<long long> v,
           std::vector<long long> w) {
  vector<bool> visited(N + 1, false);
  vector<ll> length(N + 1, 0);
  vector<vector<pair<ll, ll>>> link_list(N + 1);
  for (ll i = 0; i < N - 1; i++) {
    link_list[u[i]].push_back(make_pair(v[i], w[i] % 2));
    link_list[v[i]].push_back(make_pair(u[i], w[i] % 2));
  }
  queue<ll> q;
  q.push(1);
  visited[1] = true;
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    for (const auto &next_node : link_list[node]) {
      if (!visited[next_node.first]) {
        q.push(next_node.first);
        length[next_node.first] = length[node] + next_node.second;
        visited[next_node.first] = true;
      }
    }
  }
  for (ll i = 1; i <= N; i++) {
    printf("%s\n", length[i] % 2 == 0 ? "0" : "1");
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> u(N - 1);
  std::vector<long long> v(N - 1);
  std::vector<long long> w(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    scanf("%lld", &w[i]);
  }
  solve(N, std::move(u), std::move(v), std::move(w));
  return 0;
}
Problem p03044 (C++)

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

vector<int> color;

void dfs(int v, int dis, vector<vector<pair<int, int>>> G) {
  if (dis % 2 == 0)
    color.at(v) = 0;
  else
    color.at(v) = 1;

  for (auto k : G.at(v)) {
    int nv = k.first;
    int tmp = k.second;
    if (color.at(nv) != -1)
      continue;
    dfs(nv, dis + tmp, G);
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    G.at(u).push_back({v, w});
    G.at(v).push_back({u, w});
  }
  color.resize(n, -1);
  color.at(0) = 0;
  dfs(0, 0, G);
  for (int a : color)
    cout << a << endl;
}

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

vector<int> color;

void dfs(int v, int dis, vector<vector<pair<int, int>>> &G) {
  if (dis % 2 == 0)
    color.at(v) = 0;
  else
    color.at(v) = 1;

  for (auto k : G.at(v)) {
    int nv = k.first;
    int tmp = k.second;
    if (color.at(nv) != -1)
      continue;
    dfs(nv, dis + tmp, G);
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    G.at(u).push_back({v, w});
    G.at(v).push_back({u, w});
  }
  color.resize(n, -1);
  color.at(0) = 0;
  dfs(0, 0, G);
  for (int a : color)
    cout << a << 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 rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pii>> g(n);
  v1i f(n, 0);
  rep(i, n) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --a;
    --b;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }
  deque<int> d;
  d.push_back(0);
  int tmp = 0;
  v1b e(n, 1);
  e[0] = 0;
  while (!d.empty()) {
    d.pop_front();
    for (auto h : g[tmp]) {
      if (e[h.fi]) {
        e[h.fi] = 0;
        d.push_back(h.fi);
        f[h.fi] = f[tmp] + h.se;
      }
    }
    tmp = d.front();
  }
  rep(i, n) {
    if (f[i] & 1)
      printf("1\n");
    else
      printf("0\n");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pii>> g(n);
  v1i f(n, 0);
  rep(i, n - 1) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --a;
    --b;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }
  deque<int> d;
  d.push_back(0);
  int tmp = 0;
  v1b e(n, 1);
  e[0] = 0;
  while (!d.empty()) {
    d.pop_front();
    for (auto h : g[tmp]) {
      if (e[h.fi]) {
        e[h.fi] = 0;
        d.push_back(h.fi);
        f[h.fi] = f[tmp] + h.se;
      }
    }
    tmp = d.front();
  }
  rep(i, n) {
    if (f[i] & 1)
      printf("1\n");
    else
      printf("0\n");
  }
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INF = 1e18;
#define foreach(ix, a) for (auto &(ix) : (a))
vector<vector<pair<ll, ll>>> edge(11000);
ll seen[11000];
void dfs(ll i, ll co) {
  for (pair<ll, ll> v : edge[i]) {
    ll p = v.first, dis = v.second;
    if (seen[p] != -1)
      continue;
    seen[p] = (co + dis) % 2;
    dfs(p, seen[p]);
  }
}
int main() {
  int n, m;
  cin >> n;
  rep(i, n) seen[i] = -1;
  rep(i, n - 1) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    edge[u].push_back({v, w});
    edge[v].push_back({u, w});
  }
  seen[0] = 0;
  dfs(0, seen[0]);
  rep(i, n) { cout << seen[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INF = 1e18;
#define foreach(ix, a) for (auto &(ix) : (a))
vector<vector<pair<ll, ll>>> edge(110000);
ll seen[110000];
void dfs(ll i, ll co) {
  for (pair<ll, ll> v : edge[i]) {
    ll p = v.first, dis = v.second;
    if (seen[p] != -1)
      continue;
    seen[p] = (co + dis) % 2;
    dfs(p, seen[p]);
  }
}
int main() {
  int n, m;
  cin >> n;
  rep(i, n) seen[i] = -1;
  rep(i, n - 1) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    edge[u].push_back({v, w});
    edge[v].push_back({u, w});
  }
  seen[0] = 0;
  dfs(0, seen[0]);
  rep(i, n) { cout << seen[i] << endl; }
}
Problem p03044 (C++)

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

#define MAX_V 100000
#define MAX_E 100000

vector<pair<int, int>> G[MAX_V];
int color[MAX_V];

bool dfs(int v, int c) {
  // printf("%d %d\n", v, c);
  color[v] = c + 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (color[G[v][i].first] == 0) // not visited
    {
      dfs(G[v][i].first, c ^ G[v][i].second);
    }
  }
}

int main() {

  int n;
  int u, v, w;

  cin >> n;

  for (int i = 1; i <= n - 1; i++) {
    cin >> u >> v >> w;
    G[u].push_back(make_pair(v, w % 2));
    G[v].push_back(make_pair(u, w % 2));
  }

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

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

#define MAX_V 100001
#define MAX_E 100001

vector<pair<int, int>> G[MAX_V];
int color[MAX_V];

bool dfs(int v, int c) {
  // printf("%d %d\n", v, c);
  color[v] = c + 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (color[G[v][i].first] == 0) // not visited
    {
      dfs(G[v][i].first, c ^ G[v][i].second);
    }
  }
}

int main() {

  int n;
  int u, v, w;

  cin >> n;

  for (int i = 1; i <= n - 1; i++) {
    cin >> u >> v >> w;
    G[u].push_back(make_pair(v, w % 2));
    G[v].push_back(make_pair(u, w % 2));
  }

  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << color[i] - 1 << endl;
  }
}
Problem p03044 (C++)

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

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using edge = pair<int, llint>;
using Graph = vector<vector<edge>>;

llint n;
Graph G;
vector<llint> dir;

void dfs(int v, int p, int cur) {
  dir[v] = cur;
  for (auto e : G[v]) {
    if (e.first == p)
      continue;
    if (e.second & 1)
      dfs(e.first - 1, v, 1 - cur); // 長さが奇数
    else
      dfs(e.first, v, cur);
  }
}

void solve() {
  cin >> n;
  G.assign(n, vector<edge>());
  for (int i = 0; i < n - 1; i++) {
    llint a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  dir.assign(n, 0);
  dfs(0, -1, 1);
  for (auto d : dir)
    cout << d << endl;
}

int main() {
  solve();
  return 0;
}


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

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using edge = pair<int, llint>;
using Graph = vector<vector<edge>>;

llint n;
Graph G;
vector<llint> dir;

void dfs(int v, int p, int cur) {
  dir[v] = cur;
  for (auto e : G[v]) {
    if (e.first == p)
      continue;
    if (e.second & 1)
      dfs(e.first, v, 1 - cur); // 長さが奇数
    else
      dfs(e.first, v, cur);
  }
}

void solve() {
  cin >> n;
  G.assign(n, vector<edge>());
  for (int i = 0; i < n - 1; i++) {
    llint a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  dir.assign(n, 0);
  dfs(0, -1, 1);
  for (auto d : dir)
    cout << d << endl;
}

int main() {
  solve();
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM + 7
#define pi pair<int, int>
#define pl pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }

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


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM + 7
#define pi pair<int, int>
#define pl pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int ll;

using namespace std;

int dfs(int u, vector<vector<int>> G, vector<vector<int>> G_w,
        vector<int> &visit, vector<int> &nagasa) {

  visit[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {

    int v = G[u][i];

    if (visit[v] == 0) {

      nagasa[v] = (nagasa[u] + G_w[u][i] % 2) % 2;
      dfs(v, G, G_w, visit, nagasa);
    }
  }

  return 0;
}

int main() {

  int N;
  cin >> N;
  vector<vector<int>> G(N);

  vector<vector<int>> G_w(N);

  vector<int> nagasa(N, 0);
  vector<int> visit(N, 0);

  for (int i = 0; i < N - 1; i++) {

    int u, v, w;

    cin >> u >> v >> w;

    G[u - 1].push_back(v - 1);
    G[v - 1].push_back(u - 1);
    G_w[u - 1].push_back(w);
    G_w[v - 1].push_back(w);
  }

  dfs(0, G, G_w, visit, nagasa);

  for (int i = 0; i < N; i++) {

    if (nagasa[i] == 0) {

      cout << '1' << endl;
    } else {

      cout << '0' << endl;
    }
  }

  return 0;
}

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

using namespace std;

int dfs(int u, vector<vector<int>> &G, vector<vector<int>> &G_w,
        vector<int> &visit, vector<int> &nagasa) {

  visit[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {

    int v = G[u][i];

    if (visit[v] == 0) {

      nagasa[v] = (nagasa[u] + G_w[u][i] % 2) % 2;
      dfs(v, G, G_w, visit, nagasa);
    }
  }

  return 0;
}

int main() {

  int N;
  cin >> N;
  vector<vector<int>> G(N);

  vector<vector<int>> G_w(N);

  vector<int> nagasa(N, 0);
  vector<int> visit(N, 0);

  for (int i = 0; i < N - 1; i++) {

    int u, v, w;

    cin >> u >> v >> w;

    G[u - 1].push_back(v - 1);
    G[v - 1].push_back(u - 1);
    G_w[u - 1].push_back(w);
    G_w[v - 1].push_back(w);
  }

  dfs(0, G, G_w, visit, nagasa);

  for (int i = 0; i < N; i++) {

    if (nagasa[i] == 0) {

      cout << '1' << endl;
    } else {

      cout << '0' << endl;
    }
  }

  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define fi first
#define se second
vector<pair<int, int>> adj[10001];
int color[10001];
bool visited[10001];
void dfs(pair<int, int> cur) {
  visited[cur.fi] = true;
  for (int i = 0; i < adj[cur.fi].size(); i++) {
    if (!visited[adj[cur.fi][i].fi]) {
      if (adj[cur.fi][i].se % 2 == 0)
        color[adj[cur.fi][i].fi] = color[cur.fi];
      else
        color[adj[cur.fi][i].fi] = 1 - color[cur.fi];
      dfs(adj[cur.fi][i]);
    }
  }
}
int main() {
  int a, x, y, z;
  cin >> a;
  for (int i = 1; i < a; i++) {
    cin >> x >> y >> z;
    adj[x].push_back({y, z});
    adj[y].push_back({x, z});
  }
  pair<int, int> param;
  param.fi = 1;
  dfs(param);
  for (int i = 1; i <= a; i++)
    cout << color[i] << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define fi first
#define se second
vector<pair<int, int>> adj[100001];
int color[100001];
bool visited[100001];
void dfs(pair<int, int> cur) {
  visited[cur.fi] = true;
  for (int i = 0; i < adj[cur.fi].size(); i++) {
    if (!visited[adj[cur.fi][i].fi]) {
      if (adj[cur.fi][i].se % 2 == 0)
        color[adj[cur.fi][i].fi] = color[cur.fi];
      else
        color[adj[cur.fi][i].fi] = 1 - color[cur.fi];
      dfs(adj[cur.fi][i]);
    }
  }
}
int main() {
  int a, x, y, z;
  cin >> a;
  for (int i = 1; i < a; i++) {
    cin >> x >> y >> z;
    adj[x].push_back({y, z});
    adj[y].push_back({x, z});
  }
  pair<int, int> param;
  param.fi = 1;
  dfs(param);
  for (int i = 1; i <= a; i++)
    cout << color[i] << endl;
}
Problem p03044 (C++)

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

void addEdge(vector<pair<int, int>> g[], int u, int v, int w) {
  // g[u].push_back({v, w});
  // g[v].push_back({u, w});
  g[u].push_back(make_pair(v, w));
  g[v].push_back(make_pair(u, w));
}

void dfs_util(vector<pair<int, int>> g[], int s, bool col[], int pcol,
              vector<bool> &visited) {
  visited[s] = true;
  for (int i = 0; i < g[s].size(); ++i) {
    if (visited[g[s][i].first]) {
      continue;
    }

    if (g[s][i].second % 2)
      col[g[s][i].first] = !pcol;
    else
      col[g[s][i].first] = pcol;
    dfs_util(g, g[s][i].first, col, col[g[s][i].first], visited);
  }
}

void dfs(vector<pair<int, int>> g[], int n, bool col[]) {
  col[1] = 0;
  vector<bool> visited(n + 1, false);
  dfs_util(g, 1, col, col[1], visited);
  for (int i = 1; i <= n; ++i)
    cout << col[i] << ' ';
}

int main() {

  // freopen("a.txt", "r", stdin);

  int N, u, v, w;
  cin >> N;
  vector<pair<int, int>> g[10010];
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v >> w;
    addEdge(g, u, v, w);
  }

  bool col[100101];
  dfs(g, N, col);

  return 0;
}

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

void addEdge(vector<pair<int, int>> g[], int u, int v, int w) {
  // g[u].push_back({v, w});
  // g[v].push_back({u, w});
  g[u].push_back(make_pair(v, w));
  g[v].push_back(make_pair(u, w));
}

void dfs_util(vector<pair<int, int>> g[], int s, bool col[], int pcol,
              vector<bool> &visited) {
  visited[s] = true;
  for (int i = 0; i < g[s].size(); ++i) {
    if (visited[g[s][i].first]) {
      continue;
    }

    if (g[s][i].second % 2)
      col[g[s][i].first] = !pcol;
    else
      col[g[s][i].first] = pcol;
    dfs_util(g, g[s][i].first, col, col[g[s][i].first], visited);
  }
}

void dfs(vector<pair<int, int>> g[], int n, bool col[]) {
  col[1] = 0;
  vector<bool> visited(n + 1, false);
  dfs_util(g, 1, col, col[1], visited);
  for (int i = 1; i <= n; ++i)
    cout << col[i] << ' ';
}

int main() {

  // freopen("a.txt", "r", stdin);

  int N, u, v, w;
  cin >> N;
  vector<pair<int, int>> g[100100];
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v >> w;
    addEdge(g, u, v, w);
  }

  bool col[100101];
  dfs(g, N, col);

  return 0;
}
Problem p03044 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

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

void dfs(int v, int p, int c) {
  dir[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() {
  while (cin >> N) {
    G.assign(N, vector<Edge>());
    rep(i, N) {
      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));
    }
    dir.assign(N, 0);
    dfs(0, -1, 1);
    for (auto v : dir)
      cout << v << endl;
  }
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

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

void dfs(int v, int p, int c) {
  dir[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() {
  while (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));
    }
    dir.assign(N, 0);
    dfs(0, -1, 1);
    for (auto v : dir)
      cout << v << endl;
  }
}
Problem p03044 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

const int MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vector<int> ans;

void dfs(const vector<vector<pair<ll, ll>>> g, int start, int color) {
  ans[start] = color;
  for (auto e : g[start]) {
    if (ans[e.first] == -1) {
      if (e.second % 2 == 0) {
        dfs(g, e.first, color);
      } else {
        dfs(g, e.first, 1 - color);
      }
    }
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<ll, ll>>> graph(N);
  rep(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    graph[u - 1].push_back({v - 1, w});
    graph[v - 1].push_back({u - 1, w});
  }

  ans.assign(N, -1);
  dfs(graph, 0, 1);

  rep(i, N) { cout << ans[i] << endl; }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

const int MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vector<int> ans;

void dfs(vector<vector<pair<ll, ll>>> &g, int start, int color) {
  ans[start] = color;
  for (auto e : g[start]) {
    if (ans[e.first] == -1) {
      if (e.second % 2 == 0) {
        dfs(g, e.first, color);
      } else {
        dfs(g, e.first, 1 - color);
      }
    }
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<ll, ll>>> graph(N);
  rep(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    graph[u - 1].push_back({v - 1, w});
    graph[v - 1].push_back({u - 1, w});
  }

  ans.assign(N, -1);
  dfs(graph, 0, 1);

  rep(i, N) { cout << ans[i] << endl; }
}
Problem p03044 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
  if (x > n) {
    return x % n;
  } else if (x < 0) {
    return x % n + n;
  } else
    return x;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  }

  else {
    return (gcd(b, a % b));
  }
}
int main() {
  ll n;
  cin >> n;
  vector<vp> g(n);
  rep(i, 0, n) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(make_pair(v, w % 2));
    g[v].push_back(make_pair(u, w % 2));
  }
  vll cost(n, -1);
  cost[0] = 0;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll tmp = q.front();
    q.pop();
    for (auto u : g[tmp]) {
      if (cost[u.first] == -1) {
        q.push(u.first);
        cost[u.first] = cost[tmp] + u.second;
        cost[u.first] %= 2;
      }
    }
  }
  rep(i, 0, n) { cout << cost[i] % 2 << endl; }
}


Fixed submission:
#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
  if (x > n) {
    return x % n;
  } else if (x < 0) {
    return x % n + n;
  } else
    return x;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  }

  else {
    return (gcd(b, a % b));
  }
}
int main() {
  ll n;
  cin >> n;
  vector<vp> g(n);
  rep(i, 0, n - 1) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(make_pair(v, w % 2));
    g[v].push_back(make_pair(u, w % 2));
  }
  vll cost(n, -1);
  cost[0] = 0;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll tmp = q.front();
    q.pop();
    for (auto u : g[tmp]) {
      if (cost[u.first] == -1) {
        q.push(u.first);
        cost[u.first] = cost[tmp] + u.second;
        cost[u.first] %= 2;
      }
    }
  }
  rep(i, 0, n) { cout << cost[i] % 2 << endl; }
}
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 MAX = 100005;
// 頂点番号と距離を記録したい.

/*
方針
rootから見ていく。親との距離が偶数なら同じ色、違うなら違う色を塗れば
よさそう。それで任意の組み合わせについて条件が達成されるが...?

λ関数でdfsを実装するのは理由がわからないけど無理そう。なので外部に.
*/

vector<P> ale[MAX];
int n;
vector<int> color(MAX, -1);

void dfs(int v, bool clr = 0, int p = -1) {
  color[v] = clr;
  for (P u : ale[v]) {
    if (u.first == p)
      continue;
    if (u.second % 2 == 0) {
      dfs(u.first, clr, v);
    } else {
      dfs(u.first, !clr, v);
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    ale[u].push_back(make_pair(v, w));
    ale[v].push_back(make_pair(u, w));
  }
  dfs(0);
  rep(i, n) { cout << color[i] << 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 MAX = 100005;
// 頂点番号と距離を記録したい.

/*
方針
rootから見ていく。親との距離が偶数なら同じ色、違うなら違う色を塗れば
よさそう。それで任意の組み合わせについて条件が達成されるが...?

λ関数でdfsを実装するのは理由がわからないけど無理そう。なので外部に.
*/

vector<P> ale[MAX];
int n;
vector<int> color(MAX, -1);

void dfs(int v, bool clr = 0, int p = -1) {
  color[v] = clr;
  for (P u : ale[v]) {
    if (u.first == p)
      continue;
    if (u.second % 2 == 0) {
      dfs(u.first, clr, v);
    } else {
      dfs(u.first, !clr, v);
    }
  }
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    ale[u].push_back(make_pair(v, w));
    ale[v].push_back(make_pair(u, w));
  }
  dfs(0);
  rep(i, n) { cout << color[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)++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;

struct edge {
  int to, w;
};

vector<edge> G[100001];
int col[10001];

void dfs(int s, int d, int prev = -1) {
  for (auto e : G[s]) {
    if (e.to == prev)
      continue;

    if ((e.w + d) % 2 == 1)
      col[e.to] = 1;
    else
      col[e.to] = 0;

    dfs(e.to, (e.w + d) % 2, s);
  }
}

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

  REP(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    G[u].emplace_back(edge{v, w});
    G[v].emplace_back(edge{u, w});
  }

  dfs(0, 0);

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

/*

*/

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

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;

struct edge {
  int to, w;
};

vector<edge> G[100001];
int col[100001];

void dfs(int s, int d, int prev = -1) {
  for (auto e : G[s]) {
    if (e.to == prev)
      continue;

    if ((e.w + d) % 2 == 1)
      col[e.to] = 1;
    else
      col[e.to] = 0;

    dfs(e.to, (e.w + d) % 2, s);
  }
}

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

  REP(i, N - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    G[u].emplace_back(edge{v, w});
    G[v].emplace_back(edge{u, w});
  }

  dfs(0, 0);

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

/*

*/
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

vector<pii> g[100001];
int c[100001];

void dfs(int p, int bef, int x) {
  c[p] = x;
  for (pii P : g[p]) {
    if (P.first != bef) {
      int to = P.first;
      int w = P.second;
      dfs(to, p, x ^ (w % 2));
    }
  }
}

int main() {
  int n;
  cin >> n;
  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u - 1].push_back(pii(v, w));
    g[v - 1].push_back(pii(u, w));
  }
  dfs(0, -1, 0);
  REP(i, n) cout << c[i] << endl;
  return 0;
}


Fixed submission:
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

vector<pii> g[100001];
int c[100001];

void dfs(int p, int bef, int x) {
  c[p] = x;
  for (pii P : g[p]) {
    if (P.first != bef) {
      int to = P.first;
      int w = P.second;
      dfs(to, p, x ^ (w % 2));
    }
  }
}

int main() {
  int n;
  cin >> n;
  REP(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back(pii(v, w));
    g[v].push_back(pii(u, w));
  }
  dfs(0, -1, 0);
  REP(i, n) cout << c[i] << endl;
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

vi colour;
vvii adj;

int dfs(int node, int father, int c, int dist) {
  if (dist % 2 == 0)
    colour[node] = c;

  for (ii i : adj[node]) {
    if (i.ff != father)
      dfs(i.ff, node, c, dist + i.ss);
  }
}

void solve() {
  int n;
  cin >> n;

  colour.resize(n, 0);
  adj.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    adj[u].pub({v, w});
    adj[v].pub({u, w});
  }

  dfs(0, -1, 1, 0);
  for (int i = 0; i < n; i++)
    cout << colour[i] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

vi colour;
vvii adj;

void dfs(int node, int father, int c, int dist) {
  if (dist % 2 == 0)
    colour[node] = c;

  for (ii i : adj[node]) {
    if (i.ff != father)
      dfs(i.ff, node, c, dist + i.ss);
  }
}

void solve() {
  int n;
  cin >> n;

  colour.resize(n, 0);
  adj.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    adj[u].pub({v, w});
    adj[v].pub({u, w});
  }

  dfs(0, -1, 1, 0);
  for (int i = 0; i < n; i++)
    cout << colour[i] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p03044 (C++)

Buggy submission (status: Time Limit Exceeded):
//
// Created by kuroneko on 2019-07-06.
//

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int d[100000];
int N;

struct edge {
  int to;
  int cost;
};

void dfs(int node, vector<vector<edge>> v, int p_color) {
  d[node] = p_color;
  for (int i = 0; i < v[node].size(); i++) {
    int next_node = v[node][i].to;
    if (d[next_node] == -1) {
      int color = p_color;
      if (v[node][i].cost % 2 == 1)
        color = 1 - color;
      dfs(next_node, v, color);
    }
  }
}

int main() {
  cin >> N;
  vector<vector<edge>> tree(N);
  for (int i = 0; i < N; i++)
    d[i] = -1;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    edge e = {v - 1, w};
    edge re = {u - 1, w};
    tree[u - 1].emplace_back(e);
    tree[v - 1].emplace_back(re);
  }

  dfs(0, tree, 0);

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

  return 0;
}

Fixed submission:
//
// Created by kuroneko on 2019-07-06.
//

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int d[100000];
int N;

struct edge {
  int to;
  int cost;
};

void dfs(int node, const vector<vector<edge>> &v, int p_color) {
  d[node] = p_color;
  for (int i = 0; i < v[node].size(); i++) {
    int next_node = v[node][i].to;
    if (d[next_node] == -1) {
      int color = p_color;
      if (v[node][i].cost % 2 == 1)
        color = 1 - color;
      dfs(next_node, v, color);
    }
  }
}

int main() {
  cin >> N;
  vector<vector<edge>> tree(N);
  for (int i = 0; i < N; i++)
    d[i] = -1;
  for (int i = 0; i < N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    edge e = {v - 1, w};
    edge re = {u - 1, w};
    tree[u - 1].emplace_back(e);
    tree[v - 1].emplace_back(re);
  }

  dfs(0, tree, 0);

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

  return 0;
}