_DOCS = {
    "tree traversals": """from babygrad2 import scan, TreeOps\nn = scan.int()\nvals = []\nfor _ in range(n):\n    op = scan.int()\n    if op == 1: vals.append(scan.int())\n    else:\n        root = TreeOps.from_level_order(vals)\n        if not root: print("Empty")\n        else:\n            t = TreeOps.get_traversals(root)\n            print(*t['in'] if op==2 else (t['pre'] if op==3 else t['post']))""",

    "bst ops": """from babygrad2 import scan, TreeOps\nn, root = scan.int(), None\nres = []\nfor _ in range(n):\n    op = scan.int()\n    if op == 1: root = TreeOps.bst_insert(root, scan.int())\n    elif op == 2: root = TreeOps.bst_delete(root, scan.int())\n    elif op == 3: res.append("found" if TreeOps.bst_search(root, scan.int()) else "not found")\n    elif op in (4,5,6):\n        if not root: res.append("Empty")\n        else: res.append(" ".join(map(str, TreeOps.get_traversals(root)['in' if op==4 else ('pre' if op==5 else 'post')] )))\nprint(*res, sep='\\n')""",

    "huffman": """from babygrad2 import scan, TreeOps\nchars = scan.str()\nfreqs = scan.list_fixed(len(chars))\nroot = TreeOps.huffman_tree(chars, freqs)\ncodes = []\ndef dfs(n, p):\n    if not n: return\n    if n.data: codes.append(p)\n    dfs(n.left, p+"0"); dfs(n.right, p+"1")\ndfs(root, "")\nprint(*codes)""",

    "avl insert": """from babygrad2 import scan, TreeOps\nvals = scan.list_fixed(scan.int())\nroot = None\nfor v in vals:\n    rot = []\n    root = TreeOps.avl_insert(root, v, lambda x: rot.append(x))\n    if rot: print(rot[-1])\n    print(*TreeOps.get_traversals(root)['in'])""",

    "tree height": """from babygrad2 import scan, TreeOps\nscan.int()\nprint(TreeOps.height(TreeOps.from_level_order(scan.list_until())))""",

    "build pre in": """from babygrad2 import scan, TreeOps\nn = scan.int()\nprint(*TreeOps.get_traversals(TreeOps.build_pre_in(scan.list_fixed(n), scan.list_fixed(n)))['post'])""",

    "tree diameter": """from babygrad2 import scan, TreeOps\nprint(TreeOps.diameter(TreeOps.from_level_order(scan.list_until(sentinel=None)), edges=True))""",

    "bst lca": """from babygrad2 import scan, TreeOps\nn = scan.int()\nvals = scan.list_fixed(n)\nroot = None\nfor v in vals: root = TreeOps.bst_insert(root, v)\nprint(TreeOps.lca(root, scan.int(), scan.int()))""",

    "perfect family": """from babygrad2 import scan, TreeOps\nscan.int()\nroot = None\nfor v in scan.list_until(sentinel=None): root = TreeOps.bst_insert(root, int(v))\nprint(1 if TreeOps.perfect_family(root) else 0)""",

    "tree mirror": """from babygrad2 import scan, TreeOps\nroot = TreeOps.from_level_order(scan.list_until('-1'))\nprint(1 if TreeOps.is_symmetric(root) else 0)""",

    "kth smallest": """from babygrad2 import scan, TreeOps\nvals = scan.list_until('-1')\nprint(TreeOps.kth_smallest(TreeOps.from_level_order(vals), scan.int()))""",

    "level order": """from babygrad2 import scan, TreeOps\nscan.int()\nroot = None\nfor v in scan.list_until(sentinel=None): root = TreeOps.bst_insert(root, int(v))\nprint(*TreeOps.bfs_list(root))""",

    "max leaf sum": """from babygrad2 import scan, TreeOps\nprint(TreeOps.max_leaf_to_leaf_sum(TreeOps.from_level_order(scan.list_until('-1'))))""",

    # --- Graph series ---
    "graph matrix ops": """from babygrad2 import scan, GraphOps\nops = scan.int()\ng = GraphOps()\nverts = set()\nfor _ in range(ops):\n    cmd = scan.int()\n    if cmd == 1: verts.add(scan.int())\n    elif cmd == 2:\n        v = scan.int()\n        if v in verts:\n            verts.remove(v)\n            if v in g.g:\n                g.g.remove_node(v)\n    elif cmd == 3:\n        g.add(scan.int(), scan.int())\n    elif cmd == 4:\n        u, v = scan.int(), scan.int()\n        if g.g.has_edge(u, v):\n            g.g.remove_edge(u, v)\n    elif cmd == 5:\n        print(\"Yes\" if g.g.has_edge(scan.int(), scan.int()) else \"No\")\n\ns_verts = sorted(list(verts))\nfor i in s_verts:\n    print(*[1 if g.g.has_edge(i, j) else 0 for j in s_verts])""",

    "graph adj list": """from babygrad2 import scan, GraphOps\nn, ops = scan.int(), scan.int()\ng = GraphOps()\nfor _ in range(ops):\n    cmd = scan.str()\n    if cmd == \"ADD\":\n        g.add(scan.int(), scan.int())\n    elif cmd == \"DEL\":\n        u, v = scan.int(), scan.int()\n        if g.g.has_edge(u, v):\n            g.g.remove_edge(u, v)\n    elif cmd == \"NEIGHBORS\":\n        u = scan.int()\n        nbrs = sorted(list(g.g.neighbors(u))) if u in g.g else []\n        print(*(nbrs if nbrs else [-1]))\ng.print_adj_list(n)""",

    "dfs traversal": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps()\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nprint(*g.dfs(scan.int()))""",

    "bfs traversal": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps()\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nres = g.bfs(scan.int())\nq, visited = [res[0]] if res else [], set(res[:1])\nwhile q:\n    lvl = q[:]\n    q = []\n    print(*lvl)\n    for u in lvl:\n        for v in sorted(g.g.neighbors(u)):\n            if v not in visited:\n                visited.add(v)\n                q.append(v)""",

    "connected components": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps()\ng.g.add_nodes_from(range(v))\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nfor c in g.components():\n    print(*c)""",

    "topo kahn": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\ng.g.add_nodes_from(range(v))\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nt = g.topo_sort()\nprint(*t if t else [\"Cycle detected\"])""",

    "topo matrix": """from babygrad2 import scan, GraphOps\nn = scan.int()\nmat = scan.matrix(n, n)\ng = GraphOps.from_matrix(mat, directed=True)\nt = g.topo_sort()\nprint(*t)""",

    "is bipartite": """from babygrad2 import scan, GraphOps\nn = scan.int()\nmat = scan.matrix(n, n)\ng = GraphOps.from_matrix(mat)\nscan.int()  # source ignored\nprint(\"Yes\" if g.is_bipartite() else \"No\")""",

    "count islands": """from babygrad2 import scan, GraphOps\nr, c = scan.int(), scan.int()\ngrid = scan.matrix(r, c)\nprint(GraphOps.count_islands(grid))""",

    "min network ops": """from babygrad2 import scan, GraphOps\nn = scan.int()\nconns = []\nwhile True:\n    u = scan.int()\n    if u == -1:\n        break\n    conns.append((u, scan.int()))\nif len(conns) < n - 1:\n    print(-1)\nelse:\n    g = GraphOps()\n    g.g.add_nodes_from(range(n))\n    g.g.add_edges_from(conns)\n    print(len(g.components()) - 1)""",

    "job scheduling": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\ng.g.add_nodes_from(range(1, v + 1))\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\ntimes = {n: 1 for n in range(1, v + 1)}\nfor u in g.topo_sort():\n    for v in g.g.neighbors(u):\n        times[v] = max(times[v], times[u] + 1)\nprint(*[times[i] for i in range(1, v + 1)])""",

    "prerequisites": """from babygrad2 import scan, GraphOps\nn, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nprint(1 if g.topo_sort() else 0)""",

    "flood fill": """from babygrad2 import scan, GraphOps\nr, c = scan.int(), scan.int()\ngrid = scan.matrix(r, c)\nx, y, col = scan.int(), scan.int(), scan.int()\nres = GraphOps.flood_fill(grid, x, y, col)\nfor row in res:\n    print(*row)""",

    "nasa pairs": """from babygrad2 import scan, GraphOps\nn, p = scan.int(), scan.int()\ng = GraphOps()\ng.g.add_nodes_from(range(n))\nfor _ in range(p):\n    g.add(scan.int(), scan.int())\nsizes = [len(c) for c in g.components()]\nrem = sum(sizes)\nans = 0\nfor s in sizes:\n    rem -= s\n    ans += s * rem\nprint(ans)""",

    "create graph": """from babygrad2 import scan, GraphOps\nv = scan.int()\nif not (1 <= v <= 10):\n    print(\"-1\")\nelse:\n    mat = scan.matrix(v, v, transformer=int)\n    for row in mat:\n        print(*row)""",

    "dfs reachable": """from babygrad2 import scan, GraphOps\nn = scan.int()\nmat = scan.matrix(n, n)\ng = GraphOps.from_matrix(mat, directed=True)\nstart = scan.int()\nprint(f\"Reachable nodes from {start}:\", *g.dfs(start))""",

    "dfs adj list": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nprint(*g.dfs(scan.int()))""",

    "is connected": """from babygrad2 import scan, GraphOps\nv = scan.int()\nmat = scan.matrix(v, v)\ng = GraphOps.from_matrix(mat)\nu, v = scan.int(), scan.int()\nprint(nx.has_path(g.g, u, v))""",

    "biconnected": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps()\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nprint(g.biconnected_count())""",

    "cycle detect": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps()\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\ntry:\n    nx.find_cycle(g.g)\n    print(\"Yes\")\nexcept Exception:\n    print(\"No\")""",

    "all paths": """from babygrad2 import scan, GraphOps\nv, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nsrc, dst = scan.int(), scan.int()\npaths = g.all_paths(src, dst)\nfor p in paths:\n    print(*p)""",

    "graph symmetric": """from babygrad2 import scan, GraphOps\nv = scan.int()\nmat = scan.matrix(v, v)\nprint(\"The graph is symmetric\" if GraphOps.is_symmetric(mat) else \"The graph is not symmetric\")""",

    "mother vertex": """from babygrad2 import scan, GraphOps\nn, e = scan.int(), scan.int()\ng = GraphOps(directed=True)\ng.g.add_nodes_from(range(n))\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nprint(g.mother_vertex(n))""",

    "path exists": """from babygrad2 import scan, GraphOps\nn = scan.int()\ne = scan.int()\ng = GraphOps()\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\nsrc, dst = scan.int(), scan.int()\nprint(\"true\" if nx.has_path(g.g, src, dst) else \"false\")""",

    "grid path 123": """from babygrad2 import scan, GraphOps\nn = scan.int()\ngrid = scan.matrix(n, n)\nprint(1 if GraphOps.grid_path(grid, 1, 2, {3}) else 0)""",

    "transpose matrix": """from babygrad2 import scan, GraphOps\nv = scan.int()\nmat = scan.matrix(v, v)\nres = GraphOps.transpose_matrix(mat)\nfor row in res:\n    print(*row)""",

    "island strike": """from babygrad2 import scan, GraphOps\nn, m = scan.int(), scan.int()\ngrid = [list(scan.str()) for _ in range(n)]\nprint(GraphOps.count_islands(grid))""",

    "degrees": """from babygrad2 import scan, GraphOps\nv = scan.int()\ne = scan.int()\ng = GraphOps(directed=True)\ng.g.add_nodes_from(range(v))\nfor _ in range(e):\n    g.add(scan.int(), scan.int())\ng.print_degrees(v)""",
}


def doc(key: str):
    """Prints the solution code for the given key (no return to avoid REPL echo)."""
    if not key:
        return None
    k = key.lower().strip()
    snippet = _DOCS.get(k)
    if snippet is None:
        msg = f"# No docs found for '{key}'. Available: {', '.join(_DOCS.keys())}"
        print(msg)
        return None
    print(snippet)
    return None

