Problem p03044 (C++)

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

// マクロ //
#define rep(i, N) for (int i = 0; i < N; i++)
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) x.erase(unique(all(x)), x.end())
#define vsum(x) accumulate(all(x), 0)
#define cou(x) cout << x << endl
#define y() cout << "Yes" << endl
#define n() cout << "No" << endl
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define x2(x) (x) * (x)

// 型エイリアス //
using lint = long long;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using msi = map<string, int>;

// 関数 //
int gcd(int a, int b) {
  int t;
  while (b != 0) {
    t = a % b;
    a = b;
    b = t;
  }
  return a;
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

double distance(pii a, pii b) {
  double dist;
  dist = sqrt(x2(a.first - b.first) + x2(a.second - b.second));
  return dist;
}

lint perm(int a) {
  lint perm = 1;
  for (int i = a; i >= 1; i--) {
    perm *= i;
  }
  return perm;
}

lint comb(int n, int m) { return perm(n) / (perm(n - m) * perm(m)); }

// 定数 //
#define pi acos(-1)
// End of Template //

vvi graph;
vector<tuple<int, int, int>> length;
vli color;
vb colored;
int getLength(int k, int n) {
  int l = 0;
  int min = 0;
  int max = length.size();
  int mid = max / 2;
  int s = k > n ? n : k;
  int b = k > n ? k : n;
  while (1) {
    if (s > get<0>(length[mid])) {
      min = mid;
      mid = (mid + max) / 2;
    } else if (s < get<0>(length[mid])) {
      max = mid;
      mid = (mid + min) / 2;
    } else {
      if (b == get<1>(length[mid])) {
        l = get<2>(length[mid]);
        return l;
      }
    }
  }
  return l;
}

void coloring(int n, lint co) {
  colored[n] = true;

  for (auto i : graph[n]) {
    if (colored[i])
      continue;
    color[i] = co + getLength(i, n);
    coloring(i, color[i]);
  }
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  color.resize(N);
  colored.assign(N, false);
  length.resize(N - 1);
  graph.assign(N, vi(0));
  int a, b, l;
  rep(i, N - 1) {
    cin >> a >> b >> l;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
    length[i] = make_tuple(a > b ? b - 1, a - 1, l : a - 1, b - 1, l);
  }

  sort(length);

  coloring(0, 0);
  rep(i, N) { cou(color[i] % 2); }
}

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

// マクロ //
#define rep(i, N) for (int i = 0; i < N; i++)
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) x.erase(unique(all(x)), x.end())
#define vsum(x) accumulate(all(x), 0)
#define cou(x) cout << x << endl
#define y() cout << "Yes" << endl
#define n() cout << "No" << endl
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define x2(x) (x) * (x)

// 型エイリアス //
using lint = long long;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using msi = map<string, int>;

// 関数 //
int gcd(int a, int b) {
  int t;
  while (b != 0) {
    t = a % b;
    a = b;
    b = t;
  }
  return a;
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

double distance(pii a, pii b) {
  double dist;
  dist = sqrt(x2(a.first - b.first) + x2(a.second - b.second));
  return dist;
}

lint perm(int a) {
  lint perm = 1;
  for (int i = a; i >= 1; i--) {
    perm *= i;
  }
  return perm;
}

lint comb(int n, int m) { return perm(n) / (perm(n - m) * perm(m)); }

// 定数 //
#define pi acos(-1)
// End of Template //

vvi graph;
vector<tuple<int, int, int>> length;
vli color;
vb colored;
int getLength(int k, int n) {
  int l = 0;
  int min = 0;
  int max = length.size();
  int mid = max / 2;
  int s = k > n ? n : k;
  int b = k > n ? k : n;
  while (1) {
    if (s > get<0>(length[mid])) {
      min = mid;
      mid = (mid + max) / 2;
    } else if (s < get<0>(length[mid])) {
      max = mid;
      mid = (mid + min) / 2;
    } else {
      if (b == get<1>(length[mid])) {
        l = get<2>(length[mid]);
        return l;
      } else if (b > get<1>(length[mid])) {
        min = mid;
        mid = (mid + max) / 2;
      } else if (b < get<1>(length[mid])) {
        max = mid;
        mid = (mid + min) / 2;
      }
    }
  }
  return l;
}

void coloring(int n, lint co) {
  colored[n] = true;

  for (auto i : graph[n]) {
    if (colored[i])
      continue;
    color[i] = co + getLength(i, n);
    coloring(i, color[i]);
  }
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  color.resize(N);
  colored.assign(N, false);
  length.resize(N - 1);
  graph.assign(N, vi(0));
  int a, b, l;
  rep(i, N - 1) {
    cin >> a >> b >> l;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
    length[i] = make_tuple(a > b ? b - 1, a - 1, l : a - 1, b - 1, l);
  }

  sort(length);

  coloring(0, 0);
  rep(i, N) { cou(color[i] % 2); }
}