Language: C++

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringJoiner;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.*;

public class Main {
    public static class Edge {
      int idx;
      int node;
      public Edge(int n, int i) {
        this.idx = i;
        this.node = n;
      }
    }
    static In in = new In();
    static Out out = new Out();
    static long mod = 1000000007;
    static long inf = 0xfffffffffffffffL;

    void solve() {
        int n = in.nextInt();

        Map<Integer, List<Edge>> graph = new HashMap<>();
        Map<Integer, Set<Integer>> usedColor = new HashMap<>();
        for(int i = 1; i <= n; i++) {
            graph.put(i, new ArrayList<>());
            usedColor.put(i, new HashSet<>());
        }

        for(int i = 0; i < n - 1; i++) {
            int v = in.nextInt();
            int u = in.nextInt();
            graph.get(v).add(new Edge(u, i));
            graph.get(u).add(new Edge(v, i));
        }

        int cnt = 0;
        for(List<Edge> adjs : graph.values())
            cnt = Math.max(cnt, adjs.size());
        out.println(cnt);
        
        int[] colors = new int[n-1];
        Queue<Integer> queue = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        queue.offer(1);
        visited.add(1);
        while(!queue.isEmpty()) {
          int node = queue.poll();
          int c = 1;
          for(Edge e : graph.get(node)) {
            if(visited.contains(e.node)) continue;
            while(usedColor.get(node).contains(c) || usedColor.get(e.node).contains(c)) c++;
            if(colors[e.idx] == 0)
                colors[e.idx] = c;
            usedColor.get(node).add(c);
            usedColor.get(e.node).add(c);
            queue.offer(e.node);
            visited.add(e.node);
            c++;
          }
        }
        for(int i = 0; i < colors.length; i++)
          out.println(colors[i]);
    }

    public static void main(String[]$) {
        new Main().solve();
        out.flush();
    }

    static public class In {
        private BufferedInputStream reader = new BufferedInputStream(System.in);
        private byte[] buffer = new byte[0x8000];
        private int i = 0;
        private int length = 0;

        private int read() {
            if (i == length) {
                i = 0;
                try {
                    length = reader.read(buffer);
                } catch (IOException ignored) {
                }
                if (length == -1) {
                    return -1;
                }
            }
            return buffer[i++];
        }

        String next() {
            StringBuilder builder = new StringBuilder();
            int b = read();
            while (b < '!' || '~' < b) {
                b = read();
            }
            while ('!' <= b && b <= '~') {
                builder.appendCodePoint(b);
                b = read();
            }
            return builder.toString();
        }

        int nextInt() {
            return (int)nextLong();
        }

        long nextLong() {
            int b = read();
            while (b < '!' || '~' < b) {
                b = read();
            }
            int i = 0;
            if (b == '-') {
                i = 1;
                b = read();
            }
            long n = 0;
            while ('0' <= b && b <= '9') {
                n = n * 10 + b - '0';
                b = read();
            }
            return i == 0 ? n : -n;
        }

        char[][] nextCharArray(int n, int m) {
            char[][] a = new char[n][m];
            for (int i = 0; i < n; i++) {
                a[i] = next().toCharArray();
            }
            return a;
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        int[] nextIntArray(int n, IntUnaryOperator op) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = op.applyAsInt(nextInt());
            }
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextLong();
            }
            return a;
        }

        long[] nextLongArray(int n, LongUnaryOperator op) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = op.applyAsLong(nextLong());
            }
            return a;
        }
    }

  static class Out {
      private static PrintWriter out = new PrintWriter(System.out);

      void println(Object... a) {
          StringJoiner joiner = new StringJoiner(" ");
          for (Object obj : a) {
              joiner.add(String.valueOf(obj));
          }
          out.println(joiner);
      }

      void print(Object... a) {
          StringJoiner joiner = new StringJoiner(" ");
          for (Object obj : a) {
              joiner.add(String.valueOf(obj));
          }
          out.print(joiner);
      }

      void flush() {
          out.flush();
      }
  }
}


Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.io.BufferedInputStream;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.IOException;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.PrintWriter;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.StringJoiner;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.function.IntUnaryOperator;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: 'import' does not name a type
    6 | import java.util.function.LongUnaryOperator;
      | ^~~~~~
a.cc:6:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:1: error: 'import' does not name a type
    7 | import java.util.*;
      | ^~~~~~
a.cc:7:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:9:1: error: expected unqualified-id before 'public'
    9 | public class Main {
      | ^~~~~~