Problem p03044 (C++)

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

using namespace std;

/*

        Bismillahir Rahmanir Rahim
        Problem :
        Problem Link :
        Topics :
        Solver : Masud Parves
        I Love Code More than Sharks Love Blood <3
*/

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define SZ(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()

#define sf(a) scanf("%d", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)

#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)

#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TEST_CASE(t) for (int zz = 1; zz <= t; zz++)
#define PRINT_CASE printf("Case %d: ", zz)
#define Debug(x) cout << #x " = " << (x) << endl
#define NOT_VISITED 0
#define IS_VISITED 1
#define WHITE 0
#define GRAY 1
#define BLACK 2

int fx[4] = {1, -1, 0, 0};
int fy[4] = {0, 0, 1, -1};

/*-----------------------Bitmask------------------*/
// int Set(int N,int pos){return N=N  (1<<pos);}
// int Reset(int N,int pos){return N= N & ~(1<<pos);}
// bool Check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;

vector<int> g[maxn];
vector<ll> cost[maxn];
bool visited[maxn];
int color[maxn];

void bfs(int src) {
  queue<int> Q;

  Q.push(src);
  visited[src] = 1;
  color[src] = 1;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!visited[v]) {
        Q.push(v);
        int w = cost[u][i];
        if (w % 2 == 0) {
          color[v] = color[u];
        } else {
          if (color[u] == 0)
            color[v] = 1;
          else
            color[v] = 0;
        }
      }
    }
  }
}

int main() {
  CIN
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);

      int n;
  cin >> n;

  f1(i, n - 1) {
    int u, v;
    ll w;
    cin >> u >> v >> w;

    g[u].pb(v);
    g[v].pb(u);
    cost[u].pb(w);
    cost[v].pb(w);
  }

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

  return 0;
}


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

using namespace std;

/*

        Bismillahir Rahmanir Rahim
        Problem :
        Problem Link :
        Topics :
        Solver : Masud Parves
        I Love Code More than Sharks Love Blood <3
*/

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define SZ(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()

#define sf(a) scanf("%d", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)

#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)

#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TEST_CASE(t) for (int zz = 1; zz <= t; zz++)
#define PRINT_CASE printf("Case %d: ", zz)
#define Debug(x) cout << #x " = " << (x) << endl
#define NOT_VISITED 0
#define IS_VISITED 1
#define WHITE 0
#define GRAY 1
#define BLACK 2

int fx[4] = {1, -1, 0, 0};
int fy[4] = {0, 0, 1, -1};

/*-----------------------Bitmask------------------*/
// int Set(int N,int pos){return N=N  (1<<pos);}
// int Reset(int N,int pos){return N= N & ~(1<<pos);}
// bool Check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;

vector<int> g[maxn];
vector<ll> cost[maxn];
bool visited[maxn];
int color[maxn];

void bfs(int src) {
  queue<int> Q;

  Q.push(src);
  visited[src] = 1;
  color[src] = 1;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!visited[v]) {
        Q.push(v);
        visited[v] = 1;
        int w = cost[u][i];
        if (w % 2 == 0) {
          color[v] = color[u];
        } else {
          if (color[u] == 0)
            color[v] = 1;
          else
            color[v] = 0;
        }
      }
    }
  }
}

int main() {
  CIN
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);

      int n;
  cin >> n;

  f1(i, n - 1) {
    int u, v;
    ll w;
    cin >> u >> v >> w;

    g[u].pb(v);
    g[v].pb(u);
    cost[u].pb(w);
    cost[v].pb(w);
  }

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

  return 0;
}