Problem p02850 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

const int maxn = 1e2 + 5;
struct Edge {
  int u, v, c;
} E[maxn * 2];
set<int> colr[maxn];
VI G[maxn];

void dfs(const int u, const int p) {
  int c = 1;
  FOR_ALL(e, G[u]) {
    const int v = E[*e].v;
    if (v != p) {
      while (in_(c, colr[u]))
        c++;
      colr[u].insert(c);
      colr[v].insert(c);
      E[*e].c = c;
      dfs(v, u);
    }
  }
}

int main() {
#ifdef foo_
  freopen("foo", "rt", stdin);
#endif
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    FOR(i, 0, n + 1)
    colr[i].clear(), G[i].clear();
    int j = 0;
    FOR(_, 0, n - 1) {
      int u, v;
      cin >> u >> v;
      E[j].u = u;
      E[j].v = v;
      E[j].c = 0;
      G[u].push_back(j);
      j++;
      E[j].u = v;
      E[j].v = u;
      E[j].c = 0;
      G[v].push_back(j);
      j++;
    }
    dfs(1, 1);
    VI ans;
    FOR(i, 0, j) if (E[i].c) ans.push_back(E[i].c);
    set<int> S(ALL(ans));
    cout << S.size() << endl;
    FOR_ALL(a, ans)
    cout << *a << endl;
  }
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

const int maxn = 1e5 + 5;
struct Edge {
  int u, v, c;
} E[maxn * 2];
set<int> colr[maxn];
VI G[maxn];

void dfs(const int u, const int p) {
  int c = 1;
  FOR_ALL(e, G[u]) {
    const int v = E[*e].v;
    if (v != p) {
      while (in_(c, colr[u]))
        c++;
      colr[u].insert(c);
      colr[v].insert(c);
      E[*e].c = c;
      dfs(v, u);
    }
  }
}

int main() {
#ifdef foo_
  freopen("foo", "rt", stdin);
#endif
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    FOR(i, 0, n + 1)
    colr[i].clear(), G[i].clear();
    int j = 0;
    FOR(_, 0, n - 1) {
      int u, v;
      cin >> u >> v;
      E[j].u = u;
      E[j].v = v;
      E[j].c = 0;
      G[u].push_back(j);
      j++;
      E[j].u = v;
      E[j].v = u;
      E[j].c = 0;
      G[v].push_back(j);
      j++;
    }
    dfs(1, 1);
    VI ans;
    FOR(i, 0, j) if (E[i].c) ans.push_back(E[i].c);
    set<int> S(ALL(ans));
    cout << S.size() << endl;
    FOR_ALL(a, ans)
    cout << *a << endl;
  }
  return 0;
}