LeetCode 310: Minimum Height Trees
A triply nested loops always look scary. Especially with while (true).
First thing first, trust yourself. The leaf node (and the ptr_a contains only leafs) has only one neighbour. The
for (const auto&amp; node : graph[leaf].neighbors)

loop is effectively
auto&amp; node = graph[leaf].neighbors.begin();

Second, no naked loops please. And more functions please. The
for (const auto&amp; leaf : *ptr_a)

prunes leaves from the tree. Factor it out into a prune_leaves function, which would return a set (technically a vector) of the new leaves:
leaves = prune_leaves(leaves, graph);

Third, the outer loop shall naturally terminate when less than 3 leaves remain.
Finally, separate IO from the business logic. That said, a code along the lines of
    graph = build_graph();
    leaves = collect_leaves(graph);
    while (leaves.size() &lt; 3) {
        leaves = prune_leaves(leaves, graph);
    }
    return leaves;

would win my endorsement. Notice how ptr_a and ptr_b - which are not the most descriptive names - disappear.