Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
899 B
4
Indexable
Never
def distinctOrder(g_nodes, g_from, g_to):

    # Initialize the adjacency list
    adj_list = {i: [] for i in range(1, g_nodes+1)}
    for u, v in zip(g_from, g_to):
        adj_list[u].append(v)
        adj_list[v].append(u)

    # Perform depth-first search (DFS)
    visited = {i: False for i in range(1, g_nodes+1)}
    stack = [1]
    A = []
    while stack:
        u = stack.pop()
        if not visited[u]:
            visited[u] = True
            A.append(u)
            for v in adj_list[u]:
                if not visited[v]:
                    stack.append(v)

    # Create the lexicographically largest possible array B
    B = []
    for i in range(len(A)):
        found = False
        for j in range(len(B)):
            if A[i] == B[j]:
                found = True
                break
        if not found:
            B.append(A[i])
    B.sort(reverse=True)
    
    return B