#K51732. Assembly Order

    ID: 29153 Type: Default 1000ms 256MiB

Assembly Order

Assembly Order

You are given a directed graph representing dependencies between assembly components. Each component must be assembled after the components it depends on. Your task is to output a valid assembly order (i.e., a topological ordering) if possible; otherwise, output impossible.

Formally, you are given a directed graph with \(n\) nodes and \(m\) edges. An edge from node \(u\) to node \(v\) indicates that component \(u\) must be assembled before component \(v\). A valid assembly order is an ordering \(a_1, a_2, \ldots, a_n\) such that for every edge \(u \to v\), \(u\) appears before \(v\) in the ordering. If no such ordering exists (i.e., the graph contains a cycle), output impossible.

inputFormat

The first line contains an integer \(T\) denoting the number of test cases. Each test case is described as follows:

  • The first line contains two integers \(n\) and \(m\): the number of nodes and the number of edges.
  • The next \(m\) lines each contain two integers \(u\) and \(v\) indicating a directed edge from \(u\) to \(v\).

outputFormat

For each test case, output a single line. If a valid assembly order exists, print the nodes in that order, separated by a single space. If no valid ordering exists, print impossible.

## sample
2
4 4
1 2
2 3
3 4
4 1
3 2
1 2
2 3
impossible

1 2 3

</p>