Problem p02850 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)

typedef long long ll;

class Edge;
class Node {
public:
  vector<Edge *> con_edges;
  vector<Node *> con_nodes;
};

class Edge {
public:
  Node *a;
  Node *b;
  int color;
};

Edge *EDGE_NIL;

void Paint(Node *node, Edge *parent_edge = EDGE_NIL) {
  int color = 1;
  FOR(i, 0, node->con_edges.size()) {
    if (node->con_edges[i] == parent_edge) {
      continue;
    }
    Node *child = node->con_edges[i]->a;
    if (child == node)
      child = node->con_edges[i]->b;

    // color
    if (parent_edge->color == color)
      color += 1;
    node->con_edges[i]->color = color;
    color++;
    Paint(child, node->con_edges[i]);
  }
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  vector<Edge> edges(n - 1);
  FOR(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    edges[i].a = &nodes[a - 1];
    edges[i].b = &nodes[b - 1];
    nodes[a - 1].con_edges.push_back(&edges[i]);
    nodes[b - 1].con_edges.push_back(&edges[i]);
    nodes[b - 1].con_nodes.push_back(&nodes[a - 1]);
    nodes[a - 1].con_nodes.push_back(&nodes[b - 1]);
  }
  int k = 0;
  FOR(i, 0, n) { k = max(k, (int)nodes[i].con_edges.size()); }
  // Node *seek = &nodes[0];
  // while (true) {
  //   if (seek->con_nodes.size() == 1) break;
  //   Node *other = seek->con_nodes[0];
  //   if (other == seek) other = seek->con_nodes[1];
  //   seek = other;
  // }
  // Node *root = seek;
  // seek->con_edges[0]->color = 1;
  // Paint(seek->con_nodes[0]);
  Node *root = &nodes[0];
  Paint(root);
  cout << k << endl;
  FOR(i, 0, n - 1) { cout << edges[i].color << endl; }
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)

typedef long long ll;

class Edge;
class Node {
public:
  vector<Edge *> con_edges;
  vector<Node *> con_nodes;
};

class Edge {
public:
  Node *a;
  Node *b;
  int color;
};

Edge *EDGE_NIL;

void Paint(Node *node, Edge *parent_edge = EDGE_NIL) {
  int color = 1;
  FOR(i, 0, node->con_edges.size()) {
    if (node->con_edges[i] == parent_edge) {
      continue;
    }
    Node *child = node->con_edges[i]->a;
    if (child == node)
      child = node->con_edges[i]->b;

    // color
    if (parent_edge != EDGE_NIL) {
      if (parent_edge->color == color)
        color += 1;
    }
    node->con_edges[i]->color = color;
    color++;
    Paint(child, node->con_edges[i]);
  }
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  vector<Edge> edges(n - 1);
  FOR(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    edges[i].a = &nodes[a - 1];
    edges[i].b = &nodes[b - 1];
    nodes[a - 1].con_edges.push_back(&edges[i]);
    nodes[b - 1].con_edges.push_back(&edges[i]);
    nodes[b - 1].con_nodes.push_back(&nodes[a - 1]);
    nodes[a - 1].con_nodes.push_back(&nodes[b - 1]);
  }
  int k = 0;
  FOR(i, 0, n) { k = max(k, (int)nodes[i].con_edges.size()); }
  // Node *seek = &nodes[0];
  // while (true) {
  //   if (seek->con_nodes.size() == 1) break;
  //   Node *other = seek->con_nodes[0];
  //   if (other == seek) other = seek->con_nodes[1];
  //   seek = other;
  // }
  // Node *root = seek;
  // seek->con_edges[0]->color = 1;
  // Paint(seek->con_nodes[0]);
  Node *root = &nodes[0];
  Paint(root);
  cout << k << endl;
  FOR(i, 0, n - 1) { cout << edges[i].color << endl; }
  return 0;
}