Ciel the Commander
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".
In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.
The first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.It guaranteed that the given graph will be a tree.
If there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i. Otherwise output "Impossible!".
def shallowest_tree_decomposition(graph):
    n = len(graph)
    decomposition_tree = [[] for _ in range(n)]
    stacks = [[] for _ in range(n.bit_length())]

    def create_chain(labels, u):
        while labels:
            label = labels.bit_length() - 1
            labels ^= 1 << label
            v = stacks[label].pop()
            decomposition_tree[u].append(v)
            u = v
    dp = [-1] * n
    root = 0
    dfs = [root]
    while dfs:
        u = dfs.pop()
        if u >= 0:
            dp[u] = 0
            dfs.append(~u)
            for v in graph[u]:
                if dp[v] == -1:
                    dfs.append(v)
        else:
            u = ~u
            seen = seen_twice = 0
            for v in graph[u]:
                seen_twice |= seen & dp[v]
                seen |= dp[v]
            dp[u] = 1 + (seen | ~(~0 << seen_twice.bit_length()))
            stacks[(dp[u] & -dp[u]).bit_length() - 1].append(u)
            for v in graph[u]:
                create_chain(dp[v] & ~dp[u], u)
    decomposition_root = stacks[dp[root].bit_length() - 1].pop()
    create_chain(dp[root] & ~dp[decomposition_root], decomposition_root)
    return (decomposition_tree, decomposition_root)
import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]
ii = 0
n = inp[ii]
ii += 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1
    ii += 1
    v = inp[ii] - 1
    ii += 1
    graph[u].append(v)
    graph[v].append(u)
(decomposition_tree, root) = shallowest_tree_decomposition(graph)
labels = [0] * n
bfs = [root]
for node in bfs:
    for child in decomposition_tree[node]:
        labels[child] = labels[node] + 1
        bfs.append(child)
alpha = ''.join((chr(ord('A') + i) for i in range(26)))
print(' '.join((alpha[label] for label in labels)))
def centroid_tree_decomposition(graph):
    n = len(graph)
    graph = [c[:] for c in graph]
    bfs = [0]
    for node in bfs:
        bfs += graph[node]
        for nei in graph[node]:
            graph[nei].remove(node)
    size = [0] * n
    for node in reversed(bfs):
        size[node] = 1 + sum((size[child] for child in graph[node]))
    decomposition_tree = [[] for _ in range(n)]

    def centroid_reroot(u):
        N = size[u]
        while True:
            for v in graph[u]:
                if size[v] > N // 2:
                    size[u] = N - size[v]
                    graph[u].remove(v)
                    graph[v].append(u)
                    u = v
                    break
            else:
                for v in graph[u]:
                    decomposition_tree[u].append(centroid_reroot(v))
                return u
    decomposition_root = centroid_reroot(0)
    return (decomposition_tree, decomposition_root)
import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]
ii = 0
n = inp[ii]
ii += 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1
    ii += 1
    v = inp[ii] - 1
    ii += 1
    graph[u].append(v)
    graph[v].append(u)
(decomposition_tree, root) = centroid_tree_decomposition(graph)
labels = [0] * n
bfs = [root]
for node in bfs:
    for child in decomposition_tree[node]:
        labels[child] = labels[node] + 1
        bfs.append(child)
alpha = ''.join((chr(ord('A') + i) for i in range(26)))
print(' '.join((alpha[label] for label in labels)))
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

def shallowest_tree_decomposition(graph):
    n = len(graph)
    decomposition_tree = [[] for _ in range(n)]
    stacks = [[] for _ in range(n.bit_length())]

    def create_chain(labels, u):
        while labels:
            label = labels.bit_length() - 1
            labels ^= 1 << label
            v = stacks[label].pop()
            decomposition_tree[u].append(v)
            u = v
    dp = [0] * n

    @bootstrap
    def dfs(u, p):
        seen = seen_twice = 0
        for v in graph[u]:
            if v != p:
                yield dfs(v, u)
                seen_twice |= seen & dp[v]
                seen |= dp[v]
        dp[u] = 1 + (seen | ~(~0 << seen_twice.bit_length()))
        stacks[(dp[u] & -dp[u]).bit_length() - 1].append(u)
        for v in reversed(graph[u]):
            create_chain(dp[v] & ~dp[u], u)
        yield
    root = 0
    dfs(root, -1)
    decomposition_root = stacks[dp[root].bit_length() - 1].pop()
    create_chain(dp[root] & ~dp[decomposition_root], decomposition_root)
    return (decomposition_tree, decomposition_root)
import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]
ii = 0
n = inp[ii]
ii += 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1
    ii += 1
    v = inp[ii] - 1
    ii += 1
    graph[u].append(v)
    graph[v].append(u)
(decomposition_tree, root) = shallowest_tree_decomposition(graph)
labels = [0] * n
bfs = [root]
for node in bfs:
    for child in decomposition_tree[node]:
        labels[child] = labels[node] + 1
        bfs.append(child)
alpha = ''.join((chr(ord('A') + i) for i in range(26)))
print(' '.join((alpha[label] for label in labels)))